samdlc

A Super-Simple Slack Bot Example

April 05, 2016

Hi folks. In this post I will be walking through the basics of creating a Slack Bot custom integration. Slack is an incredibly developer-friendly platform, and offers APIs which allow you to build, amongst other things, automated bot users.

As I’m sure you can appreciate, there are countless uses for bot users. I hope to cover some other examples in future posts. For now I will be demonstrating “EchoBot” - a perfectly simple bot that repeats anything you say back to you.

Prerequisites

So before we get into the tutorial, you should have the following already set up:

  • Node.js and NPM on your local machine.
  • Elevated access to your Slack domain.
  • That’s it!

Slack Integration Setup

Before you start coding, you should set up a new bot user integration on Slack. This requires you to have elevated privileges on your Slack domain, so if you don’t have these then head over to Slack and create a new team!

On the new bot user form, pick a unique name (I chose EchoBot), and submit the form. On the next screen, grab the API Token, and customise the bot to your liking.

Project Setup

Now you have your Slack API Token, you’re ready to start coding! Well nearly, you just need to setup your project directory and dependencies.

First off, open a terminal and make a folder to hold your project files.

$ mkdir echobot
$ cd echobot

Next, initialise NPM. In the interactive menu, feel free to customise, or just hit enter to accept the defaults.

$ npm init

Now you need to install a useful library node-slack-client. This will provide us with a simple, but powerful set of tools for interfacing with Slack via their Real Time Messaging protocol.

$ npm install --save @slack/client

Coding the Bot

Congratulations on making it this far, now for what you really came here for! I’ll post the code in its entirety, as it’s only 14 lines, and then explain what’s going on.

// index.js
var Slack = require('@slack/client');
var RtmClient = Slack.RtmClient;
var RTM_EVENTS = Slack.RTM_EVENTS;

var token = 'YOUR_API_TOKEN';

var rtm = new RtmClient(token, { logLevel: 'info' });
rtm.start();

rtm.on(RTM_EVENTS.MESSAGE, function(message) {
  var channel = message.channel;
  var text = message.text;
  rtm.sendMessage(text, channel);
});

So we start off by importing the node-slack-client library mentioned earlier. We also keep a reference to RtmClient and RTM_EVENTS, for convenience.

We create an instance of RtmClient, passing in our api token and a config object. This library uses winston for logging, so define your log level as is appropriate.

When rtm.start() is called, our client will attempt to connect to Slack. If there are any errors, they will be logged to the console.

Finally, we set up handling of received message events, and in our callback simply use rtm.sendMessage to repeat the received message back to wherever it was originally sent.

Notice that we’re sending our message to a “channel”. This could be a direct conversation with a user, or a group channel. Try inviting echobot into a group chat and see what happens! (Disclaimer: you may lose friends / the respect of your coworkers).

Although the api token is being stored as a string in this example, please don’t ever commit your tokens into SCM (such as git). As an alternative to strings, use process.env.API_TOKEN and pass in as an environment variable.

Finally, to start the bot running, just run node index.js, and direct message echobot!

That’s it!

So as you can see, creating your first Slack bot is incredibly simple, thanks to the node-slack-client library available.

In future posts, I’ll be delving into some more interesting use cases for Bots, and Slack integrations so stay tuned.

Thanks for reading, and if you have any questions please feel free to contact me at sam [at] samdlc [dot] com.


Sam Delacruz

Written by Sam Delacruz
I'm a Software Engineer from London, building useful things.