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.

    1. 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.

    1. Drag and drop a new function node and an output link node onto the canvas.

     

      1. Wire the nodes together.

      1. 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;

      2. Click Done.

     

      1. Double-click the link out node. Name it Text 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 like 31523e2d.2777a2.

       

        1. Click Done.

       

        1. Click the OK Watson tab. Drag and drop a function node and an output link node onto the canvas.
        2. Double-click the new function node. Set the Outputs to 2.

         

          1. 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];

            }

         

          1. Click Done.
          2. Double-click the new link out node and name it Conversation Out for Messenger. Then, click Done.

           

            1. Wire your nodes together, breaking the previous link to the left of the text to speech node.

            1. Go to the Messenger tab and drag and drop an input link node onto the canvas.

             

              1. 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.

               

                1. Wire the nodes together.

                1. 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:

                1. Click Done and deploy your flow.

               

                1. Go to Facebook Messenger and try out your bot.

              You are now using the Watson Conversation service to drive your Facebook Messenger bot.