Skip to content

Instantly share code, notes, and snippets.

@NetRat
Last active August 29, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetRat/21269c6d0e53689fa9a3 to your computer and use it in GitHub Desktop.
Save NetRat/21269c6d0e53689fa9a3 to your computer and use it in GitHub Desktop.
Teaming up with robot!

Before we start

Development is not so efficient without collaboration with team members. That's the fact. But why not to go further and to add a bit of AI to the teams collaboration? Like checking what's the last commit or the load of production server or whatever you decide to have at hand. So, let me introduce Hubot - your company's robot. Install him in your company to dramatically improve and reduce employee efficiency.

Prerequisites:

Step 1 - Setting up Hubot

Set up generator:

% npm install -g yo generator-hubot

Execute generator:

% mkdir your-dir-name
% cd your-dir-name
% yo hubot

Pick unique name for you bot. Select kato as bot adapter.

Check your bot locally:

% ./bin/hubot

You should see something like this:

Bender> bender ping
PONG

For more commands type Hubot help or in this case bender help.

Configure Kato adapter:

% export HUBOT_NAME="Bender"
% export HUBOT_ALIAS="!"
% export HUBOT_KATO_LOGIN="katobot@mycompany.com"
% export HUBOT_KATO_PASSWORD="some-pwd"

More about Kato adapter: https://github.com/kato-im/hubot-kato

Ok, now let's test our bot on Kato:

% ./bin/hubot -a kato

Step 2 - Adding Batteries

Let's make our bot run wild on Heroku. Heroku is the PaaS which allows to run Java, Rails, Node.js and other apps. You can find more information here: https://www.heroku.com/features

First of all, let's add our bot to GitHub.

% git init
% git add .
% git commit -m "Initial commit"
% git remote add origin https://github.com/netrat/hubot.git
% git push origin master

Now it's time to setup Heroku Toolbelt. Here's an example for Debian/Ubuntu:

% wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

For other OSs: https://toolbelt.heroku.com/

Now let's create your Heroku app:

% heroku create

Before you deploy the application, you'll need to configure some environment variables for hubot to use. The specific variables you'll need depends on which adapter and scripts you are using. In our case, we'd need to set the following environment variables:

% heroku config:set HUBOT_ALIAS="!"
% heroku config:set HUBOT_NAME="Bender"
% heroku config:set HUBOT_KATO_LOGIN="katobot@mycompany.com"
% heroku config:set HUBOT_KATO_PASSWORD="some-pwd"

In addition, there is one special environment variable for Heroku. The default hubot Procfile marks the process as a 'web' process type, in order to support serving http requests (more on that in the scripting docs). The downside of this is that dynos will idle after an hour of inactivity. That means your hubot would leave after an hour of idle web traffic, and only rejoin when it does get traffic. This is extremely inconvenient since most interaction is done through chat, and hubot has to be online and in the room to respond to messages. To get around this, there's a special environment variable to make hubot regularly ping itself over http.

% heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=http://netrat-hubot-bender.herokuapp.com

Now it's time to deploy our app:

% git push heroku master

You'll see some text flying, and eventually some success. If not, you can peek at the logs to try to debug:

% heroku logs

Don't forget to assign a dyno to your app:

% heroku ps:scale web=1

If you make any changes to your hubot, just commit and push them as before:

% git commit -am "Awesome scripts OMG"
% git push heroku master

Some scripts needs Redis to work, Heroku offers an addon called Redis Cloud, which has a free plan. To use it:

% heroku addons:add rediscloud

If you want to rename your remote app you can do it:

% git remote rm heroku
% heroku git:remote -a newname

Finally, open the app using URL similar to this one: https://netrat-hubot-bender.herokuapp.com/ You will see something like that:

Cannot GET /

Your app should be up on Heroku.

Step 3 - Some Tuning

Ok, let's extend our Hubot with some hubot-scripts.

Here're two sources were scripts can be found:

In general, for the first one, it will be something like:

  • Add a line to external-scripts.json
  • Add a line to package.json
  • Add environment variables, depending on the script
  • Run npm install

For example, let's add the following scripts:

And just a bit more complicated for the second source:

  • Download the script with wget
  • Add a line to hubot-scripts.json

Something like this:

% cd scripts
% wget https://raw.githubusercontent.com/github/hubot-scripts/master/src/scripts/facepalm.coffee
% wget https://raw.githubusercontent.com/github/hubot-scripts/master/src/scripts/hubotagainsthumanity.coffee
% wget https://raw.githubusercontent.com/github/hubot-scripts/master/src/scripts/heroku-status.coffee

Remember, that it's better to check new modules for you bot locally using console adapter.

Feel free to choose your own scripts for the bot.

Step 4 - What About Steroids?

The previous part was mostly about neat things. But what about real super-powers?

Well, Hubot can do anything that can be done with Node.js.

Hubot is event driven, and when you write scripts for it, you define callbacks that should happen when some event occurs. Event can be:

  • Message in the chatroom
  • Private message to Hubot
  • A text pattern detected in any message
  • HTTP request

Callback can result in:

  • Message in the chatroom
  • Reply to a message
  • Emotion in the chatroom
  • HTTP response (if trigger was HTTP request)
  • New HTTP request
  • Executing a shell command
  • Executing something on a remote server

When you created your hubot, the generator also created a scripts directory. If you peek around there, you will see some examples of scripts. For a script to be a script, it needs to:

  • live in a directory on the hubot script load path (src/scripts and scripts by default)
  • be a .coffee or .js file
  • export a function

Here you can find some scripting basics: https://github.com/github/hubot/blob/master/docs/scripting.md

However, we're going to proceed with the JavaScript example, but you can be inspired by some advanced CoffeeScript examples: https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-learning-more

Or even go further and rule your army of bots with mastermind's console: https://github.com/spajus/hubot-control

But, back to the point. Here's the example of script which outputs the result of the top command, when you ask the bot "How are you?".

module.exports = function(robot) {
    // Listening for the key phrase to trigger callback.
    robot.respond(/how are you\s?\?/i, function(msg){
        
        // Creating child process: http://nodejs.org/api/child_process.html
        var exec = require('child_process').exec,
            child;
        
        // -n 1 - in order to grab the state and exit
        // -b   - Batch mode. Useful for sending output from top to other programs 
        //        or to a file. In this mode, top will not accept command line input. 
        //        Output is plain text suitable for display on a dumb terminal.
        child = exec('top -b -n 1',
            function (error, stdout, stderr) {
                msg.send("In general, I'm fine and ready to kill all humans.\n\n" + 
                    stdout);
                msg.send(stderr);
                if (error !== null) {
                    msg.send('exec error: ' + error);
                }
            });
    });
}

Some manuals to have at hand:

All In One

You can find all mentioned above in this repo: https://github.com/NetRat/hubot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment