key error: message in fb messenger bot
key error: message in fb messenger bot
我正在 Python 中创建一个基本的机器人。由于我是机器人开发的新手,我不知道为什么我会得到这个密钥 error.But 我的机器人按预期回复了
我的代码是:
@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
我在日志中收到以下错误:
{'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 "-" "-"
我试图通过打印输出来找出错误。
该错误意味着,您正在尝试查找键 "message" 但该键不存在。
如果您记录 messageInfo,您会看到没有 "message" 键。如果没有消息键,这两行都会抛出错误。
if messageInfo['message'].get('text'):
...
if messageInfo['message'].get('attachments'):
您可以尝试这样的操作来消除错误。
if messageInfo.get('message', {}).get('text')
我正在 Python 中创建一个基本的机器人。由于我是机器人开发的新手,我不知道为什么我会得到这个密钥 error.But 我的机器人按预期回复了 我的代码是:
@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
我在日志中收到以下错误:
{'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 "-" "-"
我试图通过打印输出来找出错误。
该错误意味着,您正在尝试查找键 "message" 但该键不存在。
如果您记录 messageInfo,您会看到没有 "message" 键。如果没有消息键,这两行都会抛出错误。
if messageInfo['message'].get('text'):
...
if messageInfo['message'].get('attachments'):
您可以尝试这样的操作来消除错误。
if messageInfo.get('message', {}).get('text')