I wrote a telegram bot with aws lambda. I have an Api gateway endpoint which runs the event and triggers the lambda function.
Now the problem is that I can't get the "body" because it always returns the same error.
This is the request I can get printed, is there any way to get the data in body and work on that data?
{'version': '2.0', 'routeKey': 'ANY /myTelegramBot', 'rawPath': '/V1/myTelegramBot', 'rawQueryString': '', 'headers': {'accept-encoding': 'gzip, deflate', 'content-length': '331', 'content-type': 'application/json', 'host': '00000000.execute-api.eu-west-1.amazonaws.com', 'x-amzn-trace-id': 'Root=1-62a1ea06-2d8ea4b553aasddczxc7209e7', 'x-forwarded-for': '91.18.6.83', 'x-forwarded-port': '443', 'x-forwarded-proto': 'https'
}, 'requestContext': {'accountId': '374177185789', 'apiId': 'awefsdfka3al', 'domainName': 'av3p3ka3al.execute-api.eu-west-1.amazonaws.com', 'domainPrefix': 'av3p3ka3al', 'http': {'method': 'POST', 'path': '/V1/myTelegramBot', 'protocol': 'HTTP/1.1', 'sourceIp': '91.108.6.83', 'userAgent': ''
}, 'requestId': 'TdGA-jhertEMkA=', 'routeKey': 'ANY /myTelegramBot', 'stage': 'V1', 'time': '09/Jun/2022: 12: 39: 34 +0000', 'timeEpoch': 1654778374039
}, 'body': '{
"update_id": 1234356,\n"message": {
"message_id": 226,
"from": {
"id": 456875213,
"is_bot": false,
"first_name": "gggggggg",
"last_name": "Di gggg",
"username": "gggggg",
"language_code": "it"
},
"chat": {
"id": ggggg,
"first_name": "ggggggg",
"last_name": "Di ggggg",
"username": "ggggggg",
"type": "private"
},
"date": 123123123,
"text": "HELLOMESSAGE"
}
}', 'isBase64Encoded': False
}
My Aws lambda:
CHAT_ID = 'xxxxxxxx'
TOKEN = 'xxxxx:AAEc_dP-xxxx'
WBH_URL = 'https:
def lambda_handler(event, context):
json_str = json.dumps(event)
resp = json.loads(json_str)
####ERROR HERE:
chat_id_message = resp['Body']['message']['chat']['id']
bot = telebot.TeleBot(TOKEN)
bot.send_message(CHAT_ID, "CIAOXXdalBOT" + str(resp))
Error: The Lambda function returned the following error: 'Body'. Check your Lambda function code and try again.
What I have tried:
I really tried everything, even formatting my answer with string analysis, but I think it is much easier than you think. I don't understand why the response is so bad