Connect the Watson Conversation service to the Messenger bot
You now have all the working components to connect the Watson Conversation service to Facebook Messenger.
-
- In the Node-RED flow editor, delete the connections between the listener and writer components. You will redirect the connections to the conversation node in between.
-
- Drag and drop a new function node and an output link node onto the canvas.
-
- Wire the nodes together.
-
- Edit the new function node with the following code, which will set the text for the conversation service and also signal that the text came from Messenger.
msg.fromMessenger = true;
msg.payload = msg.messagingEvent.message.text;
return msg;
- Click Done.
- Edit the new function node with the following code, which will set the text for the conversation service and also signal that the text came from Messenger.
-
- Double-click the link out node. Name it T
ext from Messenger
and connect it to the unnamed link input node on the OK Watson page by selecting it from the list. It might be named something like31523e2d.2777a2
.
- Double-click the link out node. Name it T
-
- Click Done.
-
- Click the OK Watson tab. Drag and drop a function node and an output link node onto the canvas.
- Double-click the new function node. Set the Outputs to 2.
-
- Add the following code, which sends a message to both outputs if the input came from Messenger. This allows you to continue to hear the Conversation service responses in Node-RED and see them on Messenger.
if (msg.fromMessenger) {
return [msg,msg];
} else {
return [msg, null];
}
- Add the following code, which sends a message to both outputs if the input came from Messenger. This allows you to continue to hear the Conversation service responses in Node-RED and see them on Messenger.
-
- Click Done.
- Double-click the new link out node and name it
Conversation Out for Messenger
. Then, click Done.
-
- Wire your nodes together, breaking the previous link to the left of the text to speech node.
-
- Go to the Messenger tab and drag and drop an input link node onto the canvas.
-
- Double-click the new link node. Name it
Writer Output
and link it to the Conversation Out for Messenger from the OK Watson flow. Then, click Done.
- Double-click the new link node. Name it
-
- Wire the nodes together.
-
- Double-click the Set up response function node. Remove the following hardcoded line:
msg.payload = 'Hello, this is all I say for now.';
The function code should now look like this:
- Double-click the Set up response function node. Remove the following hardcoded line:
-
- Click Done and deploy your flow.
-
- Go to Facebook Messenger and try out your bot.
You are now using the Watson Conversation service to drive your Facebook Messenger bot.