So I made a Slack bot… @khaledbot.

Khaledbot Website

You can get the bot yourself at khaledbot.com, or download the source from my github repository. As @khaledbot says,

Learning is cool, but knowing is better, and I know the key to success.

So here is how I created this Slack bot, and an introduction to creating bots for Slack in general.

1. Create a Bot User with Token

The first thing we will need to is create a new bot user for the Slack team.  

Creating a slackbot

All we need to do is choose a username for the bot, and a custom API Token for the bot will be generated. We can also add extra details like a real First and Last name for the bot, and a description of what the bot does.

2. Create a New Node.js Project with Botkit

As I’m more familiar with JavaScript, I decided to create this bot using Node.js and Botkit.
Botkit is a framework, recommended by Slack, for building bots quickly and easily. 

We can install the botkit module via npm -

npm install botkit --save  

Once botkit is installed, we can setup the bot. In the main index.js file -

var Botkit = require(‘botkit’) // require botkit module  
var token = process.env.SLACK_TOKEN // get slack token passed as variable

// Setup new slackbot with botkit
var controller = Botkit.slackbot({  
  debug: false
})

// Check that token was passed
if (!token) {  
  console.error(’SLACK_TOKEN is required’)
}

// Start Slack’s Bot Real Time Messaging API (RTM)
controller.spawn({  
  token: token
}).startRTM(function(err,bot,payload) {
  if (err) {
    throw new Error(err)
  }
});

3. Start Bot

While working on the bot, we can start it locally. From the terminal, while in the directory for the project, all we have to do is pass the token (from Step 1) as a variable while starting the main file - 

SLACK_TOKEN=[slack-token-here] node index.js  

4. Listening for Events and Keywords

Once our bot is setup, we can start adding functionality to make it do what we want. Typically, bots respond to events that happen, e.g. a user joining a channel, or to keywords.

Listening for Events

There are several events that a bot can respond to. For example - 

  • bot_channel_join - When the bot is invited to a new channel
  • direct_mention - When someone mentions the bot’s username directly, e.g. @khaledbot: hi!
  • direct_message - When someone sends a direct (private) message to the bot

For @khaledbot, I had it listen for when it was added to a channel. For example -

controller.on("bot_channel_join", function(bot, message) {  
 // do stuff
})

Listening for Keywords

A bot can also be alerted when another member of the Slack team mentions a keyword in certain circumstances. To set this up, we specify two things -

  1. The keyword to listen for, e.g. "hello"
  2. The context in which the keyword was mentioned. For example, if it was a direct_message, a direct_mention, or ambient (when the bot is not directly mentioned, but is active in the channel)
controller.hears(array_of_keywords, array_of_contexts, function(bot, message) {  
  // do stuff
})

For @khaledbot, whenever someone mentioned “khaled", the bot was alerted - 

controller.hears(["khaled"], ["ambient"], function(bot, message) {  
  // do stuff
})

5. Responding to Events

When an event happens, we want the bot to do something in response. A bot can send a single reply using bot.reply. This function accepts two arguments - the message the bot is replying to, and the reply text.

For @khaledbot, whenever someone mentioned “khaled”, the bot responds to them.

controller.hears(["khaled"], ["ambient"], function(bot, message) {

  var userID = message.user // the ID of the user that mentioned "khaled"
  var user = "<@"+userID+">" // wrap around like this to create an @ mention of the user

  var reply = user+" you spoke my name?"; // create reply

  bot.reply(message, reply); // send reply
})

 

We can use a series of these listens and replies to create the bot.

6. Deploying to a Server

Once we have created our bot, we need it to live on a server to be online 24/7.

For @khaledbot, I used Beep Boop, which is a service for easily deploying Slack bots. They offer free hosting for your bots for 24 hours (after that, you can restart the bot for another free 24 hour period or pay to maintain uptime).

And We're Live

Demo of khaledbot

You can get @khaledbot from khaledbot.com. It's also on product hunt. If you have any feedback on how I can improve on it, let me know. Bless up!