Hubot - a Siri to call my own

| Jan 9, 2016

I have always been interested in how these automated personal assistants: Siri, Google Now, Cortana etc. actually function. Aside from the speech recognition algorithms, the meaning of the sentence spoken to these programs must be deciphered and then acted upon pretty quickly. ‘Trigger’ words are clearly important here e.g. “give me directions to…” but the breadth of words that a computer must understand in order to appear human is simply vast.

Recently, I have been getting pretty into Slack and stumbled across this bot developed by the guys at GitHub called hubot. Hubot, is a robot who you talk to and give commands through a chat-interface, such as slack! Hubot can be setup to run locally, or pushed to an online application hosting service such as Heroku. The great thing about hubot is that it is completely open source and can be expanded by writing simple CoffeeScripts (simplified JavaScript). Here I will explain the basic installation and setup of Hubot, before writing basic scripts that can be executed.

hubot

Getting Started With Hubot

First things first, install nodejs and the nodejs package manager npm. The best way to install these is through HomeBrew, possiblity the greatest package manager for OS X.

brew install nodejs
brew install npm

With nodejs and npm installed, we can pull down and install hubot. Running the below command will pull down a hubot installer and run a whole bunch of system tests to ensure that the local system can run a bot.

npm install -g yo generator-hubot

Now we need to create a directory to hold our bot.

mkdir my-hubot-directory
cd ./my-hubot-directory
yo hubot

Now, our new hubot will ask a whole bunch of setup questions about who it is and what its purpose is. The best thing to do next is to turn this directory into a git repository so we can track changes, and lets face it, everything should be under version control and pushed to GitHub.

git init
git add --all
git commit -m 'initial commit'

Here is where the fun starts.

./bin/hubot
hello
# Well hello there, <name>

How cool is that!!

Custom Scripts

The great thing about hubot is that it is infinitely customizable and dead easy to write new scripts. To begin, as is tradition, lets create a ‘Hello, World!’ script. Within the ./scripts directory create a file called hello.coffee with touch or a similar function. Edit the file and include the following:

# Description:
#   Greet the world
#
# Commands:
#   hubot greet - Say hello to the world

module.exports = (robot) ->
  robot.respond /greet/i, (msg) ->
    msg.send "Hello, World!"

Restart hubot (./bin/hubot) and try the following:

hubot help greet
# hubot greet - Say hello to the world
hubot greet
# Hello, World!

For more information on scripts, check out Automation and Monitoring with Hubot by Tomas Varaneckas.

Remote Bot Hosting

So far, I have only discussed implementing hubot locally, but in reality, we don’t want to be running our own machine the whole time, we want the bot to be hosted somewhere remotely and interfaced through a chat client like slack.

Heroku

Introducing, Heroku, the Cloud platform company, that allows you to create, deploy, and manage web applications. What we are going to do is to ‘host’ our hubot instance on a heroku server so that the bot is always running. Luckily, Heroku has a free tier so this also doesn’t cost a penny!

But, before we deploy to Heroku we need to install some software in the form of the Heroku Toolbelt. This will allow us to talk to Heroku’s servers from the command line of our local machine.

heroku create my-own-slackbot

This will create an application on heroku’s servers (you may need to alter the name to be unique). With a skeleton application created, we can access the heroku dashboard to find our app’s URL and use it to set a config flag in our hubots directory:

heroku config:add HEROKU_URL=https://my-own-slackbot.herokuapp.com

Slack Integration

Now, we need to add a hubot ‘integration’ in slack. This will generate a ‘SLACK_TOKEN’ which can be added as a config flag:

heroku config:add HUBOT_SLACK_TOKEN=abcd-1234-5678-91011-12e4fg

Deploy

The most amazing, and logical, aspect of all of this, is that deployment to Heroku is done entirely through git. When we ‘create’ our heroku app locally, it sets up a remote repository which we can ‘push’ changes to.

git remote -v
# heroku  https://git.heroku.com/my-own-slackbot.git (fetch)
# heroku  https://git.heroku.com/my-own-slackbot.git (push)

git push heroku master

That’s it! Now, if we login to slack we should see that our very own hubot is online and ready to play. Yes, I called my bot ‘hal’.

hal
comments powered by Disqus