【问题标题】:Issue with Facebook messenger bot (welcome message and button message)Facebook Messenger bot 的问题(欢迎消息和按钮消息)
【发布时间】:2016-10-20 13:53:10
【问题描述】:

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

  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)   })
    
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. 当人们点击它们时如何弹出另一个按钮?例如: 在他们点击按钮的网址后发送函数sendbuttonmessage(sender)
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"
              },
            ]
          }
        }
      }

【问题讨论】:

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


    【解决方案1】:

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

    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>"
    

    对于第二个问题,当用户点击按钮的网址时,您无法检测到,因为它会转到外部链接。但是,您可以先使用postback 设置来设置消息,您可以在收到消息时处理回发,在此处查看如何处理回发https://developers.facebook.com/docs/messenger-platform/quickstart 顺便说一句,还记得在订阅字段下设置 messaging_postbacks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      相关资源
      最近更新 更多