I am creating a basic bot in Python. Since I am new to bot development I don't know why i am getting this key error.But My bot replied as expected My code is:
@app.route('/',methods=['POST'])
def webhook():
data=request.get_json()
log(data)
#if the request was not get, it must be POST and we can just proceed with sending a message back to user
# get whatever message a user sent the bot
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for messageInfo in messaging:
if messageInfo.get('message'):
recipient_id = messageInfo['sender']['id']
if messageInfo['message'].get('text'):
print (messageInfo['message'].get('text'))
response_sent_text = get_message()
send_message(recipient_id, response_sent_text)
#if user sends us a GIF, photo,video, or any other non-text item
if messageInfo['message'].get('attachments'):
response_sent_nontext = get_message()
send_message(recipient_id, response_sent_nontext)
return "Message Processed"
return "OK",200
I am getting the following error in the logs:
{'object': 'page', 'entry': [{'id': '129852174411680', 'time': 1517975084635, 'messaging': [{'sender': {'id': '1670470769698580'}, 'recipient': {'id': '129852174411680'}, 'timestamp': 1517975084622, 'delivery': {'mids': ['mid.$cAAAs7X9hZjVnn6LkNFhbl2bs5jB5'], 'watermark': 1517975084084, 'seq': 0}}]}]}
2018-02-07T03:44:44.792984+00:00 app[web.1]: [2018-02-07 03:44:44,792] ERROR in app: Exception on / [POST]
2018-02-07T03:44:44.792987+00:00 app[web.1]: Traceback (most recent call last):
2018-02-07T03:44:44.792989+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
2018-02-07T03:44:44.792991+00:00 app[web.1]: response = self.full_dispatch_request()
2018-02-07T03:44:44.792993+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
2018-02-07T03:44:44.792995+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-02-07T03:44:44.792997+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
2018-02-07T03:44:44.792998+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-02-07T03:44:44.793000+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2018-02-07T03:44:44.793002+00:00 app[web.1]: raise value
2018-02-07T03:44:44.793004+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
2018-02-07T03:44:44.793006+00:00 app[web.1]: rv = self.dispatch_request()
2018-02-07T03:44:44.793007+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
2018-02-07T03:44:44.793009+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-02-07T03:44:44.793011+00:00 app[web.1]: File "/app/app.py", line 28, in webhook
2018-02-07T03:44:44.793013+00:00 app[web.1]: if messageInfo['message'].get('text'):
2018-02-07T03:44:44.793018+00:00 app[web.1]: KeyError: 'message'
2018-02-07T03:44:44.793550+00:00 app[web.1]: 10.5.185.134 - - [07/Feb/2018:03:44:44 +0000] "POST / HTTP/1.1" 500 291 "-" "-"
I tried to find out the error by printing the output.
That error means, you are trying to lookup a key "message" but that key isn't there.
If you log messageInfo, you'll see that there isn't a "message" key. Both of these lines will throw an error if there is no message key.
if messageInfo['message'].get('text'):
...
if messageInfo['message'].get('attachments'):
You could try something like this to make the error go away.
if messageInfo.get('message', {}).get('text')