【发布时间】:2016-10-12 07:44:34
【问题描述】:
有人可以向我解释一下 Facebook 聊天机器人按钮元素中的“有效负载”字段是什么吗?我是机器人开发的新手。如果你也能提供一个例子就好了。
【问题讨论】:
标签: facebook-chatbot
有人可以向我解释一下 Facebook 聊天机器人按钮元素中的“有效负载”字段是什么吗?我是机器人开发的新手。如果你也能提供一个例子就好了。
【问题讨论】:
标签: facebook-chatbot
“有效负载”字段是用户定义的字段,使您能够在收到带有此有效负载的回发时调用操作。
例如;如果我在我的机器人中创建一个包含 2 个按钮的持久菜单:“主页”和“联系人”,并且每个按钮的有效负载都与按钮的名称相同。当用户单击“主页”按钮时,将使用有效负载“主页”发送回发。在这种情况下,您可以创建一个操作,将用户带到机器人的“主页”部分。
有关回发和有效负载的更多信息,请访问: https://developers.facebook.com/docs/messenger-platform/send-api-reference/postback-button https://developers.facebook.com/docs/messenger-platform/webhook-reference/postback-received
确保在您的主“post”函数中创建一个处理回发的函数。以下代码来自 Python 中的机器人教程
# Post function to handle facebook messages
def post(self, request, *args, **kwargs):
# converts the text payload into a python dictionary
incoming_message = json.loads(self.request.body.decode('utf-8'))
# facebook recommends going through every entry since they might send
# multiple messages in a single call during high load
for entry in incoming_message['entry']:
for message in entry['messaging']:
# check to make sure the received call is a message call
# this might be delivery, optin, postback for other events
if 'message' in message:
pprint(message)
### add here the rest of the code that will be handled when the bot receives a message ###
if 'postback' in message:
# print the message in terminal
pprint(message)
### add here the rest of the code that will be handled when the bot receives a postback ###
【讨论】: