Jump-starting Slack bot projects: bot-zero

Kees C. Bakker
wehkamp-techblog
Published in
4 min readAug 16, 2018

At wehkamp we believe in enabling teams to automate their work. Lately we’ve been playing around with GitHub’s Hubot project to build chat-bots for Slack. It has been quite a ride! What did we build? A lot! Bots took control of our job engine and our $universe scheduling. We can view Grafana dashboards and PagerDuty schedules or create incident channels right from our Slack channels. And this is only the beginning…

To give teams a jump start we’ve created the bot-zero open source project. It solves some setup and development problems. In this blog I’ll show how to get up and running in minutes and I’ll explain some of the choices we’ve made.

You can setup your own bot by running the Slack Developer Kit for Hubot.
Bot-zero is a de-cluttered version with some extras.

First time setup

Setup is easy. Hubot is written in Node.js, so make sure it is installed on your system. You also need an editor to code scripts. I use Visual Studio Code (the bot has been setup to do debugging with this IDE, but any IDE will work). If you have an editor and Node.js installed, do the following steps:

  1. Fork the bot-zero project.
  2. Clone your forked project to your pc.
  3. Goto http://slackapi.github.io/hubot-slack/#getting-a-slack-token to read up on how to get a Slack token for your bot.
  4. Add the token to your .env file
  5. Open a terminal and navigate to your bot directory.
  6. Enter npm install to install the NodeJs packages.
  7. Start the bot using npm start.

Easy. Peasy.

The root of the project after running the NPM install.

Improving local development

We’ve added some features to make local development easier. The first improvement is the .env configuration file. Many Hubot packages require you to add environment variables to configure settings. In previous versions we ran into the problem of configuring them on different systems (Mac, Linux and Windows). That’s why we added the .env file. The project will load the settings automatically when you start your bot. Note: you should not commit the file to GitHub as it contains your personal settings.

Writing is usually not the problem,
maintenance is.

Don’t like to use regular expressions to make Hubot listen? We’ve got you covered! We’ve added the Hubot Command Mapper so you can map commands using and API instead of those dreaded regexes. It also helps in speeding up the development of your scripts by providing a reload command, so you don’t have to restart the bot.

The project also enforces a JavaScript standard. As many people collaborate on bots, we believe it is great if there is some sort of automatic standard check. As most coding standards are pretty arbitrary, so is this one: JavaScript Standard. It will try to auto fix violations, but it can’t do everything. A validation is triggered upon test, commit and push. You can validate your code by running npm run validate.

Developing new scripts is as easy as adding a new script to the /scripts/ directory.

Removed coffee examples, added ES6 examples

Brian Donovan started the Decaffeinate Project
to turn .coffee files to ES6 JavaScript.

We love coffee… the drink that is! The default Slack bot installation comes with many (not so useful) coffee-script examples. We’ve removed all of them and added 3 JavaScript/ES6 examples. The bot will still load all .coffee and .js files it finds in the scripts directory.

The first example is the ToDo list (scripts/todo.js) — it shows how to use the Hubot Command Mapper with parameters.

The second is the Norris example (scripts/norris.js) — it shows how to use Axios for web service requests. The Axios client returns an ES6 Promise, making asynchronous operations easier. We think the Axios client does provide better API than the Hubot HTTP client, especially because the Hubot client is optimized for coffee-script.

The third example script is a progress message (scripts/progress.js) — it leverages the Slack API to update a single message. It turns your Slack message into a powerful progress indicator.

Building a progress indicator is easier than you think. Just update the message through the Slack API. The effect is pretty neat.

Testing

So how about testing your scripts? We’ve stumbled upon the Hubot Pretend package that solves this problem. It will emulate Hubot and use an ES6 Promise to interact with the results. We use a combination of Mocka and Chai to do the actual testing. To make things easier to understand and implement we’ve added tests for the example scripts to the /tests/ directory.

One of the advantages of doing using Test Driven Development is that you have a shorter cycle — you don’t have to start the Hubot and manually query it through Slack.

That’s all

So, that’s it. Clone. Add token. Run. Code. Reload & repeat. Enjoy!

Originally published at keestalkstech.com on August 16, 2018.

--

--

Kees C. Bakker
wehkamp-techblog

I work as a Lead Developer for one of the biggest web-shops in the Netherlands: wehkamp. I ❤️ C# and I like to solve nifty problems.