Building facebook chatbot using node.js

Posted in AngularJs By Raj On April 10, 2017


The world is changing a lot and companies are running behind automation, imagine if someone wants to know information about Biryanis which is a famous dish in India, if he/she types simple keywords in the facebook chatbot, if chatbot displays list of Biryanis without human intervention, answers simple questions and take orders in the bot itself, think about how much brand we are building for this small business player’s along with saving time and money, in the business world it is overhead for SMB’s to maintain a help desk’s, so, in this article, I will show you how to build a facebook messenger using node.js which shows list of Biryanis when someone enters “Biryanis” in the chatbot

here, I am going to build facebook chatbot by using node.js but, there are some free online tools using which you can build chatbots in minutes

As we are building facebook chatbot so, first we must create a facebook page and the facebook page is identity of your bot, whatever name and image gives will appear in facebook chat bot messenger when someone starts chatting

Once the page is ready then you need to build app for this, go to the app dashboard, and at the top right corner you will find “Add a new App” button just click on that to create a new app, a small dialog box will appear asking you to enter display name, email id and category once all these completed click on create app id button


In the next page, you will see new app id along with product setup items, the important things are “Webhooks” and “Messenger”


Click on “Get Started” button of “Messenger”, a new page will appear and scroll until you see “Token Generation” section, here a select a page which is created recently and copy the generated token to clipboard, we need to use this token to authenticate requests

Next, go to the webhooks section and fill up the callback URL, verify token and subscription fields

Callback URL: the callback url is the server url where the webhooks code is deployed, the SSL certificate must install for this and it should start with https

Verify Token: whatever value we authored here will be sent to your bot, to cross check whether the request is coming from facebook page or not, this should be copied to clipboard

Subscription Fields:  select the subscription fields to messages


Set up Node server

I have taken the sample code from the GitHub and setup node server, please check the below server,js code, when we run “npm start” command it will start node server by using 6000 port number

  • In the config/default.json file we need to author below entries, the appSecret you can get from the facebook app dashboard
  • The pageAccessToken is the once which is copied to clipboard while completing messenger section
  • The validationToken is the one which is copied to clipboard when we set up webhooks
  • The serverUrl is your webhook url


Mention the required dependencies in the package.json file and execute “npm install” command  to download dependencies

Once everything is completed then run the “npm start”command, then go to your facebook page and send a message, you should see the message back whatever you sent or just send “biryanis” it should show list of biryanis as a carousel format