Create a Facebook Chatbot

24

It’s not hard at all to make your own chatbot. Adding AI is a slightly different story, but for now we want to get the basic mechanics of a chatbot for Facebook’s Messenger going.

Make a native Facebook Chatbot in 20 minutes. We’ll write some server-side code, host it on Heroku, and then connect it to Facebook.

As usual, the brilliant folks at Katacoda have a playground available where you can test this code on a live Node.js server. Visit https://www.katacoda.com/courses/nodejs/playground and paste the code below to get started with your 1st Facebook chatbot. Let us know in the comments how it went.

katacoda-for-node-js.jpg


'use strict'

const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')

const app = express()

app.set('port', (process.env.PORT || 5000))

// Allows us to process the data
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

// ROUTES

app.get('/', function(req, res) {
	res.send("Hi I am a chatbot")
})

let token = ""

// Facebook 

app.get('/webhook/', function(req, res) {
	if (req.query['hub.verify_token'] === "blondiebytes") {
		res.send(req.query['hub.challenge'])
	}
	res.send("Wrong token")
})

app.post('/webhook/', function(req, res) {
	let messaging_events = req.body.entry[0].messaging
	for (let i = 0; i 

Here are the links to anything you might need to pull this off:

Download NPM/Node.js: https://nodejs.org/en/download
Download Sublime: https://www.sublimetext.com/download
Sign up for GitHub: https://github.com
Download Git: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Sign up for Heroku: https://www.heroku.com
Download Heroku Command Line Tools: https://devcenter.heroku.com/articles/heroku-cli
Curl: curl -X POST “https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=PAGE_ACCESS_TOKEN”
Code: https://github.com/blondiebits/code-in-5