GENERAL

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

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

help.coffee

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

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.

/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!”.

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

new help.coffee

You can check out how it works on your terminal by running bin/hubot like so

Screen Shot 2015-10-30 at 7.53.04 AM

Screen Shot 2015-10-30 at 7.53.30 AM

Great!

Time to deploy

:smile: 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

Screen Shot 2015-10-30 at 7.58.22 AM

4. Now head over to your heroku dashboard. Click on the app

Screen Shot 2015-10-30 at 8.02.13 AM

 

Click on Connect to Github and connect your github repo where the app resides.

Screen Shot 2015-10-30 at 8.05.14 AM

Now before you click on deploy. Head over to your slack team and integrate Hubot.

 

 

Screen Shot 2015-10-30 at 6.20.29 AM

 

Screen Shot 2015-10-30 at 6.20.43 AM

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

now xxxxxxxx… refers to your slack token

Now let’s add our heroku config

Go back to your terminal and run:

and

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

Screen Shot 2015-10-30 at 8.14.42 AM

 

You can also enable automatic deploy so that once you do a push to your repo, it automatically deploys the new change.

Screen Shot 2015-10-30 at 8.14.49 AM

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.

Screen Shot 2015-10-30 at 8.19.07 AM

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. :smile:

 

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.