Creating Your First Slack Bot
In my opinion, Slack has taken over the world. Several companies are now using Slack for their internal communication.
The amazing thing about Slack is that you can actually program bots to automate several tasks for your development team. From twitterbot to doorbot to typingspeedbot to virtually insane ideas you can think about. To be honest, Slack has done a really genius work.
Today, I’ll show you how to create a very simple slack bot that returns a developer’s info.
Let’s get started.
You can also follow this link to get started. There is a comprehensive ReadMe there.
On another note, you can head over and just clone this repo to get started.
Virtually all hubot scripts are written in coffeescript but you could implement your own logic in vanilla javascript too.
In this example, I used a combination of javascript and coffeescript.
1. First Clone the repo
2. Open example.coffee. Now this is the script that loads every other script that provides functionalities
example.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# My Bot # Crafted by Myself # Integrated with Slack info = require './info' help = require './help' module.exports = (robot) -> robot.respond /hi|hello|sup/i, (msg) -> response = "Hello friend :grin: Welcome to MyBot Enter `help` to know more about how to operate MyBot" msg.send response robot.respond /whoami (.*)/i, (msg) -> name = escape(msg.match[1]) response = info[name] if response == undefined response = "We don't Know you. Please gerrahia!" msg.send response #Spills out custome typingspeed bot help commands robot.respond /help/i, (msg) -> emit = help.join "n" msg.send emit |
At the top I required the info and help script namely info.js and help.coffee respectively.
info.js just contains the data our bot app will fetch
info.js
1 2 3 4 5 |
module.exports = { 'oduye' : 'Oduye Oluwayemisi. I am a Laravel Evangelist. I am crazy', 'prosper' : 'Prosper Otemuyiwa . I am a developer with a difference. I am a PHP Evangelist', 'goodheads': 'Goodheads . I am a community of lovely developers . I am Goodheads' }; |
help.coffee
1 2 3 |
module.exports = [ '`hi|hello|sup` - Welcomes you to MyBot with some instructions' ] |
The script starts with module.exports = (robot) ->
This just signifies you are exporting the functionalities within this anonymous function and we are passing an instance of robot which is the bot
1 2 3 4 |
robot.respond /hi|hello|sup/i, (msg) -> response = "Hello friend :grin: Welcome to MyBot Enter `help` to know more about how to operate MyBot" msg.send response |
robot.respond listens to commands a slack user enters when interacting with the bot. The argument hi|hello|sup are just words that the bot should respond to if entered.
msg.send sends responses back to Slack for the user to see.
1 2 3 4 5 6 |
robot.respond /whoami (.*)/i, (msg) -> name = escape(msg.match[1]) response = info[name] if response == undefined response = "We don't Know you. Please gerrahia!" msg.send response |
/whoami (.*)/i takes care of this scenario:
e.g whoami prosper, whoami prosper otemuyiwa
So the slack user can pass any number of words to the bot to respond to.
name = escape(msg.match[1])
this simply catches the first argument. In the example above whoami prosper , it will get prosper
So you can catch any number of arguments by specifying the index. msg.match[2] will catch otemuyiwa in whoami prosper otemuyiwa
So it passes the value/word from the Slack user to info object and returns the right value.
Remember info is an object coming from info.js. We required it at the top of our file.
If the name doesn’t exist, it returns “We don’t know you. Please gerrahia!”.
1 2 3 4 |
#Spills out custome typingspeed bot help commands robot.respond /help/i, (msg) -> emit = help.join "n" msg.send emit |
This returns the array in help.js as a string and outputs it to the Slack user
That explains it. Let’s Just Modify it.
New example.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# DevInfo Bot # Crafted by Prosper # Integrated with Slack _ = require 'lodash' info = require './info' help = require './help' module.exports = (robot) -> robot.respond /hi|hello|sup/i, (msg) -> response = "Hello friend :grin: Welcome to DevInfo bot Enter `help` to know more about how to operate devInfoBot" msg.send response robot.respond /all/i, (msg) -> _.forEach info, (value, key) -> msg.send "#{key} -> #{value} n" robot.respond /get developer (.*)/i, (msg) -> devName = escape(msg.match[1]) response = info[devName] if response == undefined response = "We don't Know you. Please gerrahia!" msg.send response #Spills out custome typingspeed bot help commands robot.respond /help/i, (msg) -> emit = help.join "n" msg.send emit |
new help.coffee
1 2 3 4 |
module.exports = [ '`hi|hello` - Welcomes you to devInfoBot with some instructions', '`get developer <dev-name>` - Displays a particular developers info' ] |
You can check out how it works on your terminal by running bin/hubot
like so
Great!
Time to get this thing working on your Slack team!
1. Make sure you have heroku toolbelt installed.
2. Login via heroku on your terminal
3. Create a heroku app by running heroku create <app-name>
like so
4. Now head over to your heroku dashboard. Click on the app
Click on Connect to Github
and connect your github repo where the app resides.
Now before you click on deploy. Head over to your slack team and integrate Hubot.
Once you add that, it adds a member to your slack team which is the BOT named whatever you gave it when integrating. It will also redirect you to a page where you will see this
1 |
HUBOT_SLACK_TOKEN=xxxxxxxxxxxxxxx |
now xxxxxxxx… refers to your slack token
Now let’s add our heroku config
Go back to your terminal and run:
1 |
heroku config:add HEROKU_URL=https://my-company-slackbot.herokuapp.com |
and
1 |
heroku config:add HUBOT_SLACK_TOKEN=xoxb-1234-5678-91011-00e4dd |
HUBOT_SLACK_TOKEN -> your slack token
HEROKU_URL -> the url of the app your created on heroku
Now that you’ve done that, go back to where we held off and click deploy
You can also enable automatic deploy so that once you do a push to your repo, it automatically deploys the new change.
Yaaay!!!..we have deployed!.
Let’s test the BOT. Go to your direct messages and look for the bot. Then let’s start interacting with it.
Awesome!!!
Now, we have built our first bot.
This is just scratching the surface, there is a whole lot more we can actually do.
We can connect to third party services, we can invite the bots to channels and make it listen to certain choice of words in users conversations and do something based on that.
We can use the bot with Raspberry PI and Arduino and a lot more.
I’ll be covering other use cases in later posts.
Meanwhile, pat yourself on the back because you have just done a great work and deployed your first bot.
Please, if you have any question or comment, let me know in the comments section below.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016