├── conf.js ├── .gitignore ├── package.json ├── LICENSE ├── README.md └── index.js /conf.js: -------------------------------------------------------------------------------- 1 | var conf = module.exports = { 2 | FB_MESSAGE_URL: 'https://graph.facebook.com/v2.6/me/messages', 3 | VERIFY_TOKEN: 'verify token here', 4 | PROFILE_TOKEN: 'page access token here', 5 | PORT: '3000' 6 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fb-messenger-echo-bot", 3 | "version": "0.0.1", 4 | "description": "Facebook messenger echo bot", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/sgaurav/fb-messenger-echo-bot.git" 12 | }, 13 | "author": "Gaurav Singh", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/sgaurav/fb-messenger-echo-bot/issues" 17 | }, 18 | "homepage": "https://github.com/sgaurav/fb-messenger-echo-bot#readme", 19 | "dependencies": { 20 | "body-parser": "~1.15.0", 21 | "compression": "~1.6.1", 22 | "express": "~4.13.4", 23 | "request": "~2.71.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Gaurav Singh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Facebook Messenger Echo Bot 2 | Facebook Launched Messenger API in F8 Conference. This opens up gate for many who want to build bots for Facebook Messenger. 3 | 4 | This is a simple echo bot built using the APIs. 5 | 6 | Setup 7 | --------------------------- 8 | - Setup a Nodejs client running on HTTPS. Refer to awesome Digital Ocean resource for same [here]('https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04') 9 | - Facebook does not play nice with self signed certificates. Buy one from a provider else you can generate one for free from LetsEncrypt. Refer to [this](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04) guide for same. 10 | - Follow steps mentioned at [Facebook Messenger API docs](https://developers.facebook.com/docs/messenger-platform/implementation) to create an app and a page required. 11 | - Generate page access token 12 | - Use token generated in previous step to make a cURL call and subscribe app to page. 13 | - Fill relevant details in `conf.js` file. 14 | - Start node server and complete the Webhook setup process. 15 | - You are ready to go, bot should respond back with exact string sent. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var request = require('request'); 2 | var http = require('http'); 3 | var express = require('express'); 4 | var bodyParser = require('body-parser'); 5 | var compression = require('compression'); 6 | 7 | var conf = require('./conf'); 8 | 9 | var app = express(); 10 | app.use(compression()); 11 | app.set('case sensitive routing', true); 12 | app.use(bodyParser.json()); 13 | 14 | var httpServer = http.createServer(app); 15 | 16 | app.get('/', function (req, res, next) { 17 | res.send('Welcome to Facebook Messenger Bot. This is root endpoint'); 18 | }); 19 | 20 | app.get('/webhook/', handleVerify); 21 | app.post('/webhook/', receiveMessage); 22 | 23 | function handleVerify(req, res, next){ 24 | if (req.query['hub.verify_token'] === conf.VERIFY_TOKEN) { 25 | return res.send(req.query['hub.challenge']); 26 | } 27 | res.send('Validation failed, Verify token mismatch'); 28 | } 29 | 30 | function receiveMessage(req, res, next){ 31 | var message_instances = req.body.entry[0].messaging; 32 | message_instances.forEach(function(instance){ 33 | var sender = instance.sender.id; 34 | if(instance.message && instance.message.text) { 35 | var msg_text = instance.message.text; 36 | sendMessage(sender, msg_text, true); 37 | } 38 | }); 39 | res.sendStatus(200); 40 | } 41 | 42 | function sendMessage(receiver, data, isText){ 43 | var payload = {}; 44 | payload = data; 45 | 46 | if(isText) { 47 | payload = { 48 | text: data 49 | } 50 | } 51 | 52 | request({ 53 | url: conf.FB_MESSAGE_URL, 54 | method: 'POST', 55 | qs: { 56 | access_token: conf.PROFILE_TOKEN 57 | }, 58 | json: { 59 | recipient: {id: receiver}, 60 | message: payload 61 | } 62 | }, function (error, response) { 63 | if(error) console.log('Error sending message: ', error); 64 | if(response.body.error) console.log('Error: ', response.body.error); 65 | }); 66 | } 67 | 68 | var port = conf.PORT; 69 | httpServer.listen(port, function () { 70 | console.log("Express http server listening on port " + port); 71 | }); --------------------------------------------------------------------------------