Building Somerset

Using BotKit and Aylien’s Text API to Make a Slack Bot

Sam Havens
5 min readMar 12, 2016

Conversational UIs are more popular now than ever before. Others have speculated on what this means and where the trend may be going (see The Future of Chat Isn’t AI, The Beer Test, What Happens Next with Conversational UIs, and My Journey From Blindness to Building a Fully Conversational User Interface for an overview). There’s a lot of talk, and I am worried that we may be at peak-hype in the adoption cycle, but personally I am excited for conversational UIs. Assuming you have good speech-to-text and vice-versa, they are very accessible, and the same bot, via an API, could reside on many platforms (Messenger, Telegraph, Slack, Echo, SMS…). Also, they are really fun to build and play with, and that aspect is what I am going to focus on in this article, by leading you through building a conversational agent built on Botkit and Aylien’s Text Analysis API.

Botkit

If you love Slack and know JavaScript, but haven’t played with Botkit, get on it! It works pretty much straight out of the box, has short- and long-term memory, and can listen for keywords or regexes. If you plan on building this example bot, follow steps 1–5 here to get started (git clone the repo, rather than installing directly from npm, so you have access to the examples).

Aylien’s Text Analysis API

The default bot from Botkit is fun, but I wanted to build something useful. I read a lot of articles, and like sharing them on Slack. Other team members, however, may not share my exact interests. I thought it might be nice to auto-generate a summary, to help someone decide if the article is worth their time. After some research, I decided that Aylien’s Text Analysis API was the best product out there. They have a generous free tier and an easy signup process— what more can you ask for? Well, a nice SDK. And they have that too! (I can only speak to the JS SDK).

If you are coding along, you’ll need an application ID and key from Aylien, available here.

Getting Started

Assuming you’ve already cloned the Botkit repo and gotten a token from Slack for a bot named Somerset (or whatever you want to call him/her), we rename the directory and install dependencies:

$ mv botkit/ somerset/ && cd somerset
$ npm install
$ npm install aylien_textapi --save

Out of the box, we can already play with Somerset:

$ token=your-long-token-from-Slack node bot.js
Yo
Call me :emoji_name:

After a quick change in somerset/bot.js

we are ready to hit the API. But first, let’s get a feel for Botkit.

Botkit in 30 seconds

Here is an example of how Botkit works:

Let’s unpack this. The .hears method takes three arguments: an array (of strings), a list (as a string), and a callback. The array is what our bot listens for. The elements of the array are regex strings. If, for example, we put ‘up’ in the array, it would listen for any string which contains the substring ‘up’. This means ‘sup?’ and ‘uptime’ would both trigger it. If we just wanted to listen for a message containing only the word ‘up’, we would listen for ‘^up$’. This is a regex. If you don’t know regexes, don’t worry, but if you do, you can make the .hears method quite accurate.

Anyway, onto the second argument. In what context do we want Somerset to listen? The example contains most contexts, but there is also ‘ambient’, which means “any message in a channel that Somerset has been invited to.” Finally, there is the callback, which works as you’d expect.

Let’s Summarize (Summarize, Somerset… get it?)

So, Somerset’s raison d’être is to do the following:

Summarizing an article on his mentor, AlphaGo

We’ll use a couple helper functions to do this.

So here is the meat of it:

First off: what the hell? Why are there promises and callbacks mixed together like that? Because it was a pyramid-of-doom callback hell, and I promisified it just enough to be able to understand what was happening. Then I told myself I would clean the code up before writing this article… but of course, that just meant putting this article off. So here it is, and maybe one day it will be cleaner.

Wrap Up

Et voilà! Running

$ token=your-long-token-from-Slack node bot.js

should bring your own Somerset online, and he/she should be able to summarize lots of articles before you hit the limit of the free tier of Aylien’s API. There are a lot of places to go from here: deploying to Heroku, adding functionality, and adding personality, but those are for another post. I will leave you with GIFs, as a teaser.

No joke, this is usually what articles on AI end up being classified as
Go forth and be relevant on social media

If you enjoyed this post, consider recommending or sharing it, and please, let me know on Twitter!

Notes

  1. Why is Somerset masculine? Why does everything default to male? I am actually working on another bot with a female identity, so I decided to alternate and have this one be male.
  2. It is weird that I’m not linking to a GitHub repo with the full project. But, because I deploy with git to Heroku, I hardcoded in the API keys and Slack token, so that repo is private. If you just want to look at my GitHub anyway, here it is:

--

--

Sam Havens

NLP Research @ MosaicML. Previously led NLP at Writer and other startups