I successfully created my first Telegram bot and connected it via webhook to a public API exposed using AWS API Gateway that triggers a lambda written in Java 8.
I verified the connection using CloudWatch, sending a message to the bot and receiving a log written in the lambda.
My problem lies in the lambda code, because although the communication works, it contains information that I believe is wrong.
Let me explain:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class App implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(Object update, Context context) {
context.getLogger().log("Object: " + update);
return 0
}
}
This is the simple Lambda content that allowed me to get this log:
"Object":{
version=1.0,
"resource=/javatest",
"path=/default/javatest",
"httpMethod=POST",
"headers="{
Content-Length=273,
"Content-Type=application/json",
Host=odd9kjkana.execute-api.eu-west-1.amazonaws.com,
X-Amzn-Trace-Id=Root=1-663158ad-585f95f7491168214c543f33,
X-Forwarded-For=91.108.6.96,
X-Forwarded-Port=443,
"X-Forwarded-Proto=https",
"accept-encoding=gzip",
"deflate"
},
"multiValueHeaders="{
"Content-Length="[
273
],
"Content-Type="[
"application/json"
],
"Host="[
odd9kjiaoa.execute-api.eu-west-1.amazonaws.com
],
"X-Amzn-Trace-Id="[
Root=1-663158ad-585f95f7491168214c543f33
],
"X-Forwarded-For="[
91.108.6.96
],
"X-Forwarded-Port="[
443
],
"X-Forwarded-Proto="[
"https"
],
"accept-encoding="[
"gzip",
"deflate"
]
},
"queryStringParameters=null",
"multiValueQueryStringParameters=null",
"requestContext="{
accountId=891370093172,
apiId=odd9kjkana,
domainName=odd9kjkana.execute-api.eu-west-1.amazonaws.com,
domainPrefix=odd9kjkana,
extendedRequestId=XDrLLhT4DoEEPSg=,
"httpMethod=POST",
"identity="{
"accessKey=null",
"accountId=null",
"caller=null",
"cognitoAmr=null",
"cognitoAuthenticationProvider=null",
"cognitoAuthenticationType=null",
"cognitoIdentityId=null",
"cognitoIdentityPoolId=null",
"principalOrgId=null",
sourceIp=91.108.6.96,
"user=null",
"userAgent=",
"userArn=null"
},
"path=/default/javatest",
protocol=HTTP/1.1,
requestId=XDrLLhT4DoEEPSg=,
"requestTime=30/Apr/2024":"20":"46":37 +0000,
requestTimeEpoch=1714509997602,
"resourceId=ANY /javatest",
"resourcePath=/javatest",
"stage=default"
},
"pathParameters=null",
"stageVariables=null",
"body="{
"update_id":879419474,
"message":{
"message_id":107,
"from":{
"id":177801168,
"is_bot":false,
"first_name":"Legolas",
"username":"Sl1me",
"language_code":"it"
},
"chat":{
"id":177801168,
"first_name":"Legolas",
"username":"Sl1me",
"type":"private"
},
"date":1714507808,
"text":"Test37"
}
},
isBase64Encoded=false
}
While the message I had sent was very different, that ‘Test37’ found was from a few days ago.
That said, once I understand the correct structure obviously I will have to try to correctly deserialize the object sent by Telegram, for now I need to understand why it returns me an old message and what is the structure I should expect so that I can build the appropriate supporting classes.
Just for completeness, by directly calling the bot via polling (https://api.telegram.org/bot{MY_TOKEN}/getUpdates?offset=-1) I get the result I am looking for, but precisely I want to use webhooks.
I’m trying to consult this guide, but I can’t find the problem.
I hope you can help me. Thank you