Setting up a dev chatbot for Facebok and RedBot

This is a quick tutorial to setup a very simple chatbot with Facebook Messenger and RedBot.
First of all install Node-RED
sudo npm install -g node-red
Then open the user data directory $HOME/.node-red and install the package
cd $HOME/.node-red npm install node-red-contrib-chatbot
Then run node-red
.
Setting up a Facebook Messenger is quite tricky since Facebook API talks to Red-Node via a https callback (a self signed certificate is not enough). It also requires a verify token that makes the whole process non linear and confusing.
We'll use ngrok to create a https tunnel for our local Node-RED instance. Install it, then open a shell window and run
ngrok http 127.0.0.1:1088
Grab the https address you get, something like https://123123.ngrok.io, this is the base url of the Facebook callback

The callback is
http://youraddress.ngrok.io/redbot/facebook
Replace the hostname in bold with what you got above.
Start here to create a new Facebook app

Then click on "get started with Messenger". Your Facebook app must be connected to a Facebook page, select one from the drop down menu (or create one if you don't have any) and confirm, that will create a new access token

Copy this value as well as the app secret that you find in the "dashboard" section. Don't close this page yet and open Node-Red.
Add a new Facebook Receiver
node, then open the configuration of the node and fill up form with the access token and app secret you got before, then give a name to this configuration for your convenience and specify the verify token: this is an arbitrary string, you can put here whatever you like, just remember it you'll need in the next step.

Save your node and switch back to the Facebook App configuration page, click on "Setup webhooks"

Copy the callback url you got before and the arbitrary verify token you put in Node-RED, don't forget to check "messages" (otherwise your chatbot will never be notified of incoming messages).
Click on "Verify and Save", if you don't get any error means Facebook is able to contact your Node-RED instance and everything is ok.
If you get an error means that you have used wrong parameters (access token, app secret, etc) or, for some reason, Facebook APIs cannot reach the your callback url.
To exclude the second option and be sure that your Node-RED instance works correctly, grab your ngrok url and open this address
http://youraddress.ngrok.io/redbot/facebook/_status
If you get an "ok" then Node-RED is up and running.
The most is done, let's test if it works: connect a RedBot Debug node
to the Facebook Receiver node
, then open the Facebook page selected with the drop down (when the access token was generated), click on the "Message" button and type anything.
Something should be visible in the console. From now on everything is similar to what explained in the Telegram chat bot tutorial (most of the nodes in RedBot can be used for all platforms Telegram, Facebook, Slack so there's no need to create platform specific flows).
Happy chatboting!