Taxonomy Icon

Artificial Intelligence

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.

Learning objectives

By following the steps in this tutorial you will learn how to:

  • Set the URL of the HTTP request node from a function.
  • Make API calls from within Node-RED environment.
  • Make the HTTP request node return the result in a parsed JSON.
  • Access the JSON for the relevant information.
  • Set params of Watson Assistant Node from Node-RED function.

Prerequisites

To follow the steps in this tutorial, you need to have:

Estimated time

Assuming the prerequisites are completed prior to performing the steps in this tutorial, then going through it should take around 30 minutes.

Steps

Making room for new nodes

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:

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:

Configuring new nodes

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.

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.

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:

Now when interacting with the Facebook page messenger we can have a custom greeting and proper responses when chatting with users!

Summary

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.