how to setup “Get started” button in facebook messenger bot and when to send welcome message

寵の児 提交于 2019-12-04 03:54:46

Make a POST call to API JSON body as below.

curl -X POST -H "Content-Type: application/json" -d '{
"setting_type":"call_to_actions",
"thread_state":"new_thread",
"call_to_actions":[
 {
  "payload":"USER_DEFINED_PAYLOAD"
 }
]
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=PAGE_ACCESS_TOKEN"

Ref: Facebook messenger get started button

Postman screentshot

Neha Creado

The current format is, https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN

{ 
  "get_started":{
    "payload":"GET_STARTED_PAYLOAD"
  }
}

It is possible you set it successfully, but are not seeing it because you already have an existing conversation with the facebook page.

After successfully setting the "Get Started" thread, you will only see it if you delete your existing conversation thread and start a new one.

Get started button is only shown when you are interacting with the facebook page for the first time, so if you have previously messaged the page, you will not be able to see "Get Started" unless you delete the thread from your Facebook Messenger client (either mobile or desktop).

From the FB Messenger Docs:

There are certain conditions to seeing the Welcome Screen and the Get Started button:

  • They are only rendered the first time the user interacts with the Page on Messenger
  • Only admins/developers/testers of the app can see it when the app is in development mode
  • Your app must be subscribed to postbacks on your webhook

thanks for the valuable comments, after some workaround found this solution working, as per facebook guidelines

need to send a independent POST request to this URL only ONCE

https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN

used postman to send request like this here

If the Get Started button was successfully set, you will get the following response

{
  "result": "Successfully added new_thread's CTAs"
}

you have to run an appropriate curl command to set it up. check this link out and look at their example. https://developers.facebook.com/docs/messenger-platform/implementation#send_api

There is a library in npm that wraps the functionality of the POST/DELETE actions here: https://www.npmjs.com/package/fb-get-started-button

$ npm install -g fb-get-started-button

$ fb-get-started-button add <YOUR PAGE ACCESS TOKEN>
Adding "Get Started" button with the payload "GET_STARTED"
Successfully added new_thread's CTAs

$ fb-get-started-button remove <YOUR PAGE ACCESS TOKEN>
Removing "Get Started" button
Successfully deleted all new_thread's CTAs
MithooG

Send a post request using your page access token

https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR-TOKEN

with following data

{ 
  "get_started":{
     "payload":"<GET_STARTED_PAYLOAD>"
   }
}

Facebook Docs: Get Started Button

Hope this new method solve your problem. Don't forget to delete sent messages first using Facebook Web to see button in action.

A better solution in my opinion is to use the Microsoft Bot Framework and use its /firstRun to send the messenger get started button

function firstRun(session) {
  console.log('This user is running our bot the first time')
  createUser(session)
  platforms.firstRun(session.message.user.id, session.message.address.channelId)
    .then((values) => {
      for (let value of values) {
        if (value.data.firstName && value.data.lastName) {
          session.userData.user.profile = value.data
        }
      }
    })
    .catch((errors => {
      console.log(errors);
    }))
  reply(session)
  session.endDialog()
}

The platforms.firstRun looks as shown below

platforms.firstRun = function (userId, channel) {
    switch (channel) {
        case platforms.channels.emulator:
            return Promise.reject('none')
        case platforms.channels.facebook:
            return platforms.facebook.firstRun(userId)
        case platforms.channels.skype:
            return Promise.reject('none')
        default:
            return Promise.reject('none')
    }
}

This in turn calls platforms.facebook.firstRun

platforms.facebook.firstRun = function (userId) {
    return Promise.all([
        platforms.facebook.sendThread(facebookTemplates.greet(), 'Greeting'),
        platforms.facebook.sendThread(facebookTemplates.getStarted(), 'Get Started'),
        platforms.facebook.sendThread(facebookTemplates.getPersistentMenu(), 'Persistent Menu'),
        platforms.facebook.sendThread(facebookTemplates.getDomainWhitelisting(), 'Domain Whitelisting'),
        platforms.facebook.getProfile(userId)
    ])
}

The platforms.facebook.sendThread looks as shown below // Calls the Facebook graph api to change bot settings

platforms.facebook.sendThread = function (template, cmd) {

    return new Promise((resolve, reject) => {
        // Start the request
        request({
            url: platforms.facebook.GRAPH_BASE_URI + '/me/thread_settings?access_token=' + endpoints.FACEBOOK_PAGE_ACCESS_TOKEN,
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            form: template
        },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    // Print out the response body
                    resolve({ status: response.statusCode, data: body })
                } else {
                    // TODO: Handle errors
                    reject({ status: response.statusCode, data: error })
                }
            });
    })
}

Notice the facebookTemplates.getStarted(), that actually has the json for get started which looks as shown below

templates.getStarted = function () {
    return {
        setting_type: "call_to_actions",
        thread_state: "new_thread",
        call_to_actions: [
            {
                payload: payloads.FACEBOOK_GET_STARTED
            }
        ]
    }
}

Fully pluggable code architecture for performing a first run operation across all chatbot platforms. Works perfectly on my bot HERE

In our case, the following worked:

  • Hit the thread_settings API

    https://graph.facebook.com/v2.6/me/thread_settings?access_token=<YOU FACEBOOK PAGE'S PAGE ACCESS TOKEN>

  • Passed the following sample JSON

    {
      "setting_type": "call_to_actions",
      "thread_state": "new_thread",
      "call_to_actions": [
        {
          "payload": "Start"
        }
      ]
    }
  • The API shall give the following as the result:
    {
        "result": "Successfully added new_thread's CTAs"
    }
Akhmad

Very simple solution, just open in terminal and go to your host folder location,( in mine /var/www/html/booking/public/facebookbot ) and paste the following code:

curl -X POST -H "Content-type: application/json" -d '{ 
    "setting-type":"call_to_actions",
    "thread_state":"new_thread",
  "get_started":{
     "payload":"GET_STARTED_PAYLOAD"
   }
}' "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR_ACCESS_TOKEN"

and press enter, make sure to put your correct access token, also you may recognize when your getting started button pressed from payload which is GET_STARTED_PAYLOAD in above code sample.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!