Create a Facebook Chatbot

Create a Facebook Chatbot

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.


'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 < messaging_events.length; i++) {
		let event = messaging_events[i]
		let sender = event.sender.id
		if (event.message && event.message.text) {
			let text = event.message.text
			sendText(sender, "Text echo: " + text.substring(0, 100))
		}
	}
	res.sendStatus(200)
})

function sendText(sender, text) {
	let messageData = {text: text}
	request({
		url: "https://graph.facebook.com/v2.6/me/messages",
		qs : {access_token: token},
		method: "POST",
		json: {
			recipient: {id: sender},
			message : messageData,
		}
	}, function(error, response, body) {
		if (error) {
			console.log("sending error")
		} else if (response.body.error) {
			console.log("response body error")
		}
	})
}

app.listen(app.get('port'), function() {
	console.log("running: port")
})

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

*Create a Facebook Chatbot*
https://www.artificial-intelligence.blog/education/create-a-facebook-chatbot

Learn how to create your own Facebook Messenger chatbot with video tutorials, code samples ... the easy way.


#ai #ArtificialIntelligence
Deep Learning 101

Deep Learning 101

Hello World - Machine Learning Recipes #1

Hello World - Machine Learning Recipes #1

You are invited to help this AI blog become better - please comment, share, like, etc. to make it more widely known.

0