├── chatbot_for_slack ├── Procfile ├── .gitignore ├── package.json └── bot.js ├── chatbot_for_facebook ├── Procfile ├── .gitignore ├── package.json └── bot.js ├── .gitignore ├── LICENSE └── README.md /chatbot_for_slack/Procfile: -------------------------------------------------------------------------------- 1 | web: node bot.js -------------------------------------------------------------------------------- /chatbot_for_facebook/Procfile: -------------------------------------------------------------------------------- 1 | web: node bot.js -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /chatbot_for_slack/.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /chatbot_for_facebook/.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /chatbot_for_slack/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatbot_for_slack", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bot.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "botkit": "^0.2.0" 13 | }, 14 | "engines": { 15 | "node": "5.10.1" 16 | } 17 | } -------------------------------------------------------------------------------- /chatbot_for_facebook/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatbot_for_facebook", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bot.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "botkit": "^0.2.0" 13 | }, 14 | "engines": { 15 | "node": "5.10.1" 16 | } 17 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Dominik Pich 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 | -------------------------------------------------------------------------------- /chatbot_for_slack/bot.js: -------------------------------------------------------------------------------- 1 | //=== 2 | //SLACK dummy bot for botkit 3 | //=== 4 | 5 | //test environment 6 | if (!process.env.SLACK_TOKEN) { 7 | console.log('Error: Specify SLACK_TOKEN in environment'); 8 | process.exit(1); 9 | } 10 | 11 | //get BotKit to spawn bot 12 | var Botkit = require('botkit'); 13 | var controller = Botkit.slackbot({ 14 | debug: false 15 | }); 16 | var bot = controller.spawn({ 17 | token: process.env.SLACK_TOKEN 18 | }); 19 | 20 | // start Slack RTM 21 | bot.startRTM(function(err,bot,payload) { 22 | // handle errors... 23 | }); 24 | 25 | //prepare the webhook 26 | controller.setupWebserver(process.env.PORT || 3001, function(err, webserver) { 27 | controller.createWebhookEndpoints(webserver, bot, function() { 28 | // handle errors... 29 | }); 30 | }); 31 | 32 | //=== 33 | //bot commands 34 | //=== 35 | 36 | controller.hears(['hello','hi'],['direct_message','direct_mention','mention'],function(bot,message) { 37 | bot.reply(message,"Hello."); 38 | }); 39 | 40 | 41 | controller.on('slash_command',function(bot,message) { 42 | // reply to slash command 43 | bot.replyPublic(message,'Everyone can see the results of this slash command'); 44 | }); 45 | -------------------------------------------------------------------------------- /chatbot_for_facebook/bot.js: -------------------------------------------------------------------------------- 1 | //=== 2 | //FACEBOOK dummy bot for botkit 3 | //=== 4 | 5 | //test environment 6 | if (!process.env.PAGE_TOKEN) { 7 | console.log('Error: Specify PAGE_TOKEN in environment'); 8 | process.exit(1); 9 | } 10 | 11 | if (!process.env.VERIFY_TOKEN) { 12 | console.log('Error: Specify VERIFY_TOKEN in environment'); 13 | process.exit(1); 14 | } 15 | 16 | //get BotKit to spawn bot 17 | var Botkit = require('botkit'); 18 | var controller = Botkit.facebookbot({ 19 | debug: false, 20 | access_token: process.env.PAGE_TOKEN, 21 | verify_token: process.env.VERIFY_TOKEN, 22 | }); 23 | var bot = controller.spawn({ 24 | }); 25 | 26 | //prepare the webhook 27 | controller.setupWebserver(process.env.PORT || 3000, function(err, webserver) { 28 | controller.createWebhookEndpoints(webserver, bot, function() { 29 | // handle errors... 30 | }); 31 | }); 32 | 33 | //=== 34 | //bot commands 35 | //=== 36 | 37 | controller.hears(['hello', 'hi'], 'message_received', function(bot, message) { 38 | controller.storage.users.get(message.user, function(err, user) { 39 | if (user && user.name) { 40 | bot.reply(message, 'Hello ' + user.name + '!!'); 41 | } else { 42 | bot.reply(message, 'Hello.'); 43 | } 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #SIMPLE facebook or slack bot using node.js + botkit + heroku 2 | 3 | Today I wanted to set up a facebook bot and a slack bot. botkit appealed to me because it provides a more or less generic wrapper around each vendor's specific api. :) 4 | 5 | This seemed a good idea, and I think it still is, because botkit rather nicely abstracts the grunt-work away. You can also easily use it with wit.ai and deploying it on heroku is also a breeze. 6 | 7 | The only 'downside' to BotKit is its documentation IMHO. Don't get me wrong ! https://howdy.ai provides lot of detailed documentation, a lot of samples. There is also an active community.
8 | But for me a really short **getting started guide** would have been good. 9 | 10 | ##Intro 11 | So Ill provide my simple 'hello world'-style chatbots for slack and facebook, summarizing what I 'learned' today :) 12 | 13 | Ill provide two simple checklists of how to get each bot up and running. Both guides are 90% equal but Ill not merge the lists to keep it simple! 14 | 15 | *This guide does not aim to replace any of the detailed documentation available at https://howdy.ai* 16 | 17 | - **[Chatbot for Facebook](#fb)** 18 | - **[Chatbot for Slack](#slack)** 19 | 20 | ##Chatbot for Facebook 21 | 22 | ###BEFORE 23 | Before we begin to develop our bot. we need to make sure we have all required dependencies 24 | 25 | 1. we need to make sure we got `node`, `npm`, `git` and `heroku` installed. I wont detail setting this up. www.heroku.com's getting started guide details this quite nicely :) 26 | 27 | ###DEVELOP 28 | Now set up the bot 29 | 30 | 1. download this repo and copy the folder 'chatbot_for_facebook' -- this folder will be the starting point for your new repository! 31 | 32 | 2. change into the new folder (`$ cd chatbot_for_facebook`) 33 | 34 | 3. run `$ git init` to make the repository valid. I assume this output: 35 | 36 | Untracked files: 37 | (use "git add ..." to include in what will be committed) 38 | 39 | .gitignore 40 | Procfile 41 | bot.js 42 | package.json 43 | 44 | 4. run `$ npm install` to fetch the packages. (This is only required so you can locally run your bot before pushing it to heroku. I added the resulting nodes_modules folder to the .gitignore file. **you don't want to publish the dependencies!**) 45 | 46 | 5. run `$ heroku create` to create a new heroku app and connect it with your repo. write down the output so you know the hostname! E.g.: 47 | 48 | Creating app... done, ⬢ polar-earth-39642 49 | https://polar-earth-39642.herokuapp.com/ | https://git.heroku.com/polar-earth-39642.git 50 | 51 | In this case the URL that botkit will setup for your bot is: 52 | 53 | https://polar-earth-39642.herokuapp.com/facebook/receive/ 54 | 55 | 6. Visit Facebook in your browser to connect your bot with the platform. 56 | 57 | You will need your own app, your own page and then get a page access token, provide a verify token and setup a incoming webhook. (the url for you bot)
58 | The whole process is explained nicley on the fb developer help websites. 59 | 60 | 61 | Follow the process: https://developers.facebook.com/docs/messenger-platform/implementation 62 | 63 | All you need is `the "setup" section and I wont go into too much detail but in the end you should have a long cryptic token and a human readable verify token. You should also have had to enter the URL you got. **The process is straight forward BUT not really that intuitive, so pay attention to the docs** 64 | 65 | 7. now create a `.env` file (`$ touch .env`) so you have a local config where you can store your PAGE_TOKEN and VERIFY_TOKEN. (**I added this file to the .gitignore file. you don't want to publish your token!**) 66 | 67 | sample environment: 68 | 69 | PAGE_TOKEN=ASDASDHE64BAITtHJkD2ZB0zTYZAHkRC7OpvAqJdzAxGQgfdoQtz40APfTuKxXbusiRviqUfMAzSanlXjUu4ZACzam06lTtZAHa754OjkjXxKAu8C2yhDt4jTN08zD5SJZANg4uT59zjvQP1ZBS11mZChU4Gqnhl9ZAembIW3xdZAgZDZD 70 | VERIFY_TOKEN=my_personal_secret 71 | 72 | 8. As a test of the configuration so far, execute `heroku local` (stop with ctrl+c). You should get: 73 | 74 | [OKAY] Loaded ENV .env File as KEY=VALUE Format 75 | 12:55:49 AM web.1 | info: ** No persistent storage method specified! Data may be lost when process shuts down. 76 | 12:55:49 AM web.1 | info: ** Starting webserver on port 5000 77 | 12:55:49 AM web.1 | info: ** Serving webhook endpoints for Slash commands and outgoing webhooks at: http://MY_HOST:5000/facebook/receive 78 | 79 | 9. the .env file is NOT used by heroku (they don't want you to commit it either ;)) so we have to set our 2 configuration variables separately by running 2 commands 80 | 81 | $ heroku config:set PAGE_TOKEN=ASDASDHE64BAITtHJkD2ZB0zTYZAHkRC7OpvAqJdzAxGQgfdoQtz40APfTuKxXbusiRviqUfMAzSanlXjUu4ZACzam06lTtZAHa754OjkjXxKAu8C2yhDt4jTN08zD5SJZANg4uT59zjvQP1ZBS11mZChU4Gqnhl9ZAembIW3xdZAgZDZD 82 | $ heroku config:set VERIFY_TOKEN=my_personal_secret 83 | 84 | 10. if the bot has no more issues, it is time to deploy all for the first time 85 | 86 | $ git add . 87 | $ git commit -am "initial commit" 88 | $ git push heroku master 89 | 90 | 11. start the new web app we deployed to heroku with `heroku ps:scale web=1` 91 | 92 | **DONE** 93 | 94 | ###AFTERWARDS 95 | go to your facebook page for which the bot is configured (for me it is e.g. My-Wine-Test) and try messaging the bot. Send it a simple hello. it should answer you with hello :) 96 | 97 | if the bot doesn't answer, it is most likely a configuration issue. Make sure your tokens are correct and the webhook URL is set correctly! 98 | 99 | All further modifications can now be done by editing bot.js and pushing the changes to heroku :) 100 |
101 |
102 |
103 | 104 | ##Chatbot for Slack 105 | 106 | ###BEFORE 107 | Before we begin to develop our bot. we need to make sure we have all required dependencies 108 | 109 | 1. we need to make sure we got `node`, `npm`, `git` and `heroku` installed. I wont detail setting this up. www.heroku.com's getting started guide details this quite nicely :) 110 | 111 | ###DEVELOP 112 | Now set up the bot 113 | 114 | 1. download this repo and copy the folder 'chatbot_for_slack' -- this folder will be the starting point for your new repository! 115 | 116 | 2. change into the new folder (`$ cd chatbot_for_slack`) 117 | 118 | 3. run `$ git init` to make the repository valid 119 | 120 | Untracked files: 121 | (use "git add ..." to include in what will be committed) 122 | 123 | .gitignore 124 | Procfile 125 | bot.js 126 | package.json 127 | 128 | 4. run `$ npm install` to fetch the packages. (This is only required so you can locally run your bot before pushing it to heroku. I added the resulting nodes_modules folder to the .gitignore file. **you don't want to publish the dependencies!**) 129 | 130 | 5. run `$ heroku create` to create a new heroku app and connect it with your repo. write down the output so you know the hostname! E.g.: 131 | 132 | Creating app... done, ⬢ polar-earth-39642 133 | https://polar-earth-39642.herokuapp.com/ | https://git.heroku.com/polar-earth-39642.git 134 | 135 | In this case the URL that botkit will setup for your bot is: 136 | 137 | https://polar-earth-39642.herokuapp.com/slack/receive/ 138 | 139 | 6. Visit Slack in your browser to connect your bot with the platform. 140 | 141 | For a pure bot, you don't need a webserver. Slack's Real-Time-Messaging API doesn't work with http. Your bot connects to Slack and opens a connection by itself. You need to register it though so you get a TOKEN that authenticates the bot. 142 | 143 | Read more about the RTM here: https://api.slack.com/bot-users 144 | 145 | BUT slack also has these nice slash commands... e.g. /remind or /help or so and they are usually answered by a Bot. I decided I want to have that and so my template also handles a slash command! 146 | 147 | See this page how to set it up: https://api.slack.com/slash-commands 148 | 149 | I wont go into too much detail but in the end you should have a cryptic token. You should also have had to enter the URL you got. **The process is straight forward BUT not really that intuitive, so pay attention to the docs** 150 | 151 | 7. now create a `.env` file (`$ touch .env`) so you have a local config where you can store your SLACK_TOKEN. (**I added this file to the .gitignore file. you don't want to publish your token!**) 152 | 153 | sample environment: 154 | 155 | SLACK_TOKEN=N08zD5SJZANg4uT59zjvQP1ChU4Gqnhl9ZAembIW3xdZAgZDZD 156 | 157 | 8. As a test of the configuration so far, execute `heroku local` (stop with ctrl+c). You should get: 158 | 159 | [OKAY] Loaded ENV .env File as KEY=VALUE Format 160 | 12:55:49 AM web.1 | info: ** No persistent storage method specified! Data may be lost when process shuts down. 161 | 12:55:49 AM web.1 | info: ** Starting webserver on port 5000 162 | 12:55:49 AM web.1 | info: ** Serving webhook endpoints for Slash commands and outgoing webhooks at: http://MY_HOST:5000/slack/receive 163 | 164 | 9. the .env file is NOT used by heroku (they don't want you to commit it either ;)) so we have to set our configuration variable separately by running this command 165 | 166 | $ heroku config:set SLACK_TOKEN=N08zD5SJZANg4uT59zjvQP1ChU4Gqnhl9ZAembIW3xdZAgZDZD 167 | 168 | 10. if the bot has no more issues, it is time to deploy all for the first time 169 | 170 | $ git add . 171 | $ git commit -am "initial commit" 172 | $ git push heroku master 173 | 174 | 11. start the new web app we deployed to heroku with `heroku ps:scale web=1` 175 | 176 | **DONE** 177 | 178 | ###AFTERWARDS 179 | go to your slack team's chat for which the bot is configured (I invited the @testbot into our team's #general channel for example) and try messaging the bot. Send it a simple hello. it should answer you with hello :)
180 | Also try the slash command and type /hello. The bot should say hello to everybody and send you a direct message! 181 | 182 | if the bot doesn't answer, it is most likely a configuration issue. Make sure your token is correct and the webhook URL is set correctly! 183 | 184 | All further modifications can now be done by editing bot.js and pushing the changes to heroku :) 185 | --------------------------------------------------------------------------------