Facebook messenger平台:具有快速回复的通用模板

时间:2017-06-29 08:30:44

标签: bots facebook-messenger facebook-send-api

我正在看一些非常受欢迎的机器人,比如“卫报”,我注意到每当你从它获得一个通用模板回复时,它也会显示一些快速回复按钮(见附图)。 “卫报”机器人是如何实现这一目标的?他如何结合快速回复和通用模板?它必须涉及两条消息。

enter image description here

4 个答案:

答案 0 :(得分:2)

快速回复通常附有“文字”属性,可在快速回复之前发送短信。您似乎可以替换任何模板。例如,以下是具有快速回复的通用模板轮播的请求正文:

{
  "recipient":{
    "id":"{{PSID}}"
  },
  "messaging_type": "response",
  "message":{
        "quick_replies": [
      {
        "content_type":"text",
        "title":"Quick Reply 1",
        "image_url":"https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
        "payload":"payload1"
      },
      {
        "content_type":"text",
        "title":"Quick Reply 2",
        "payload":"payload2"
      }
    ],

    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
          {
            "title":"This is a generic template",
            "subtitle":"Plus a subtitle!",
            "image_url":"https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
            "buttons":[
              {
                "type":"postback",
                "title":"Postback Button",
                "payload":"<POSTBACK_PAYLOAD>"
              }
            ]      
          }, 
          {
            "title":"Another generic template",
            "subtitle":"Plus a subtitle!",
            "image_url":"https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
            "buttons":[
              {
                "type":"postback",
                "title":"Postback Button",
                "payload":"<POSTBACK_PAYLOAD>"
              }
            ]      
          },
          {
            "title":"And another!",
            "subtitle":"Plus a subtitle!",
            "image_url":"https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
            "buttons":[
              {
                "type":"postback",
                "title":"Postback Button",
                "payload":"<POSTBACK_PAYLOAD>"
              }
            ]      
          }
        ]
      }
    }
  }
}

答案 1 :(得分:0)

我在nodejs中实现了bot,我正在使用一个名为messenger-bot的节点模块,这样可以更容易地调用messenger bot API。这是我的自定义代码

const http = require('http')
const https = require('https')
const Bot = require('messenger-bot')
var bot = new Bot({
    token: 'your FB app token',
    verify: 'VERIFY_TOKEN'
})

bot.on('postback', (payload, reply) => {
    var postback = payload.postback.payload;
    if (postback == "yes") {

        function getQuickReplies() {
            console.log("in next function");
            var quick_list = {
                "text": "Check the next article?",
                "quick_replies": [{
                        "content_type": "text",
                        "title": "More stories",
                        "payload": "more stories"
                    },
                    {
                        "content_type": "text",
                        "title": "Sport",
                        "payload": "sport"
                    },
                    {
                        "content_type": "text",
                        "title": "Business",
                        "payload": "business"
                    }

                ]
            };
            bot.getProfile(payload.sender.id, (err, profile) => {
                if (err) throw err
                text = quick_list;
                bot.sendMessage(payload.sender.id, text) {//this prints quick replies
                    console.log("sending message");
                }
            });
        }

        //calling generic template

        var generic_temp = "message": {
            "attachment": {
                -- - your code-- -
            }
        }; //generic template refer - https://developers.facebook.com/docs/messenger-platform/send-api-reference/generic-template

        bot.getProfile(payload.sender.id, (err, profile) => {
            if (err) throw err
            bot.sendMessage(payload.sender.id, generic_temp) {//this prints generic template
                console.log("sending message");
            }
        });

        //calling the quick replies once the generic template is sent

        getQuickReplies(); //to avoid async execution issue, we will have to put this in a function.

    }
});

参考 - Generic templateQuick repliesmessenger-bot npm

希望这有帮助!快乐的编码;)

答案 2 :(得分:0)

这在Dialogflow中对我有用,在后端返回类似的Json Object以实现结果:

 {
  "facebook": {
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
           {
            "title":"Welcome!",
            "image_url":"https://petersfancybrownhats.com/company_image.png",
            "subtitle":"We have the right hat for everyone.",
            "default_action": {
              "type": "web_url",
              "url": "https://petersfancybrownhats.com/view?item=103",
              "webview_height_ratio": "tall"
            },
            "buttons":[
              {
                "type":"web_url",
                "url":"https://petersfancybrownhats.com",
                "title":"View Website"
              },{
                "type":"postback",
                "title":"Start Chatting",
                "payload":"DEVELOPER_DEFINED_PAYLOAD"
              }              
            ]      
          }
        ]
      }
    },
     "quick_replies":[
      {
        "content_type":"text",
        "title":"Search",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/red.png"
      },
      {
        "content_type":"location"
      }
    ]
  }
}

答案 3 :(得分:0)

新更新

{
  "facebook": {
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
           {
            "title":"Welcome!",
            "image_url":"https://petersfancybrownhats.com/company_image.png",
            "subtitle":"We have the right hat for everyone.",
            "default_action": {
              "type": "web_url",
              "url": "https://petersfancybrownhats.com/view?item=103",
              "webview_height_ratio": "tall"
            },
            "buttons":[
              {
                "type":"web_url",
                "url":"https://petersfancybrownhats.com",
                "title":"View Website"
              },{
                "type":"postback",
                "title":"Start Chatting",
                "payload":"DEVELOPER_DEFINED_PAYLOAD"
              }              
            ]      
          }
        ]
      }
    },
    "quick_replies":[
      {
        "content_type":"text",
        "title":"Red",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/red.png"
      },{
        "content_type":"text",
        "title":"Green",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/green.png"
      }
    ]
  }
}