Getting full name from facebook messenger

Hello everyone, I’m new to the forum and I started with rasa for a few days. I downloaded rasa stack and started to develop from there. I connected it to facebook via the credential.yml file and everything works perfectly. But I would like to be able to take automatically the full name of the interlocutor through facebook without having to ask, just like the other bots. Can you give me some advice or an example to follow? How can I interface the Graph Api with Rasa?

Hey @ElMandrillo,

You can achieve this by writing a custom action which calls fb graph api to retreive the sender name and set it as a slot. Let me explain this in detail:

  1. User opens the chat and sends a message ‘Hello’.
  2. Under the hood, the webhook gets a requests with a payload which looks similar to the following: {'sender': {'id': '2992338274125246'}, 'recipient': {'id': '427313918005852'}, 'timestamp': 1550239397425, 'message': {'mid': 'Hw1Mpy5uQeWv1iGmK1FmUFDnGKEoevzQJaC5wL0YaheUjX1lwgV1l2DAJjdN0Ph6PnSE0praoKM1GVrGWpc-Gg', 'seq': 117023, 'text': 'Hello'}}
  3. sender_id and message.text from this payload are extracted, and passed to the Rasa Core → added to tracker.
  4. Since sender_id is added to the tracker, you can write a custom action which can retrieve it, and find user’s name and surename by calling facebook graph api, and set them as slots for bot to use. An example custom action could look like this:
class GetName(Action):
	def name(self):
		return 'action_name'
		
	def run(self, dispatcher, tracker, domain):
		import requests
		
		most_recent_state = tracker.current_state()
		sender_id = most_recent_state['sender_id']
		
		r = requests.get('https://graph.facebook.com/{}?fields=first_name,last_name,profile_pic&access_token={}'.format(sender_id, fb_access_token)).json()
		first_name = r['first_name']
		last_name = r['last_name']
		
		return [SlotSet('name', first_name), SlotSet('surname', last_name)]
  1. To make sure that your bot extracts this info first thing when the conversation starts, design your training stories in a way that assistant would execute this custom action first. For example:
## Story
* greet
	- action_name
	- slot{"name":"Juste"}
	- slot{"surname":"Petraityte"}
	- utter_greet
  1. Last, but not least, make sure that the slots you are setting using the custom action are included in your domain. For example:
slots:
  name:
    type: text
  surname:
    type: text
  1. Here is nothings that stops you from using the extracted name and surname in the very first response the bot sends back to your user. In your domain, you can define utter_greet as:
templates:
  utter_greet:
    - 'Hello {name} {surname}! How can I help?'

Hope it helps :slight_smile:

6 Likes

Hi @Juste! thats cool… i am also trying to get the user name on rocketchat… is there a similar way to achieve this in rocketchat?

1 Like

Hi @Juste. I follow your reply to this post, but I get an error due to the endpoints : Encountered an exception while running action 'action_welcome_with_name'. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code.

I try many endpoints in endpoints.yml like this one: http://localhost:5005/webhooks

Can you help please ?

[solved]

@Juste i follow your instructions but i get error " Encountered an exception while running action ‘action_name’. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information."

how you solved it me too got that error

Insted of returning firstname and lastname to slots is okay to use it in this way>

r = requests.get(
                'https://graph.facebook.com/{}?fields=first_name,last_name,profile_pic&access_token={}'.format(
                    sender_id, access_token)).json()
            first_name = r['first_name']
            last_name = r['last_name']

dispatcher.utter_message("Hey " + first_name + " " + last_name +"!  I’m Rick")

@rishier827 If you don’t need these details being set as slots, then sure, it’s a valid approach. It really just depends on how you want to model your stories :slight_smile:

great thankz

The sender_id is not the Facebook user_id, how to retrieve user_id?

@Juste I also want to get username without asking but from my own website which also requires the user to login like facebook. Can you please help me to do that?

@devesh “requires the user to login like facebook”

Are you using the “Facebook Customer Chat Plugin” on your site?

If you are, the solution that Juste provide will work for you.

No, my website has nothing to do with facebook

Ok. So this isn’t really related to Facebook or Rasa.

You need to be able to do same kind api call to your own site/web app. How to do this? It all depends how you have build your site. Learning from google and youtube tutorials for example should be enough to build this.

@k1m Well, it is related to rasa because I am using rasa bot on my website. As of now, my website doesn’t have any api and I don’t know how to make one. But as you said, I will look on internet how to make api, Thanks.

Hello,

Does anybody know how to get the access_token from the credentials.yml?

Thanks in advance.

Without asking the user, how can a website know/extract name of user? Unless your website is linked to Facebook Messenger, Slack, Telegram etc.