FB Messenger Bot 🤖 — How to Identify a User via Page- & App-Scoped User-ID’s

Update 2017–04–17: Facebook finally has an API for that 🎉
https://developers.facebook.com/docs/messenger-platform/connecting-accounts

Philipp Holly
Chatbots Magazine

--

In my last blog post I wrote about our first chatbot and the challenge to identify users.

We get a page-scoped user-id from Messenger, so we can’t match existing users with their app-scoped user-id, even though we are using the same FB App for Facebook Login and Messenger Bot Integration.

(..) Ids are page-scoped. These ids differ from those returned from Facebook Login apps which are app-scoped. You must use ids retrieved from a Messenger integration for this page in order to function properly. — Source: Facebook Docs

In my test I had the following user-ids:
page-scoped user-id: 1026377564065xxx (through Messenger Bot)
app-scoped user-id: 10205652825102xxx (through FB App, Facebook Login)
These user-ids are totally different — so we can’t check if the user already exists in our database. We need a user identification to save all votes and swells. At least one unique identifier.

There is a little trick to match the page-/app-scoped user-id based on the profile picture url. Let me explain it in a few steps:

Facebook offers an API to get the userprofile with the Messenger user-id (page-scoped) and the page access-token:

GET https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=<PAGE_ACCESS_TOKEN>Result:
{
first_name: 'Philipp',
last_name: 'Holly',
profile_pic: 'https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13177091_10209330090191529_6260308789231765xx_n.jpg?oh=83704b11843eef0e2b590943532e3cxx&oe=57AA83xx',
...
}

Let’s check the Graph API Explorer to get the userprofile with the app access-token from the user.

GET https://graph.facebook.com/v2.6/me?fields=picture&access_token=<APP_USER_ACCESS_TOKEN>Result:
{
picture: {
data: {
is_silhouette: false,
url: 'https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/13177091_10209330090191529_6260308789231765xx_n.jpg?oh=6f15b6c4272b535bc4b5bf4e2193ccxx&oe=57E473xx'
}
},
id: '10205652825102xxx'
}

As you can see, the filename of the profile picture is identical: 13177091_10209330090191529_6260308789231765xx_n

Now we can match the Messenger user with the existing user (who signed up with facebook) in our database. If we dont find a matching user, we create a temporary user with the Messenger user-id and the profile picture filename. If this user signs up with Facebook, we can match the new user with the temporary user.

Attention: If a user changes his profile picture, the url will change too. So it’s impossible to match an old temporary user. 🙀

Hopefully Facebook will add a “matching api” for this case.

#swell yeah

Your’s Phil

đź‘Źđź‘ŹClap below to recommend this article to othersđź‘Źđź‘Ź

--

--