33

I am trying out the new Facebook Messenger Platform and have hit a bit of a problem.

When a user first chats with my Bot, I want to use the sender.id to lookup the user in my DB and verify whether they're a customer or not and offer a more tailored UX.

User's sign up to my service using Facebook Login, but unfortunately it appears my App's Facebook ID & my Bot's Facebook ID are different due to IDs being limited to App-scopes.

Is there any way associate the 2 IDs to allow me to find a user in my DB?

UPDATE (4/20/2016): We got around this by asking users on first contact via messenger to click a link to login to their account so we could associate their messenger_id with their account in our DB.

Would be awesome if facebook instead included PAGE_SCOPED IDs in the ids_for_business endpoint.

UPDATE: (6/1/2016): Facebook's latest update includes a new "Account Linking" functionality that appears to solve this issue. See https://developers.facebook.com/docs/messenger-platform/account-linking

Richard O'Brien
  • 429
  • 1
  • 5
  • 9
  • 2
    The best solution I could think of is to check the profile picture. It is not guaranteed that the user will not change, but it might give you some correct ones. – niry Apr 29 '16 at 00:03
  • 1
    @Richard O'Brien one question: How do you handle new users? Right know I am having the issue that I want to allow FB Messenger users to sign up for my service from with in an iFrame but it seems like FB Login SDK doesn't work in the Facebook iFrames of the Messenger. – roundrobin Oct 31 '16 at 19:45

5 Answers5

9

Facebook's latest update includes a new "Account Linking" functionality that appears to solve this issue. See https://developers.facebook.com/docs/messenger-platform/account-linking

Richard O'Brien
  • 429
  • 1
  • 5
  • 9
  • 2
    Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. From [How to Answer](http://stackoverflow.com/help/how-to-answer). – Gustavo Morales Jul 01 '16 at 02:55
  • can you can add some details about this process? – Jnewbie Apr 11 '18 at 15:54
3

Unfortunately, there's no way of doing that currently. Asking them to login for new threads is the best way of linking accounts.

pschang
  • 2,568
  • 3
  • 28
  • 23
  • I get this error: "Unsupported get request. Object with ID 'xxxxxxxxxxxxxxxx' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api – Diego Jan 27 '21 at 03:12
3

Yes. You can get details like first name, last name, profile pic url, locale, timezone, gender from an api. Only you have to pass is their recipient id/sender id and acccess_token of your messenger bot.

https://graph.facebook.com/v2.6/USER_ID?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=TOKEN
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122
2

I solved it different way but it works perfectly by graph API,i read mailbox of my page so that when any one can interact by messenger bot than i read pages inbox and iterate through,

Step 1:

Do a HTTP get request by graph API , you want a PAGE_UNIQUE_ID from Graph API ,

GET REQUEST

https://graph.facebook.com/v2.6/<PAGE_UNIQUE_ID>?fields=conversations.limit(10){participants,updated_time,id}&access_token=<PAGE_ACCESS_TOKEN>

PAGE_UNIQUE_ID Hints:

Go ===> https://developers.facebook.com/tools/explorer/ ==> Press "Get Token" ===> Select your desired page ===>Finally Submit , You show a id on output that is PAGE_UNIQUE_ID in here.

Check on browser for resoponse.

Step 2:

After doing above http request ,you get a JSON object that will show latest 10 conversation of pages where desired facebook id included.

Step 3:

Do iteration through by user full name or you can use lodash or underscore at your choice,

getUserID(response,'Zahid Rahman')


function getUserID(response, fullname) {
  if (response && response.conversations && response.conversations.data) { //Check response is correct
    var conversations = response.conversations.data;                       //Get all conversations
    for (var j = 0; j < conversations.length; j++) {
      var conversationsParticipants = conversations[j].participants.data; 
      for (var k = 0; k < conversationsParticipants.length; k++) {         //Get all particiapnts of a single conversation.

        var conversationsParticipantsEach = conversationsParticipants[k];
        if (conversationsParticipantsEach.name === fullname) {             //Check fullname match or not
          console.log("Desired Facebook User ID : " + conversationsParticipants[k].id);
          return conversationsParticipants[k].id;     
        } 
      }
    }
  }
}

Hope it will help you.

Zahidur Rahman
  • 1,688
  • 2
  • 20
  • 30
  • This required the permission "read_mailbox" which doesn't seems to be easy to get: "This permission is granted to apps building a Facebook-branded client on platforms where Facebook is not already available. For example, Android and iOS apps will not be approved for this permission. In addition, Web, Desktop, in-car and TV apps will not be granted this permission." – j0k Apr 03 '17 at 09:13
1

This is probably what you want: https://developers.facebook.com/docs/apps/for-business

It allows you to get all of the app-scoped user ids on a per-business basis.

QuotidianVoid
  • 609
  • 5
  • 12
  • Thanks. I didn't realise that existed... looks like it could work. I'll give it a try. – Richard O'Brien Apr 14 '16 at 20:56
  • 1
    Unfortunately doesn't look like this will work. Messenger doesn't appear in the list as a seperate app. :( I think I will need to get the user to click a link and login with Facebook, passing the messenger id in the querystring and then associating FB ID and Messenger ID post login in my callback. – Richard O'Brien Apr 16 '16 at 03:43
  • Are both of your apps owned by the same business on Facebook's website? – QuotidianVoid Apr 17 '16 at 19:28
  • What do you get as a response when you do https://graph.facebook.com//ids_for_business ? – QuotidianVoid Apr 17 '16 at 19:38
  • Only my app is returned when I hit that end-point. Looks like it's because `ids_for_business` only returns app ids and messenger is Page scoped, not app scoped. – Richard O'Brien Apr 18 '16 at 23:00
  • Isn't that what you want though? If the sender is already using your app, it will return the app-scoped ID of your app user so you can find him in your database. Or am I misunderstanding what you are trying to do? – QuotidianVoid Apr 19 '16 at 03:19
  • Not quite, when the user posts something to us in messenger we wanted to save it against their account. But on first use, we can't tell who the user is as we have their app-scoped id on file, so we need to explicitly ask them to click a link to login to our app so we can associate the messenger-scoped id with their existing account. Not sure f that makes sense but I think that's the only resolution here until facebook includes page_ids in `ids_for_business` end-point. Thx a lot for your help though. – Richard O'Brien Apr 20 '16 at 04:47
  • have you got any news on that subject? I have the exact same issue and the only way I found was to compare profile picture url which is clearly unsafe – Sebastien C. Jun 17 '16 at 05:46