Jump-starting Slack bot projects: bot-zero
At wehkamp we believe in enabling teams to automate their work. Lately we’ve been experimenting 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 choices we’ve made.
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:
- Fork the bot-zero project.
- Clone your forked project to your pc.
- Goto http://slackapi.github.io/hubot-slack/#getting-a-slack-token to read up on how to get a Slack token for your bot.
- Add the token to your
- Open a terminal and navigate to your bot directory.
npm installto install the NodeJs packages.
- Start the bot using
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.
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.
npm run validate.
Developing new scripts is as easy as adding a new script to the /scripts/ directory.
.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.
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 advantage 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.
So, that’s it. Clone. Add token. Run. Code. Reload & repeat. Enjoy!