In the past weeks, in order to explore new ways to improve DevOps people daily job introducing chatbots, I had a chance to evaluate and play with Hubot. It is an Open Source chat robot implemented by GitHub Inc. which is easy to program using simple scripts written in CoffeeScript and runs on Node.js. I started almost from scratch, being this my first production experience with Node.js and the first experience at all with CoffeeScript.
In this post I am sharing just the basics to start implementing a personal Hubot. Prerequisites to follow this tutorial are Node.js and the npm package manager for JavaScript. Download and install the latest versions for your OS. In this post I am going to refer to Node.js 6.10.3 and npm 4.6.1.
First of all you need to install the Hubot generator:
npm install -g yo generator-hubot
Then create the directory for your first Hubot:
mkdir firstbot
and generate the bot instance through the yeoman generator:
cd firstbot
yo hubot
At creation time you will be asked for some information: the bot owner, the bot name, a description for it and the adapter to use. An adapter is the interface to the service you want your Hubot to run on. Hubot provides two official adapters, Shell and Campfire, but several third party Open Source adapters are available for the most popular chat services (Slack, XMPP, Facebook Messenger, etc.).
Now you can start the bot. The start script has been generated in the bin directory inside the bot home. Run
./bin/hubot
and the bot is ready to interact with you in a command shell. You can check for a list of the available commands by typing
firstbot help
Let's see now how it is possible to implement and add a custom script (scripts give really power to a bot). A script must export at least one function. So the first line of code (excluding comments) has to be the following:
Then you can start to add your code. The most common interaction between the bot and humans is based on messages, so the hear and respond methods are the most used:
It is possible, through regular expressions, to capture content from the input messages using res.match. Example:
The bot can do more complex things, like HTTP requests for example:
Here's an example of HTTP request to a Jenkins server to get the results of the unit test for a given execution of a build job by parsing the JSON content of the response:
The first match there is the build job URL and the second one is the build number. The output by the bot would be like this:
Test results: 32 passed; 0 failed; 0 skipped.
Once you have completed a script implementation, in order to register it you have to save it with the .coffee extension in the scripts directory of the bot home and then restart the bot to use it.
The process of implementing bots in Hubot and enhancing them through scripts is pretty straightforward. Furthermore there are several hundreds available scripts ready to be installed through npm: so please check that list before implementing anything.
I will share next more interesting scripts and tips on Hubot.
In this post I am sharing just the basics to start implementing a personal Hubot. Prerequisites to follow this tutorial are Node.js and the npm package manager for JavaScript. Download and install the latest versions for your OS. In this post I am going to refer to Node.js 6.10.3 and npm 4.6.1.
First of all you need to install the Hubot generator:
npm install -g yo generator-hubot
Then create the directory for your first Hubot:
mkdir firstbot
and generate the bot instance through the yeoman generator:
cd firstbot
yo hubot
At creation time you will be asked for some information: the bot owner, the bot name, a description for it and the adapter to use. An adapter is the interface to the service you want your Hubot to run on. Hubot provides two official adapters, Shell and Campfire, but several third party Open Source adapters are available for the most popular chat services (Slack, XMPP, Facebook Messenger, etc.).
Now you can start the bot. The start script has been generated in the bin directory inside the bot home. Run
./bin/hubot
and the bot is ready to interact with you in a command shell. You can check for a list of the available commands by typing
firstbot help
Let's see now how it is possible to implement and add a custom script (scripts give really power to a bot). A script must export at least one function. So the first line of code (excluding comments) has to be the following:
module.exports = (robot) ->
Then you can start to add your code. The most common interaction between the bot and humans is based on messages, so the hear and respond methods are the most used:
robot.hear /badger/i, (res) ->
# your code here
robot.respond /open the pod bay doors/i, (res) ->
# your code here
It is possible, through regular expressions, to capture content from the input messages using res.match. Example:
robot.respond /open the (.*) doors/i, (res) ->
doorType = res.match[1]
if doorType is "pod bay"
res.reply "I'm afraid I can't let you do that."
else
res.reply "Opening #{doorType} doors"
The bot can do more complex things, like HTTP requests for example:
robot.http("https://midnight-train")
.get() (err, res, body) ->
# your code here
Here's an example of HTTP request to a Jenkins server to get the results of the unit test for a given execution of a build job by parsing the JSON content of the response:
robot.hear /Unit tests status for (.*) build number (.*)/i, (res) ->
buildJobTestResultUrl = res.match[1] + res.match[2] + "/testReport/api/json?pretty=true"
res.robot.http(buildJobTestResultUrl)
.header('Accept', 'application/json')
.get() (err, response, body) ->
data = null
try
data = JSON.parse(body)
res.send "Test results: #{data.passCount} passed; #{data.failCount} failed; #{data.skipCount} skipped."
#res.send "#{body} content."
catch error
res.send "Ran into an error parsing JSON :( #{error}"
return
The first match there is the build job URL and the second one is the build number. The output by the bot would be like this:
Test results: 32 passed; 0 failed; 0 skipped.
Once you have completed a script implementation, in order to register it you have to save it with the .coffee extension in the scripts directory of the bot home and then restart the bot to use it.
The process of implementing bots in Hubot and enhancing them through scripts is pretty straightforward. Furthermore there are several hundreds available scripts ready to be installed through npm: so please check that list before implementing anything.
I will share next more interesting scripts and tips on Hubot.
Comments
Post a Comment