Hubot is a chat bot built by GitHub to automate things through chat. Hubot comes with great features such as integration to varios chat services. This will be a two post series explaning how to setup hubot, and how to add a utility to hubot such that when you ask hubot who is oncall, it would lookup the google sheet for oncall and will respond with the oncall name and number. So lets get started.

Part 1 - Installating and configuring Hubot

This should be the simplest part, you can follow the official hubot documentation. It can be found here. We are not convering step by step details of the installation as the it is very well documented in hubot docs.

After the installation, you can move into the directory where its installated and execute ./bin/hubot This should land you in a prompt of hubot. This is the place where you can interact with hubot. You can test whether hubot is working correctly or not by executing a ping command.

myhubot> myhubot ping
myhubot> PONG

As you would notice, I have named my hubot as myhubot. You can ping the bot by referring to the name that you have set.

Now lets do a quick walk over hubot installation dir. Below is the contents of installation dir in my computer.

➜  ~/myhubot: ls
Procfile              bin                   hubot-scripts.json    package-lock.json     scripts
README.md             external-scripts.json node_modules          package.json

node_modules contains modules which are required for hubot. Hubot ships with couple of inbuilt commands, you can do yourhubotname help to see the list of commands, most of the these modules are residing in node_modules dir.

The dir where we can put our custom scripts are in scripts dir.

Scripts

hubot framework supports writing scripts in CoffeeScript or in JavaScript(nodejs), we would using JavaScript in our tutorial.

Our First Script

In our first script, we would just see how to capture an input given to Hubot and how to echo back.

Create a file called hello.js under scripts/ dir. Include following contents in hello.js

module.exports = function(robot) {
        robot.hear(/hello/i, function(msg){
                msg.reply('world');
        });
}

This snippet will respond with the word world, when it hears hello. Save the file and start hubot again by ./bin/hubot. When you enter hello in the prompt, you should see a response like this,

myhubot> hello
myhubot> Shell: world

Cool!, now we know how to listen for a word and how to respond.

Interpret

We now need to know how to interpret a message and how to respond accordingly. For example, when I tell hubot to get me a something, it should respond with Sure!, I will get you something.

Open up our file scripts/hello.js and replace the content with the following snippet,

module.exports = function(robot) {     
                robot.hear(/get me a (.*)/i, function(msg){                   
                capture_string = msg.match[1]                                 
                msg.reply('sure i will get you a ' + capture_string)          
        });        
}

This is similar to what we have written previously but if you notice, we have few extra things,

 	robot.hear(/get me a (.*)/i, function(msg){ 

The regex (.*) tells to capture everything that comes after the string get me.

	capture_string = msg.match[1]

msg.match contains the items that have matched with our parameters. In our case, msg.match[1] contains the string which came after get me a.

Save the file and fire the command ./bin/hubot again, in the prompt, you should see something like this.

myhubot> get me a dollar               
myhubot> Shell: sure i will get you a dollar 

Great! Now we know how to interpret the message. In the next part of this series, we will integrate google-sheets with our hubot. But before this, you need to configure certain things in your google account. Details are documented in Google API docs. There are 4 steps, which are straightforward. Follow the steps and get the credentials.

See you on next post!