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

Streamsets Data Collector log shipping and analysis using ElasticSearch, Kibana and... the Streamsets Data Collector

One common use case scenario for the Streamsets Data Collector (SDC) is the log shipping to some system, like ElasticSearch, for real-time analysis. To build a pipeline for this particular purpose in SDC is really simple and fast and doesn't require coding at all. For this quick tutorial I will use the SDC logs as example. The log data will be shipped to Elasticsearch and then visualized through a Kibana dashboard. Basic knowledge of SDC, Elasticsearch and Kibana is required for a better understanding of this post. These are the releases I am referring to for each system involved in this tutorial: JDK 8 Streamsets Data Collector 1.4.0 ElasticSearch 2.3.3 Kibana 4.5.1 Elasticsearch and Kibana installation You should have your Elasticsearch cluster installed and configured and a Kibana instance pointing to that cluster in order to go on with this tutorial. Please refer to the official documentation for these two products in order to complete their installation (if you do

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

Using Rapids cuDF in a Colab notebook

During last Spark+AI Summit Europe 2019 I had a chance to attend a talk from Miguel Martinez  who was presenting Rapids , the new Open Source framework from NVIDIA for GPU accelerated end-to-end Data Science and Analytics. Fig. 1 - Overview of the Rapids eco-system Rapids is a suite of Open Source libraries: cuDF cuML cuGraph cuXFilter I enjoied the presentation and liked the idea of this initiative, so I wanted to start playing with the Rapids libraries in Python on Colab , starting from cuDF, but the first attempt came with an issue that I eventually solved. So in this post I am going to share how I fixed it, with the hope it would be useful to someone else running into the same blocker. I am assuming here you are already familiar with Google Colab. I am using Python 3.x as Python 2 isn't supported by Rapids. Once you have created a new notebook in Colab, you need to check if the runtime for it is set to use Python 3 and uses a GPU as hardware accelerator. You