Create Node-RED webhooks

You are now ready to create the webhooks that will allow your Node-RED applications to listen into the chats on Facebook Messenger.

    1. Find the Webhooks section on the Facebook Developer page.

    1. Click Setup Webhooks.
    2. Complete the new page subscription:
      • Enter a Callback URL, which will be the URL for your Node-RED instance with any entry point that you want. For example, enter:
        <your_Node-RED_instance_URL>/mybot
      • Enter your Verify Token.
      • Select the messages check box under Subscription fields.
      • Click Verify and Save.


      You will see a callback verification error. This is because you haven’t yet created your /mybot callback.

    1. Go to your node-RED instance in the flow editor.

    1. Create a new tab and name it something like Messenger Webhooks.

    1. Drag and drop the following nodes onto the canvas:
      • Input http node
      • Output http response node
      • Function node
      • Output debug node

 

    1. Wire the nodes together: wire the debug node (msg.payload) to the input http node and to the function node and then to the http response

    1. Double-click the http input node. Set the method to GET and set the URL to the URL that you registered as the webhook for Facebook Messenger, such as /mybot. Name it Messenger Verification Webhook. Then, click Done.

    1. Double-click the function Name it GET Subscribe and then enter the following JavaScript. Remember to substitute the token‘ThisIsMyPersonalVerificationTokenThatOnlyIKnowAbout’ (shown in green) with your Verify Token.

      This script verifies that the request coming is a Verify Token request. The script checks that the token sent is what it is expecting. The script then sets msg.payload to the challenge sent by Facebook Messenger to verify that the subscription was accepted.

var mode = '';
var vtoken = '';
var challenge = '';
if (msg.payload['hub.mode'])
{
mode = msg.payload['hub.mode'];
}
if (msg.payload['hub.verify_token'])
{
vtoken = msg.payload['hub.verify_token'];
}
if (msg.payload['hub.challenge'])
{
challenge = msg.payload['hub.challenge'];
}
if ('subscribe' == mode &&
'ThisIsMyPersonalVerificationTokenThatOnlyIKnowAbout' == vtoken) {
msg.payload = challenge;
}

return msg;

    1. Click Done and deploy your flow.
    2. Go back to the Facebook developers page and click Verify and Save.

      The webhook should now be registered.

  1. Click Select a Page. Then, click Subscribe.

    Your application should now be subscribed to the Messenger events on your new page.