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:
- GitHub account
- Heroku account: https://www.heroku.com/pricing
- Kato account: http://go.kato.im/zwWuaJUM
- one for you
- and one for your bot
- Node.js and NPM:
- CLI way: https://gist.github.com/isaacs/579814
- Cloud9 and Node.js workspace: https://docs.c9.io/creating_new_workspace.html
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="[email protected]"
% 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
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="[email protected]"
% 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.
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:
- https://github.com/hubot-scripts/hubot-explainshell
- https://github.com/hubot-scripts/hubot-calculator
- https://github.com/hubot-scripts/hubot-http-status
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.
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:
- Node.js:
- Original: http://nodejs.org/api/
- In Russian: http://nodeguide.ru/doc/
- CoffeeScript: http://coffeescript.org/
You can find all mentioned above in this repo: https://github.com/NetRat/hubot