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…
Hubot continues to be used at Wehkamp, but it is no longer the first framework of choice. Slack has evolved a lot, so it makes more sense to use the Bolt framework. We continue to update our bot-zero repo as long as we use it. The latest version is rewritten in Typescript (so we had to change the links in this article). I'll keep this article up as a reference.
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.ts) – it shows how to use node-fetch for web service requests. It returns an ES6 Promise, making asynchronous operations easier. We think fetch provides a 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.ts) - it leverages the Slack API to update a single message. It turns your Slack message into a powerful progress indicator. For more information, check Building an updatable Slack message.
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.
We've removed the testing feature from the latest version of the bot. Unit testing is very valuable, but as the bot contains many integrations, it is not feasible to use unit testing for this.
So, that's it. Clone. Add token. Run. Code. Reload & repeat. Enjoy!
- 2018-08-16 Initial article.
- 2022-08-18 Updated links and content to reflect the latest version of the bot-zero project.