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

Bots Facebook messenger平台:具有快速回复的通用模板,bots,facebook-messenger,facebook-send-api,Bots,Facebook Messenger,Facebook Send Api,我在看一些非常流行的机器人,比如《卫报》,我注意到每当你从它那里得到一个通用的模板回复时,它也会显示一些快速回复按钮(见附图)。“守护机器人”是如何做到这一点的?他是如何将快速回复和通用模板结合起来的?必须包含两条消息 我已经在nodejs中实现了bot,并且我正在使用一个名为messenger bot的节点模块,这使得调用messenger bot API更加容易。这是我为你定制的代码 const http = require('http') const https = require('ht

我在看一些非常流行的机器人,比如《卫报》,我注意到每当你从它那里得到一个通用的模板回复时,它也会显示一些快速回复按钮(见附图)。“守护机器人”是如何做到这一点的?他是如何将快速回复和通用模板结合起来的?必须包含两条消息


我已经在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.

    }
});
参考文献-


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

快速回复通常伴随着一个“text”属性,该属性在快速回复之前发送一条文本消息。看来你可以用任何模板来代替它。例如,以下是带有快速回复的通用模板转盘的请求正文:

{
  "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>"
              }
            ]      
          }
        ]
      }
    }
  }
}
{
“收件人”:{
“id”:“{PSID}”
},
“消息类型”:“响应”,
“信息”:{
“快速回复”:[
{
“内容类型”:“文本”,
“标题”:“快速回复1”,
“图像url”:https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
“有效载荷”:“有效载荷1”
},
{
“内容类型”:“文本”,
“标题”:“快速回复2”,
“有效载荷”:“有效载荷2”
}
],
“附件”:{
“类型”:“模板”,
“有效载荷”:{
“模板类型”:“通用”,
“要素”:[
{
“标题”:“这是一个通用模板”,
“字幕”:“加字幕!”,
“图像url”:https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
“按钮”:[
{
“类型”:“回发”,
“标题”:“回发按钮”,
“有效负载”:”
}
]      
}, 
{
“标题”:“另一个通用模板”,
“字幕”:“加字幕!”,
“图像url”:https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
“按钮”:[
{
“类型”:“回发”,
“标题”:“回发按钮”,
“有效负载”:”
}
]      
},
{
“标题”:“还有另一个!”,
“字幕”:“加字幕!”,
“图像url”:https://raw.githubusercontent.com/fbsamples/messenger-platform-samples/master/images/Messenger_Icon.png",
“按钮”:[
{
“类型”:“回发”,
“标题”:“回发按钮”,
“有效负载”:”
}
]      
}
]
}
}
}
}

这在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"
      }
    ]
  }
}
{
“facebook”:{
“附件”:{
“类型”:“模板”,
“有效载荷”:{
“模板类型”:“通用”,
“要素”:[
{
“标题”:“欢迎!”,
“图像url”:https://petersfancybrownhats.com/company_image.png",
“副标题”:“我们有适合每个人的帽子。”,
“默认操作”:{
“类型”:“web_url”,
“url”:”https://petersfancybrownhats.com/view?item=103",
“网络视图高度比”:“高”
},
“按钮”:[
{
“类型”:“web_url”,
“url”:”https://petersfancybrownhats.com",
“标题”:“查看网站”
},{
“类型”:“回发”,
“标题”:“开始聊天”,
“有效负载”:“开发者定义的有效负载”
}              
]      
}
]
}
},
“快速回复”:[
{
“内容类型”:“文本”,
“标题”:“搜索”,
“有效载荷”:“,
“图像url”:http://example.com/img/red.png"
},
{
“内容类型”:“位置”
}
]
}
}

新更新

{
  "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"
      }
    ]
  }
}
{
“facebook”:{
“附件”:{
“类型”:“模板”,
“有效载荷”:{
“模板类型”:“通用”,
“要素”:[
{
“标题”:“欢迎!”,
“图像url”:https://petersfancybrownhats.com/company_image.png",
“副标题”:“我们有适合每个人的帽子。”,
“默认操作”:{
“类型”:“web_url”,
“url”:”https://petersfancybrownhats.com/view?item=103",
“网络视图高度比”:“高”
},
“按钮”:[
{
“类型”:“web_url”,
“url”:”https://petersfancybrownhats.com",
“标题”:“查看网站”
},{
“类型”:“回发”,
“标题”:“开始聊天”,
“有效负载”:“开发者定义的有效负载”
}              
]      
}
]
}
},
“快速回复”:[
{
“内容类型”:“文本”,
“标题”:“红色”,
“有效载荷”:“,
“图像url”:http://example.com/img/red.png"
},{
“内容类型”:“文本”,
“标题”:“绿色”,
“有效载荷”:“,
“图像url”:http://example.com/img/green.png"
}
]
}
}

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