IBM Developer Day | Bengaluru | March 14th Register now
Tutorial
By M Fahad Minhas | Updated August 16, 2018 - Published January 15, 2018
Artificial IntelligenceMessagingNode.jsCloud
In this tutorial you will go through the steps involved in using Facebook’s Graph API to get a user’s username when interacting with a chatbot on a Facebook Page integrated with Watson Assistant. The extracted username will be used to set the context of the Watson Assistant so that the bot responds with the name of the user.
Before starting this guide you can refer to the Integrate Watson Converstion with Facebook Page using Node-RED to learn how to integrate the various pieces.
By following the steps in this tutorial you will learn how to:
To follow the steps in this tutorial, you need to have:
Assuming the prerequisites are completed prior to performing the steps in this tutorial, then going through it should take around 30 minutes.
It is assumed that you have completed the previous how-to, Integrate Watson Assistant with Facebook Page using Node-RED. There should be two flows in Node-RED that look like the images below:
We will work with the first one, specifically by making room to add new nodes. First, remove the link between the Listener node named Look for messages and the unnamed function node highlighted below:
Look for messages
Drag and drop a function node and name it Extract user ID and another node named http request, wire them together in the manner shown below:
Extract user ID
http request
First we configure the Extract user ID node to extract the Facebook user name, for convenience it is shown below:
var userid=msg.payload.entry[0].messaging[0].sender.id; msg.url='https://graph.facebook.com/v2.6/'+userid+'?fields=first_name,last_name,profile_pic&access_token=<PASTE_YOUR_TOKEN_HERE>'; msg.useridTest=userid; return msg;
In this code, we first extract the user ID from the entry array that we receive while listening to messages. Then, we are calling Facebook’s Graph API by setting msg.url, but we also add in the recently extracted user ID and our access token to the URL. This context will be sent to the http request node to call the API and get the response.
entry
msg.url
The only change we need to make to the http request node is to change the return type to a parsed JSON object, as shown below, perform this by double-clicking the node.
a parsed JSON object
Lastly, the code for the unnamed function that is receiving the http request response is shown below:
msg.fromMessenger = true; var myid=msg.payload.first_name; msg.payload = msg.messagingEvent.message.text; msg.params = {"context":{"usern": myid}}; return msg;
After making the HTTP request we can see the first name of the user in msg.payload, we save this value in a new variable called myId. We can add this variable to the msg.params object, specifically in the context array. Note that the usern variable is context that can now be used in Watson Assistant, see the image below for an example:
msg.payload
myId
msg.params
context
usern
Now when interacting with the Facebook page messenger we can have a custom greeting and proper responses when chatting with users!
In this guide, we learned how to use Facebook’s Graph API to extract the username and how to modify Node-RED to pass this information to the Watson Assistant service as a context variable.
Guide for integrating Watson Assistant workspace with the Facebook page using Node-RED.
API ManagementArtificial Intelligence+
Back to top