Create Simple Facebook 🤖 ChatBot 🤖 Messenger using Nodejs on Heroku
-
Install the Heroku toolbelt from here https://toolbelt.heroku.com to launch, stop and monitor instances. Sign up for free at https://www.heroku.com if you don't have an account yet.
-
Install Node from here https://nodejs.org, this will be the server environment. Then open up Terminal or Command Line Prompt and make sure you've got the very most recent version of npm by installing it again:
sudo npm install npm -g
-
Create a new folder somewhere and let's create a new Node project. Hit Enter to accept the defaults.
npm init
-
Install the additional Node dependencies. Express is for the server, request is for sending out messages and body-parser is to process messages.
npm install express request body-parser --save
-
Create an index.js file in the folder and copy this into it. We will start by authenticating the bot.
var express = require('express') var bodyParser = require('body-parser') var request = require('request') var app = express() app.set('port', (process.env.PORT || 5000)) // Process application/x-www-form-urlencoded app.use(bodyParser.urlencoded({extended: false})) // Process application/json app.use(bodyParser.json()) // Index route app.get('/', function (req, res) { res.send('Hello world, I am a chat bot') }) // for Facebook verification app.get('/webhook/', function (req, res) { if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') { res.send(req.query['hub.challenge']) } res.send('Error, wrong token') }) // Spin up the server app.listen(app.get('port'), function() { console.log('running on port', app.get('port')) })