Simple Slack Bot Integration (Step-by-step)

Benjamin Brooke
Chingu
Published in
4 min readJul 31, 2017

No more head scratching — this is how it’s done

Step 1: Setup Bot with Slack

Click new bot user integration

Click this link and scroll down until you see the paragraph up there

That will bring you to the page below where you name your Bot and click the big green button. Easy so far.

Give your Bot a Name

Next you will be brought to a page that gives you an API Token, it’s the long string that starts with ‘xoxb’. Here you can also pick an emoji or image for your Bot and add a description. Once you are done again click the big green button. Now you can go to you Slack chat and type /msg [yourBot’sName]. Now you are in a direct message with your Bot. Only it does nothing. Yet.

Step 2: Get Started With Your Code

In this example I will use Cloud 9 for development, but you can use whatever you want. You will need Node.js for this example. If you are on the newer side, like me, Cloud 9 development environment for this. Each workspace has Node.js pre-installed and it is free. Cloud 9

If you don’t have a Github account make one. Then Go To This Repository and Fork It. This will make a copy of all this files in your account. These files are the javascript framework for your Bot.

Fork is upper right. Clone is the big green button.

Next, go to your account and click clone. Copy the web address inside.

Next, create a Cloud 9 account if you don’t already have one. This will make working as a team fun. Create a new Cloud 9 Workspace. Name it whatever you want and paste the clone web address in the Clone From Git field. Use Blank for template.

Now you can work as a team easily

Step 3: Explore Your Code

Make sure you are in the same folder with bot.js, index.js and project.json files. Now type npm install on the command line. First lets look at bot.js. This file is all set up for you. It creates the bot instance and connects to Slack. There is some logic in here that creates the bot commands you will use in index.js as well. You don’t need to worry much about this file.

Snippet from bot.js

This above snippet shows us the bot is using RegEx to detect patterns in messages that are typed in channels it is in. The snippet also describes respondTo and send methods. The bot responds to certain patterns and sends certain responses back to the channel. That is all you need to know for now. Let’s look at index.js. This is what you will be working with.

Snippet from index.js

In the above snippet the bot is coming equipped with an example response. This is the code for the bot to respond to “hello”. The bot will return “Hello to you too” in the channel. Now you can follow this pattern to create more interactions.

Step 4: Starting the Server

The Bot is running in Node.js so the server needs to be up and running for the Bot to work in your Slack. To make this happen you will need the API Token from earlier. Type the following on the command line to start your Bot. Boom. You should get a small message saying your bot is running in the terminal. Now go test it in Slack.

SLACK_TOKEN=<BOT_SLACK_TOKEN_HERE> node index.js

Step 5: Taking Thing Further

There is a lot more you can do. If you want some more examples you can checkout the testing branch of the repo you cloned. There you fill find examples of how to make API calls with your Bot. You can make your Bot tell jokes, deal card games, dispense news…the possibilities are endless. Installing a package like node-fetch makes doing the API calls much easier.

Step 6: Have Fun

--

--