【问题标题】:Facebook messenger platform: generic template with quick repliesFacebook Messenger 平台:具有快速回复的通用模板
【发布时间】:2017-12-02 20:28:30
【问题描述】:

我正在查看一些非常流行的机器人,例如“卫报”,我注意到每当您从它那里获得通用模板回复时,它还会显示一些快速回复按钮(见附图)。 “守护者机器人”是如何做到这一点的?他如何结合快速回复和通用模板?必须涉及两条消息。

【问题讨论】:

  • 机器人发布第一个附件,然后是菜单。那么?
  • 它到底是如何工作的?它首先发送通用模板然后快速回复?但在快速回复中,您必须指定标题,并且标题不能为空。
  • 所以?他们已经指定了标题:)
  • 那么标题是什么?
  • 有5个标题

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


【解决方案1】:

我已经在 nodejs 中实现了机器人,并且我正在使用一个名为 messenger-bot 的节点模块,这使得调用 messenger 机器人 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 template、Quick replies、messenger-bot npm

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

【讨论】:

    【解决方案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>"
                  }
                ]      
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

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

       {
        "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"
            }
          ]
        }
      }
      

      【讨论】:

        【解决方案4】:

        新更新

        {
          "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"
              }
            ]
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-26
          • 1970-01-01
          • 2016-08-09
          • 1970-01-01
          • 2016-08-08
          相关资源
          最近更新 更多