Nick Hammond

Software Developer, Cyclist, & Traveler.


In botkit, development

I’ve always been fascinated with simple messaging systems. Back when SMS was the primary text message platform I worked with an agency to build various text based projects.

One project was for an anti-smoking campaign and it had a dynamic messaging flow and the idea was to basically bring your addiction to life, to give it a personality. A constant reminder to the user that they’re trying to quit and that they wanted to disassociate themselves with their habit. It was a really fun project and we utilized a voice API to truly give the addiction a voice.

I built various other projects similar to that and the simplicity but effectiveness of that kind of a tool is great. It’s a simple send and receive messaging format that’s incredibly accessible.

The other night I went to the Refresh Austin meetup and I had this same spark of excitement about a new tool called Botkit. It’s a simple library to build bots for Slack, an incredibly simple library. Here, just let me show you.

Botkit is node based and you can utilize any Node package to get the functionality you need. To get things going I just started off with a simple bot to grab the latest video from a Youtube channel.

You could definitely utilize the YouTube API and send a request to get videos within a channel but I’m going to simplify things and just scrape the channel’s page. I don’t really feel like digging through the API, I just need that one piece of data and we can utilize the Scrape library to do that.

Go through the setup steps on Botkit’s site and add a new bot on your Slack team to get going. Once you’ve done that then we can start scripting the bot.

Botkit allows you to hear, reply, and have multi-message conversations. You can keep things super simple and just respond to keywords or you can get more involved and have a multi-message conversation with the bot. For this we just want the bot to listen for a simple phrase, “casey me” which will grab a new video from Casey’s channel.

Go ahead and start setting up your bot, we’ll name this file bot.js:

var Botkit = require('botkit');
var Scrape = require('scrape');

var controller = Botkit.slackbot({
  debug: false,
  logLevel: 7 // verbose logging

// connect the bot to a stream of messages
  token: slackbot_token

* startRTM connects to Slack’s Real Time Messaging API.

If you haven’t installed the Scrape package yet, go ahead and do that with npm install scrape. Now if we run the bot.js file with node we should see our bot in our Slack team. Go ahead and run node bot.js to have the bot connect to your Slack team. Geez, that was easy. Typically you have to write code to handle opening the socket, processing incoming messages, disconnecting, etc.

The bot still can’t do anything though, it just connects and that’s it. Now we can script it to hear things in Slack.

Here’s the method for a bot to hear something:

controller.hears('phrase, regex, or keyword to listen to', ['ambient', 'direct_message','direct_mention','mention'],function(bot,message) {
  bot.reply(message, "I'm alive!");

The first argument to hears can be a simple string or a regex to match whatever you’d like. The second argument is just an array of the various types of messages that the bot can listen to. Now that we have all of that setup we can swap that out with what we’d like to the bot to actually respond to.

controller.hears('casey me',['ambient', 'direct_message','direct_mention','mention'],function(bot,message) {
  Scrape.request('', function (err, $) {
      if (err) return console.error(err);

      video = $('.channels-content-item .yt-uix-tile-link').first();
      url = "" + video.attribs.href;

      bot.reply(message, video.text + " " + url);

From looking at YouTube’s source you can see that we just need to grab the first .channels-content-item and then we can grab the URL from there. We then tell the bot to reply to the original message with the text from the URL and the actual video URL. Since we’re putting the URL in the message Slack will take over and give us a thumbnail preview of the video as well, see Slack’s formatting documentation.

That’s pretty nifty. Think about all of the other uses you can utilize that for! You can build more advanced tools that interact with an API, aggregate data from various sources, kick off a CI job. You can also run Botkit within your internal network to access any data or services on your network that you don’t want to expose which is great.

What’s great about Botkit is that you can utilize any Node package which offers an endless amount of functionality. I’ve already setup a personal Slack team and started scripting various other bots to build my own little personal assistant bot, it’s wonderful.

*One other thing to note that’s not as obvious at first is that if you want your bot to be in a channel you must invite it to that channel in slack. If you don’t invite it to a channel then you can just send it a direct message instead.

P.S. We should keep in touch, subscribe below.