Meet the Amazing Hubot

Recently, I came across Hubot – the amazing bot from Github, which lets me do nearly anything while talking to it. Some of the examples being querying my site for availability, tracking down how many visitors I had in last 24 hours, updating my twitter status, grabbing tasks from my basecamp projects, and so on.

Currently, I am using Hubot on HipChat and GTalk, but as my team frequently uses Skype, I will be enabling a Skype adapter for it, pretty soon (the Skype adapter, in particular, can not be installed on Heroku). The Hubot instance runs as Edwin Jarvis, as I always wanted to have something close to the esteemed J.A.R.V.I.S. from the movie Iron-Man.

Installing Hubot

Here is a brief overview for anyone wanting to get started with Hubot:

  • Install Node.js
  • Clone the hubot repository from github, locally:
1
2
3
4
git clone http://github.com/github/hubot jarvis
cd jarvis
npm install
bin/hubot

This should start the Hubot in your Shell. If, instead, this gives an error saying redis was not found or something similar, make sure to remove redis-brain.coffee from hubot-scripts.json file, and try running again!

Using Multiple Adapters

Ok, so running Hubot locally is good – we can see what it does, we can test out our code/scripts, etc., but the real power of hubot lies in running it over a chat server, so that it acts like ChatOps – you can virtually do anything online by simply talking to the Hubot (think: deploying your code).

Since, Hipchat now provides a free plan for upto 5 users, it stands ideally as a Chat server for our needs (we are a small team). Additionally, GTalk serves as a quick access to our bot, since it is always open via either the GMail browser tab, or via some chat client. However, there is a sligh problem. Hubot doesn’t currently support multiple adapters, i.e. we can’t tell hubot to run on both Hipchat and Gtalk, simultaneously, while using the same source code. But, why use the same source? Why not make another hubot to run on GTalk? Well, I use a lot of scripts – some are custom – and it is a pain to update two sources to make the two hubots use the same set of scripts. Therefore, I implemented the following procedure to let the two hubots use the same source:

  • First, create new dedicated accounts for our hubot on both Hipchat and Gtalk.
  • Add hubot-hipchat and hubot-gtalk as dependencies in package.json. It should look like:
1
2
3
4
5
"dependencies": {
    "hubot-hipchat": ">= 2.4.5",
    "hubot-gtalk": ">= 0.0.1",
    ...
}
  • Edit Procfile so that it uses adapter name and user from the environment variables that we will create for the heroku instance of each bot:
1
web: bin/hubot -a $HUBOT_ADAPTER_NAME -n $HUBOT_ADAPTER_USER
  • Create a git repository for our bot, so that we can track our bot’s code:
1
2
3
git init
git add .
git commit -am "Initial Commit"
  • Download and install Heroku Toolbelt, since we will be deploying our bots to Heroku.

  • Let us, first deploy our bot on GTalk, since it is a bit easy. Start by creating a Heroku application for our GTalk bot:

1
2
3
heroku create
heroku rename 'some-random-name-for-our-gtalk-bot'
git remote rename heroku gtalk
  • We will be using redis as our bot’s brain. So, install redis on our heroku instance (which is free, by the way):
1
heroku addons:add redistogo:nano
  • Push the bot to heroku:
1
git push gtalk master
  • Set our environment variables for the GTalk bot:
1
2
3
4
5
6
heroku config:add \
  HUBOT_ADAPTER_NAME="gtalk" \
  HUBOT_ADAPTER_USER="<bot name>" \
  HUBOT_GTALK_USERNAME="<bot gmail complete address>" \
  HUBOT_GTALK_PASSWORD="<bot gmail password>" \
  HUBOT_GTALK_WHITELIST_DOMAINS="<your company domain name>"
  • Run our heroku process:
1
heroku ps:scale web=1
  • Your Gtalk bot is now live.

  • Let us, now, run this on HipChat. Start again, by creating a Heroku application for our Hipchat bot:

1
2
3
heroku create
heroku rename 'some-random-name-for-our-hipchat-bot'
git remote rename heroku hipchat
  • Install redis:
1
heroku addons:add redistogo:nano
  • Push the bot to heroku:
1
git push hipchat master
  • Set our environment variables for the HipChat bot. Settings marked with @@ can be obtained by visiting your bot’s XMPP/Jabber account settings, after logging in HipChat with your bot’s account:
1
2
3
4
5
6
heroku config:add --app="<name-for-our-hipchat-bot>" \
  HUBOT_ADAPTER_NAME="hipchat" \
  HUBOT_ADAPTER_USER="<@@room name>" \
  HEROKU_URL="http://<name-for-our-hipchat-bot>.herokuapp.com" \
  HUBOT_HIPCHAT_JID="<@@username>@chat.hipchat.com" \
  HUBOT_HIPCHAT_PASSWORD="<hipchat.com password for your bot>"
  • Run our heroku process:
1
heroku ps:scale web=1
  • Your HipChat bot should now be live. Try logging in as a normal hipchat user, and talking to the bot (hint: @jarvis help, where my bot’s mention name was @jarvis)
- by Nikhil Gupta

Comments

Copyright © 2015 - Nikhil Gupta - Powered by Octopress