Hackathon 2018 – It’s Dead Jim

JIRA Down! 

If I had a £1 for every time JIRA went down, financially… it would make no difference, but by god can it be frustrating when JIRA decides to have its own little vacation. So we thought wouldn’t it nice if we had some sort of notification telling us when it has gone for walkies rather than everyone asking “has JIRA gone down?” followed by a unison of sighs. That is why we came up with the idea (thanks to Harry Mitchinson) to create a bot which generated a random meme related to JIRA being down and is sent to a Rocket Chat room whenever it went down.

Time to Hack the Mainframe

There were three of us, Jakub Dudelo, Nick Spicer and myself (Rajeev Arora), who were tasked to complete this formidable project, in which we only three days where we poured our blood, sweat and tears to create this beast… I mean bot.

We had two different directions in which we decided to take, in terms of tackling this project. One direction was using the current technology which we were comfortable .Net. The other direction was using newer and different technology such as Docker and a language which I’m pretty sure is used to summon the devil himself, Coffeescript.

Since we were also able to complete out project on schedule, we decided to do a bit more which allowed us to dabble the deep depths of computer vision.

It’s Alive… Sort Of (Thought Process)

The creation of a bot was no easy task, considering we had no idea where to start, but after a putting our brains together and intensive googling, we finally knew where to begin.

Dead Man walking (Dead Man switch)

We were thinking what would be a good way to tackle this, and one idea was to use the concept of the Dead Man switch. If you do not know what a Dead Man switch is, here’s a ELI5:

Imagine you had a box with a button on the side of it. Now this box requires you to press the button every 10 seconds, but suddenly you forgot because you got distracted by the pretty butterfly. Now more than 10 seconds have passed and a internal switch is triggered causing the box lid to open pouring all kinds of evil, misery, disease and death… well done you just opened Pandora’s box.

So using said concept, our trail of thought was, constantly check, for every x number of seconds, if you could get onto JIRA, if all of a sudden JIRA is “dead”, the trigger our bot to send a meme into chat.

Captain Web-hook (Webhooks)

With Rocket Chat you are able to create incoming webhooks. With these webhooks these you are able to write a script, so every time the url for this hook is hit the script is ran. So what we had done was create the meme generation script, which randomly picked a meme, have text had via the code for the dead man switch and send it the the chat room.

Mr Hubot-o (Hubot)

However even though we were able to create this “bot” that worked quite effectively despite the odd occasion of DDoSing the JIRA, we were fully utilising the bot creation provided to us.

Lucky for us Rocket Chat actually provided the means to create these bots which could live in rooms and to do numerous of tasks. They had also provided an adapter to make out bots come alive, called hubot.

So to create a bot user it was a pretty easy task. All you had to do was go to the administration page, where you would locate the user tab. Then simply you would add another user, filling all the correct details in and set the role of the user to bot. Voliá you now have a bot. Only issue its offline and not doing a lot, essentially its just an empty shell which needs to be filled up.

OK, now we’ve got a bot, but its not doing anything, so what’s next you’re asking? As said before Rocket Chat also provided us an adapter, Hubot. In short, Hubot, is an open source adapter which allows the bot in Rocket Chat come alive, by running scripts written CoffeeScript on Node.js.  By using a combination of Docker and scripts written in CoffeeScript, we were able to create a living bot.

“Whale Noises” (Docker)

Our first step was to actually get the bot alive and working. This is where we were pointed to Docker in the Hubot documentation. So what exactly is Docker? It is a tool which provides container management services. So essentially you’ll be able to develop an application, package it (container), and run it where ever you want.

For the bot it is required for it to be running all the time and also needs to have scripts so it could perform certain tasks. For this to be possible, by running a small script, we were able to create an image, which then simultaneously started to run an instance of this image, which is also known as the container. Now we have this container running, if you were to look at Rocket Chat, you’ll be able to see that the bot is now online. Depending on the scripts written for this bot, they should able to perform tasks they’ve been created for.

However to get these bots to perform these various actions, you’ll need to write some scripts in CoffeeScript before creating this bot in your image.

Wake up and smell the CoffeeScript

You are probably now asking, what the hell is CoffeeScript? Don’t worry I was like that too,  but once you start getting into it you realise it basically JavaScript with slightly different syntax and requires you worry about white-spacing. Other than that… its alright.

We were able to create some scripts, with the help of Hubots API, which had a few commands that allowed the bot to hear, reply, respond and so on.

An example of a bot that was created was one which would respond to a phrase sent in the chat room. Essentially the bot would wait and listen for the name of itself to be mentioned with the phrase “joke” alongside, so it would like “@Bot joke”. The bot would then respond with the a joke randomly picked out from a subreddit r/joke. Only problem with this was that it would show any joke… and yes that was including the NSFW jokes.

module.exports = (robot) ->
  robot.respond /joke/i, (msg) ->
      .get() (err, res, body) ->
          data = JSON.parse(body)
          children = data.data.children
          joke = msg.random(children).data
          joketext = joke.title + ' ' + joke.selftext
          msg.send joketext.trim()
        catch ex
          msg.send "Error, something went wrong - #{ex}"

Looking at The Fourth Dimension (Tesseract API)

Great! We’ve finished our project on time, even with time to spare… now what? Was there anything that we could add to this? That’s when we came up with the idea, with the help of Rob Church, of wouldn’t be great to get a notification about the status of the coffee machine straight to the computer, without worrying about getting up, going to to kitchen and getting that lovely, “out of beans” or “fill the tank” message.

So the premise of this idea was to get a camera to be constantly looking at the coffee machine screen, and the camera to take a picture every x number of seconds. The picture will then be uploaded to some sort of cloud directory, where text of the picture will be read and then sent to Rocket Chat via a bot.

After a good amount of googling, we finally found an api which we thought would be perfect for this type of task: Tesseract, an open source OCR (Optical Character Recognition) API. With this we’re able to add this into our meme messaging code, where it would look at a google cloud directory. Then is would look at the newly added picture where the text would be read and then put on a meme.

The Good, The Bad, The Ugly

Docker was a very helpful tool to use and think it should definitely be utilised in everyday development, especially since it can be used for micro-services (note: that’s why we use it! To be fair, not on the website so it was new to most of those on this project – Ed.). Being new to the concept of containers and images, it was pretty easy to get a good grasp of what docker provides, and the tool itself is pretty friendly. Great thing about this tool is there seems to be a strong community base with docker, so if you need help its literally a quick google away.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s