Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

How to access Messenger User Profile? #544

Closed
ajith007 opened this issue Dec 10, 2016 · 10 comments
Closed

How to access Messenger User Profile? #544

ajith007 opened this issue Dec 10, 2016 · 10 comments

Comments

@ajith007
Copy link

What is the best way to retrieve the User Profile info in Messenger using Botkit?
https://developers.facebook.com/docs/messenger-platform/user-profile

I'm trying to greet the user by the FB profile first name.

@PierreMarieRiviere
Copy link

In the greeting thread, you can refer to the user's ID by using {{user_name}}

controller.api.thread_settings.greeting('Hello {{user_name}}! Please meet our Facebook chatbot!');

You can try {{user_first_name}} or {{user_last_name}} as well, or refer to any feature of the user's public profile. https://developers.facebook.com/docs/facebook-login/permissions

@ajith007
Copy link
Author

Thanks devhaoy for the quick response!

I tried your suggestion and it worked fine on the welcome/greeting/home page. In fact, I wanted to greet the user with the first name every time the user starts a conversation with the bot by saying hello or hi. Sorry, I wasn't clear earlier. I tried as below, but didn't work.

controller.hears(['hello','hi'], 'message_received', function(bot, message) {
		var msgAttachment = {
			'type':'template',
			'payload':{
				'template_type':'generic',
				'elements':[
					{
						'title':'Hi **{{user_first_name}}**!',
						'subtitle':'What would you like to do?',
						'buttons':[
							{
							  'type':'postback',
							  'title':'Read a Joke',
							  'payload':'joke'
							},
							{
							  'type':'postback',
							  'title':'Listen to Music',
							  'payload':'music'
							},
							{
							  'type':'postback',
							  'title':'Play a Game',
							  'payload':'game'
							}
						]
					}
				]
			}
		};

		bot.reply(message,{
		  attachment: msgAttachment 
		});
	});

@benbrown
Copy link
Contributor

This may be the built in template parser getting in the way of the Facebook tags, which use the same {{}} syntax. I'll do some testing.

@ajith007
Copy link
Author

Thanks Ben! Let me know the best way to retrieve the profile info.

@PierreMarieRiviere
Copy link

I don't think Botkit is the cause of the issue. Seems the {{user_name}} tag is only available within the greeting message.
My understanding is you must call to the Graph API to get the user's public profile in the conversation. Here is a sample code you can use in the convo:

var request = require('request'); // make http call

getUserName = function(response, convo) {
var usersPublicProfile = 'https://graph.facebook.com/v2.6/' + response.user + '?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=' + process.env.page_token;
request({
    url: usersPublicProfile,
    json: true // parse
}, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            convo.say('Hi ' + body.first_name);
        }
    });
};

This call grants access to the user's public profile only, depending on what you request in the url. Would be super nice if we had a native Login flow built in our Botkit :)

@ajith007
Copy link
Author

Great! That worked very well. Thanks devhaoy! 👍

Yes, would have been nice if we could retrieve the public profile info without making the http call to Graph API. :)

@peterswimm
Copy link
Contributor

Marking this as a enhancement request for Botkit, if anyone wants to take a stab as at PR.

@znat
Copy link

znat commented Jan 12, 2017

I wrote this middleware to populate a user_profile field with the facebook profile in inbound the message.

@pibicalderon
Copy link

pibicalderon commented Feb 6, 2017

Do I have to make the http call each time I want to use the user information in a chatbot in the same conversation?

@znat
Copy link

znat commented Feb 8, 2017

@pibicalderon it saves the user profile to the storage to avoid making several API calls

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants