Regexbot – Slack chat bot and Jira integration

See the code on Github

Slack is a great tool with loads of plugins and integrations. There’s already an integration with Jira that allows you to create and update issues from Slack; but I wanted a bot that would listen out for Jira case numbers, and produce a link to the case along with the case summary/title. The goal was something like this:

regexbot

There are a few solutions for building bots, in particular Github’s hubot is a sensible choice – not only does it come with loads of stuff built in; but also abstracts the connection to the chat system. This means that one piece of code can connect to Slack and many other chat systems.

However; being in this for the lulz I rejected the sensible option and went for the slack-client npm package; while browsing around the Slack API docs to try and figure out what to call. In particular we’re interested in the real time messaging (RTM) api. slack-client makes it remarkably easy to create a self-hosted bot; or “bot user integration” in Slack parlance. The steps are roughly:

  1. Create a bot user to get a secret token that can be used to authenticate to Slack
  2. Open a connection to the RTM api
  3. Listen for when someone posts a message; and write a function to reply back

However, it still seemed worth abstracting away the capture and detection of Jira case numbers. Thus Regexbot was born – it will sit on any Slack channel its invited too; and listen out for messages matching a regex. When it finds one it’ll reply with either a bit of text or call the specified callback. This is just a tiny improvement over the existing slackbot functionality; but it makes quite a difference as replies can now substitute in bits of the original message using the regex matches.

Having sorted out the link – just substituting any matches of /[A-Z]+-[0-9]+/g into an URL – we can now think about retrieving some info from Jira’s JSON API. In my regexbot installation i’ve got a function that just uses node’s https module to do this; but there already exists a jira npm package that would probably be a better choice.

It’s a pretty useful tool and being able to just type out a case number and get a link and description back just makes life that little bit easier. I guess I should also emit the assignee for hassling purposes…

The next step would be to package this up and deploy it as either a Slack App (a SaaS system) – although this would naturally only be able to connect to a publicly exposed Jira – or perhaps a Jira plugin. Could be interesting; and would make it so much easier to use.