快速回复 Facebook Messenger 问题

Quick reply on Facebook messenger problem

我在 Facebook Messenger 聊天机器人中遇到 quick_reply 问题;

问题是:我创建了一个 2 选项快速回复按钮,但我不知道如何在单击 2 按钮之一后继续流程。

这里是代码:https://glitch.com/edit/#!/daffodil-authorization?path=app.js:267:3

它是如何工作的?:如果你点击 "what is the meaning of life",2 个快速回复按钮将出现“42”和 "chocolate",但在点击时,机器人会像往常一样回答你(重复什么你写);我怎样才能让机器人听“42”或 "chocolate" 并继续做其他事情?

Quick Replies are different from Buttons in that their payload do not return as Postback 条消息,而是作为普通消息传递,就好像用户自己键入并发送消息一样。

它实际上只是用户 return 回复的一种方式, 比打字更快。所以您的机器人将其视为普通消息,因为它是作为普通消息发送的。

因此,要么您的代码需要解析和捕获正常的传入消息,寻找“42”或 "chocolate"。或者从使用快速回复改为使用实际的回发按钮,这样您就可以通过回发消息 return 捕获它们。

您可以使用 Quick Replies 然后捕获用户对它们的响应!

我的机器人没有在寻找按钮标题(你的“42”和 "chocolate",我将它们排除在我的 echo 功能之外)并且取决于 "Payload" 运行下一个动作。

"When a quick reply is tapped, the buttons are dismissed, and the title of the tapped button is posted to the conversation as a message. A messages event will be sent to your webhook that contains the button title and an optional payload."

为快速回复按钮添加了负载(使用了 2 k-v 对):

response_sent_text = "My text"
buttons = (
            {
                "content_type": "text",
                "title": "Python_title",
                "payload": json.dumps(
                    {"id": i_id, "language": "python"}
                ),
            },
        )
        bot.send_message(
            recipient_id,
            message={"text": response_sent_text, "quick_replies": buttons},
        )

并用这个来捕捉:

for message in messaging:
    payload = (message.get("message", {}).get("quick_reply", {}).get("payload"))
        if payload:
            returned_payload = json.loads(payload)
            if returned_payload["language"] == "python":
                pass