Get a full name in messenger

hello rasa friends i have a issue about facebook messenger, well i tried to get a facebook full name with the request methon

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)]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)]`

but when the robot responds the server omit the action, the error what appear it´s

https://graph.facebook.com/{}?fields=first_name,last_name,profile_pic&access_token={}’.format(sender_id, fb_access_token)).json() NameError: name ‘fb_access_token’ is not defined

so it any person that has appeared that incovenient i really appreciate the help

regards :wink: :

Hi @Robert14! The problem is that your variable fb_access_token is not defined anywhere as mentioned in the error message. The problem is that you define the access token in the credentials file which is not available in the action server (as it is in the core server). You need to define this access token somewhere. My recommendation would be to add it as an environment variable.

You could then do something like:

FB_ACCESS_TOKEN` = os.getenv("FB_ACCESS_TOKEN")

...

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()

You could also somehow copy the credentials.yml file to your action servers and read them in your action.

Hope that helps! Nicolas

i really thing the fb_access_token are global thank for you reply the issue it´s solved

regards :wink:

Could you share how you solved it? It might help others having the same issue :slightly_smiling_face:

of course:`

    import requests
    fb_access_token  = ("")
    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&access_token={}'.format(sender_id, fb_access_token)).json()
    
    
    first_name = r["first_name"]
    last_name = r["last_name"]
    dispatcher.utter_message("hola {} {},bienvenido a nombre de pizzeria seleccione los botones de abajo para interactuar con nosotros' ".format(first_name, last_name)) r = requests.get('https://graph.facebook.com/{}?fields=first_name,last_name&access_token={}'.format(sender_id, fb_access_token)).json()
1 Like

Hi, I’m currently also looking for a way for the chatbot to get the username from the messenger. But I still can’t do it successfully. If yes, can you give me the source of this part? thank you very much