To start building a bot, you need to install Nodejs on your computer. You may install nodejs from link below:
Let’s start of with building that would say hello on your input.
The front end API is provided by facebook which can be accessed through a facebook page. To accomplish this you’ll have to create a facebook page.
In order to have your bot communicate with the page, facebook provides a unique token which is to be included in your code. To generate a token, you will have to create a facebook app here. Once you are done creating the app, click on “Messenger” and select your page from the drop down which generates a random token. Have this token saved for later use in the code. Then click on “Add Products” and select webhooks.
Now that we have token ready, lets write the code say file name is server.js. Note that the above generated token needs to be replaced in the below code
Now that we are done with the code, its time to run our bot. To run the server, type node server.js.
Since, the facebook API communicates through secure tunnel, we need a secure server to establish the connection between facebook and our node server. Ngrok comes to the rescue in this case. Ngrok provides a secure tunnel which allows our server to connect to facebook API. Install and run Ngrok which generates a secure link. Copy the generated secure link(looks like https://localhost:XXX).
In the facebook Developers page, click on “Webhooks” and paste the above secure link in callback url field and enter the verify token from code in “Verify Token” section, click “Verify and save”. To activate the tunnel, click on “Messenger” and select your page and click on “Subscribe” to subscribe it.
Finally, we are done with setup and our bot is ready to talk. In the created facebook page, click on Message and type “Hi” and boom you’ll get the response.
This is a basic chat bot which simply sends text message. Similarly, you can use different templates available in Messenger platform to make your own bot.