0

I'm facing 2 issues when developing a Facebook messenger bot, and I'm a newbie to programming.

  1. I followed FB's tutorial to add the code - welcome message and deployed it in heroku, but my bot didn't pop up the said message.

    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. How do I pop up another button when ppl clicked them? For example: send function sendbuttonmessage(sender) after they click the web url of the button.
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"
              },
            ]
          }
        }
      }
zondo
  • 19,901
  • 8
  • 44
  • 83
shing916
  • 1
  • 1
  • 1

1 Answers1

0

For the first question, you should set the welcome message buy a independent POST request as https://developers.facebook.com/docs/messenger-platform/send-api-reference#welcome_message_configuration shown because it uses different API and it should only be executed 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>"

For the second question, you cannot not detect when user clicks the web url of the button, because it will go to the link externally. However, you can set the message with the postback settings first, them you can process the postback when receiving the message, see how to handle postback here https://developers.facebook.com/docs/messenger-platform/quickstart BTW also remember to set the messaging_postbacks under Subscription Fields.

Hui-Yu Lee
  • 909
  • 8
  • 20