Slacking Off with Bots

As developers, we always strive for efficiency, but often we find ourselves bogged down carrying out the same repetitive tasks, whether that be compiling reports or administration tasks on users. In the world of DevOps, we’ve seen the rise in tools such as Chef and Puppet, making managing infrastructure a breeze. New servers can be provisioned and brought online at the click of a button.

At Unicorn Agency, we’ve been investigating how we can bring the benefits of automation to our team, using a tool we each use everyday, Slack.

Using the Slack Ruby Bot SDK we’ll dive into the basics of creating a chat bot.

Setup

To get started, create a new folder for your project and inside create a Gemfile

A quick bundle install is all thats required for us to be good to go!

Hello, World!

Let’s run through the above code real quick. We define a class that inherits from SlackRubyBot::Bot to begin, which gives our class all the behavior it needs to act as a bot.

Commands in Slack are defined by blocks, most commonly using the command keyword. There are a few others that we’ll look at later on, but command is fine for beginning with. command takes two arguments, the keyword we want our bot to listen for and a block of code that will be executed when matched. client lets us interact with Slack, data includes some information about the chat, such as user and channel. We can worry about match later, it provides some useful Regular Expression functionality.

In our block, we are simply telling the client to send the text ‘world!’, to the channel we sent the message from. Simple as that!

Regular Expressions

As well as defining simple command statements, Slack also supports regular expressions and even automatically matches on them for you.

Let’s take the code from above and make it a little smarter:

We’ve created a regular expression that will capture a name from the command greet Name. Slack automatically passes the match to the block handling our command, so we don’t even have to do that ourselves. Pretty handy!

Attachments

These simple examples are great, but what happens when you want to display a large amount of text, say some debug output or a large list of database results? In the Slack GUI, you can easily create attachments, that expand out when clicked, not crowding your chat channel. This is also pretty easy through the bot API. To do so, we can utilise the web client API.

Let say we have a hash of data in Ruby, that we want to render to Slack.

We simply provide a title, filetype, content and a filename and that will be wrapped nicely into a JSON file and posted to the chat.

Long Running Requests

As a rule of thumb, long running Slack commands should not block more commands from being processed. This is pretty easy to handle, as the Ruby Slack Bot already makes use of EventMachine. We can simply render our response in a new thread and all other commands will be responded to, while our long running process is doing it’s thing.

Overall, we are really enjoying automating frequently performed developer tasks with Slack Bots. The sky is really the limit as to what your bot can do.

Are you using Slack bots on your team? Let us know!

This article was written by one of our AMAZING Unicorns

If you liked it clap it up!

Mark Provan is a Software Developer and Unicorn hailing from Sunny Scotland. He loves using his skills to build products that solve real problems for businesses and people. When not at the computer, Mark enjoys cycling and blogging at markprovan.com. He can be found on Twitter @markprovan

Want to build magical experiences together? Contact us!

E-mail: adam-leonard@unicornagency.com

Twitter: @UnicornHQ

Website: http://www.unicornagency.com