Facebook messenger bot问题(欢迎信息和按钮信息)

时间:2016-06-19 02:16:27

标签: facebook facebook-graph-api heroku facebook-messenger

我在开发Facebook messenger机器人时面临两个问题,而且我是编程的新手。

  1. 我按照FB的教程添加代码 - 欢迎消息并将其部署在heroku中,但我的机器人没有弹出上述消息。

    app.post('/webhook/', function (req, res) {
    let messaging_events = req.body.entry[0].messaging
    for (let i = 0; i < messaging_events.length; i++) {
      let event = req.body.entry[0].messaging[i]
      let sender = event.sender.id
      if (event.message && event.message.text) {
        let text = event.message.text
        if (text === 'Generic') {
            sendGenericMessage(sender)
            continue
        }
        if (text === 'button') {
            sendbuttonmessage(sender)
            continue
        }
        welcomemessage(sender)
        //sendbuttonmessage(sender)
      }
      if (event.postback) {
        let text = JSON.stringify(event.postback)
        sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token)
        continue
      }
    }
    res.sendStatus(200)   })
    
  2. function welcomemessage (sender) {   let messageData = {
        "setting_type":"call_to_actions",   "thread_state":"new_thread",   "call_to_actions":[
        {
          "message":{
            "text":"Welcome to My Company!"
          }
        }   ] }   request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
          method: 'POST',
          json: {
              recipient: {id:sender},
              message: messageData,
          }   }, function(error, response, body) {
          if (error) {
              console.log('Error sending messages: ', error)
          } else if (response.body.error) {
              console.log('Error: ', response.body.error)
          }   }) }
    
    1. 当ppl点击它们时,如何弹出另一个按钮?例如: 在点击按钮的网址后发送功能sendbuttonmessage(sender)
    2. function sendbuttonmessage (sender) {
          let messageData = {
              "attachment": {
                "type":"template",
                "payload":{
                  "template_type":"button",
                  "text":"Welcome to Taikoo Place. How can we help?",
                  "buttons":[
                    {
                      "type":"web_url",
                      "url":"https://peterapparel.parseapp.com",
                      "title":"Show Website"
                    },
                    {
                      "type":"postback",
                      "title":"Service Lift Booking",
                      "payload":"what"
                      //"payload":"USER_DEFINED_PAYLOAD"
                    },
                  ]
                }
              }
            }
      

1 个答案:

答案 0 :(得分:0)

对于第一个问题,您应该将欢迎消息设置为显示https://developers.facebook.com/docs/messenger-platform/send-api-reference#welcome_message_configuration的独立POST请求,因为它使用不同的API并且只应在ONCE执行。

curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"call_to_actions",
  "thread_state":"new_thread",
  "call_to_actions":[
    {
      "message":{
        "text":"Welcome to My Company!"
      }
    }
  ]
}' "https://graph.facebook.com/v2.6/<PAGE_ID>/thread_settings?access_token=<PAGE_ACCESS_TOKEN>"

对于第二个问题,您无法检测用户何时单击按钮的Web网址,因为它将从外部转到该链接。但是,您可以首先使用postback设置设置消息,在接收消息时可以处理回发,请参阅此处处理回发的方法https://developers.facebook.com/docs/messenger-platform/quickstart BTW还记得在Subscription Fields下设置 messaging_postbacks