4

I would like to show 'welcome message' like 'Hi Robert, welcome to my application'. So I need to send:

  1. "https://graph.facebook.com/v2.6/USER_ID?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=PAGE_ACCESS_TOKEN"

  2. using 'first_name' from 1st request, send "https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN" request to set 'welcome message'.

However, I need to know user_id before first request.

My questions:

  1. What are steps to create 'Welcome message'?
  2. How to show 'Welcome message', when user opened facebook messenger window?

I reviewed https://developers.facebook.com/docs/messenger-platform document, and I have still questions.

Yauhen
  • 1,145
  • 12
  • 19

5 Answers5

6

So you can make this with a "get started" Button. This button is only there if the User Messages the bot for the first time.

With this command you can set the button:

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"      

As you can see if the Button is pressed your Webhook recieves a "postback"

this is the callback you get:

{
  "sender":{
    "id":"USER_ID"
  },
  "recipient":{
    "id":"PAGE_ID"
  },
  "timestamp":1458692752478,
  "postback":{
    "payload":"USER_DEFINED_PAYLOAD"
  }
}  

So this is from where you can get User id

Now you can code a Function for this. My looks like this:

function receivedPostback(event) {
  var senderID = event.sender.id;
  var recipientID = event.recipient.id;
  var timeOfPostback = event.timestamp;

  // The 'payload' param is a developer-defined field which is set in a postback 
  // button for Structured Messages. 
  var payload = event.postback.payload;

  console.log("Received postback for user %d and page %d with payload '%s' " + 
    "at %d", senderID, recipientID, payload, timeOfPostback);


  if (payload) {

    // When a postback is called, we'll send a message back to the sender to 
    // let them know it was successful
      switch (payload) {
        case 'USER_DEFINED_PAYLOAD':
          startedConv(senderID);
          break;

       default:
         sendTextMessage(senderID, "Postback called");
       }

My startedConv() looks like this

function startedConv(recipientId){
var name;

request({
          url: 'https://graph.facebook.com/v2.6/'+ recipientId +'?fields=first_name',
          qs: {access_token: PAGE_ACCESS_TOKEN},
          method: 'GET'
      }, function(error, response, body) {
          if (error) {
              console.log('Error sending message: ', error);
          } else if (response.body.error) {
              console.log('Error: ', response.body.error);
          }else{
              name = JSON.parse(body);
              sendTextMessage(recipientId, "Hello "+ name.first_name+", how can i help you ? ")
          }
      });
}
NockaRocka
  • 163
  • 1
  • 8
4

You don't need it. Just follow the steps below,

1- Go to your page that related your messenger api.

2- Settings (FB Page Settings)

3- From tabs select Messaging

4- In messaging tab at the bottom you will see "Show a Messenger Greeting" change it from "No" to "Yes". Than you can customize it ;)

Note: To see your greeting message you should delete your previous conversation with the page that you set greeting message. Then start new conversation with the page you should see greeting message.

Omera
  • 71
  • 5
  • Thank you for the answer, I have tried this option as well. But it did not work for me. I put my message and set 'Yes' to show and click 'Save button'. Unfortunately it did not work (. Maybe I missed some settings? – Yauhen Jun 22 '16 at 14:36
  • Is your app approved ? If No for now only admin, testers, and developers can see that message. And for you can check it like that, Add a friend to your test group in messenger app settings. Message should appear after he open converisation window. – Omera Jun 23 '16 at 13:44
  • Step 1 of your instructions is totally unclear as for what you mean. I searched all pages in the App Dashboard in developers.facebook... and I found no setting for a Greeting :( – iBobb Dec 20 '16 at 16:33
  • For example my page name is : KayzerSoze-1795503650697564 And settings url : https://www.facebook.com/KayzerSoze-1795503650697564/settings/?tab=messaging Just change "KayzerSoze-1795503650697564" with your page username. – Omera Dec 22 '16 at 12:03
  • @iBobb It's not in the App Dashboard, it's in the FB Page Settings – Carmela Jun 21 '17 at 03:45
  • @Omera This doesn't work well, I've turned on and off, customized, it doesn't change the Welcome Greetings. I think this is a bug that Facebook have not fixed until now. – Carmela Jun 21 '17 at 03:45
  • @Carmela I just tried and it shows me greeting message. Can you try to delete conversation i mean conversation which is you made with your page and start new one then check the greetings ? – Omera Jun 21 '17 at 07:40
  • @Omera I already did that, several times. Didn't work. I even disabled the greeting message through the FB Page, still didn't work. – Carmela Jun 21 '17 at 08:03
  • @Carmela Are you trying too see from Mobile Messenger Application right ? Because i don't see greetings in desktop i mean in https://www.messenger.com/. Only in my mobile phone's messenger apps. – Omera Jun 21 '17 at 08:31
  • @Omera Yes, that's right. My other FB Page responds to the changing greeting text, the other does not. I am admin to both pages. I explained the bug here: https://developers.facebook.com/bugs/135976446978726/ – Carmela Jun 21 '17 at 09:22
1

As of 9/12 you can set the greeting text that shows up before your user hits Get Started and it includes these tokens that will be replaced when the text is rendering:

  • {{user_first_name}}
  • {{user_last_name}}
  • {{user_full_name}}

Take a look at this page of the Messenger Platform docs: https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text

ScottWasserman
  • 1,700
  • 1
  • 11
  • 8
1

Creating a Greeting Text isn't hard at all at this point. I spent some time finding out how to do it and it turned out quite simple. I refactored one of the methods for a POST request that I found in the samples and I'm using NodeJS btw.

function createGreetingApi(data) {
request({
uri: 'https://graph.facebook.com/v2.6/me/thread_settings',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: data

}, function (error, response, body) {
if (!error && response.statusCode == 200) {
  console.log("Greeting set successfully!");
} else {
  console.error("Failed calling Thread Reference API", response.statusCode,     response.statusMessage, body.error);
}
});  
}

Then I have another method just so that I keep code readable:

function setGreetingText() {
var greetingData = {
setting_type: "greeting",
greeting:{
text:"Hi {{user_first_name}}, welcome!"
}
};
createGreetingApi(greetingData);
}

Then you use this method in app.listen like this:

app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
setGreetingText();
});
iBobb
  • 1,140
  • 1
  • 14
  • 35
0

So i guess in messenger this happens for the very first message. If you delete message and start over this is NOT triggered. In Facebook messages you see it working.

Chandan Maruthi
  • 183
  • 1
  • 2
  • 9