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:
- User opens the chat and sends a message ‘Hello’.
- 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'}}
-
sender_id
andmessage.text
from this payload are extracted, and passed to the Rasa Core → added to tracker. - 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)]
- 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
- 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
- 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