Facebook messenger机器人的快速回复没有有效载荷

时间:2016-10-16 12:06:05

标签: javascript facebook bots payload

我正在使用此代码在facebook messenger bot(JavaScript)中快速回复按钮:

    var message = {
    "attachment": {
        "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                    "title": "Order a cucumber",
                    "subtitle": "A cucumber",
                    "image_url": "SOME_LINK_HERE",
                    "buttons": [
                        {"type": "postback",
                        "title": "Order",
                        "payload": "cucumber"},
                        {"type": "postback",
                        "title": "Order & Finish",
                        "payload": "cucumberf"}
                    ]
                },
                {
                    "title": "Order a tomato",
                    "subtitle": "A tomato",
                    "image_url": "SOME_LINK_HERE",
                    "buttons": [
                        {"type": "postback",
                        "title": "Order",
                        "payload": "tomato"},
                        {"type": "postback",
                        "title": "Order & Finish",
                        "payload": "tomatof"}
                    ]
                },
                {
                    "title": "Order a cake",
                    "subtitle": "A cake",
                    "image_url": "SOME_LINK_HERE",
                    "buttons": [
                        {"type": "postback",
                        "title": "Order",
                        "payload": "cake"},
                        {"type": "postback",
                        "title": "Order & Finish",
                        "payload": "cakef"}
                    ]
                }]
            }
    },
    "quick_replies": [
        {"content_type": "text",
        "title": "Main menu",
        "payload": "mainMenu"}
    ]};

当我试图获取快速回复按钮(event.message.quick_reply)的有效负载时,它说它未定义。但对于其他字段,它返回它应该的内容(如“seq”字段或“text”字段。 我做错了什么,如何使用快速回复按钮的有效负载?

1 个答案:

答案 0 :(得分:1)

下面的代码向您展示了如何获取用户发送/点击的所有内容:

// handle bot's anwser
$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];

// gets the text the user sent to you
$messageText = $input['entry'][0]['messaging'][0]['message']['text'];

// gets the payload of the button
$buttonPayLoad = $input['entry'][0]['messaging'][0]['postback']['payload'];

// gets the payload of the quick replay
$quickReplyPayLoad = $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload'];