什么是Facebook聊天机器人有效载荷字段?

时间:2016-10-12 07:44:35

标签: facebook-chatbot

有人可以向我解释一下facebook chatbot按钮元素中的'payload'字段是什么?我是机器人开发的新手。如果你能提供一个例子,那将会很棒。

1 个答案:

答案 0 :(得分:1)

'payload'字段是一个用户定义的字段,只要收到带有此有效负载的回发,就可以调用操作。

例如

;如果我在我的机器人中创建一个包含2个按钮的持久菜单:'Home'和'Contact',并且每个按钮的有效负载与按钮的名称相同。当用户单击“主页”按钮时,将发送带有效负载“主页”的回发。在这种情况下,您可以创建一个操作,将用户带到机器人的“主页”部分。

有关回发和有效负载的更多信息,请访问: 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的bot教程

# 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 ###