Skip to main content

Started playing with Hubot

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:

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

Popular posts from this blog

Exporting InfluxDB data to a CVS file

Sometimes you would need to export a sample of the data from an InfluxDB table to a CSV file (for example to allow a data scientist to do some offline analysis using a tool like Jupyter, Zeppelin or Spark Notebook). It is possible to perform this operation through the influx command line client. This is the general syntax: sudo /usr/bin/influx -database '<database_name>' -host '<hostname>' -username '<username>'  -password '<password>' -execute 'select_statement' -format '<format>' > <file_path>/<file_name>.csv where the format could be csv , json or column . Example: sudo /usr/bin/influx -database 'telegraf' -host 'localhost' -username 'admin'  -password '123456789' -execute 'select * from mem' -format 'csv' > /home/googlielmo/influxdb-export/mem-export.csv

jOOQ: code generation in Eclipse

jOOQ allows code generation from a database schema through ANT tasks, Maven and shell command tools. But if you're working with Eclipse it's easier to create a new Run Configuration to perform this operation. First of all you have to write the usual XML configuration file for the code generation starting from the database: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-2.0.4.xsd">   <jdbc>     <driver>oracle.jdbc.driver.OracleDriver</driver>     <url>jdbc:oracle:thin:@dbhost:1700:DBSID</url>     <user>DB_FTRS</user>     <password>password</password>   </jdbc>   <generator>     <name>org.jooq.util.DefaultGenerator</name>     <database>       <name>org.jooq.util.oracle.OracleDatabase</name>     ...

Turning Python Scripts into Working Web Apps Quickly with Streamlit

 I just realized that I am using Streamlit since almost one year now, posted about in Twitter or LinkedIn several times, but never wrote a blog post about it before. Communication in Data Science and Machine Learning is the key. Being able to showcase work in progress and share results with the business makes the difference. Verbal and non-verbal communication skills are important. Having some tool that could support you in this kind of conversation with a mixed audience that couldn't have a technical background or would like to hear in terms of results and business value would be of great help. I found that Streamlit fits well this scenario. Streamlit is an Open Source (Apache License 2.0) Python framework that turns data or ML scripts into shareable web apps in minutes (no kidding). Python only: no front‑end experience required. To start with Streamlit, just install it through pip (it is available in Anaconda too): pip install streamlit and you are ready to execute the working de...