2017-06-29 62 views
0

我正在看一些像「衛報」一些非常流行的機器人,我注意到,每當你從它得到一個通用的模板回覆它也會顯示一些快速回覆按鈕(見附圖)。 「衛報機器人」是如何實現這一目標的?他如何將快速回復和通用模板結合起來?它必須涉及兩條消息。Facebook信使平臺:通用模板與快速回復

enter image description here

+0

該博客發佈第一個附件,然後是菜單。那麼? –

+0

它的工作原理是什麼?它首先發送通用模板,然後快速回復?但在快速回復中,您必須指定標題和標題不能爲空。 –

+0

那麼?他們已經指定了標題:) –

回答

1

我已經實現在對的NodeJS機器人,我使用所謂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

希望這有助於!快速編碼;)

0

快速回復通常伴隨着一個'文本'屬性,在快速回復之前發送文本消息。看起來你可以用任何模板替代它。例如,以下是具有快速回復的通用模板輪播的請求正文:

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