Putting your bot on #Slack
The Hubot project headed by Github is a highly extensible bot application used to automate and simplify development tasks. Hubot acts as a member of a chatroom, reading and posting messages of its own.
Unlike your human teammates, however, Hubot is backed by a server, and can do things like retrieve information from a database or post a message when a particular build is finished. While some Hubot tasks might be automated, others can be performed in response to messages posted by other users in the chatroom:
some-member: @hubot how many users are logged in right now?
hubot: There are currently 70 logged in users.
In the above interaction, Hubot’s message was generated by a function that made a query for all logged in users. In a Hubot script, you could then define a “listener” that performs this query when Hubot reads a particular message.
hubot.respond('how many users are logged in right now?'), (msg) -> // perform the query
Wouldn’t it be great if Hubot could understand other forms of this type of question? That’s why we’ve written a script that allows you to talk to a Pandorabot via a Hubot. Now you can harness the NLP capabilities of AIML to reduce a large number of inputs to a single query. This allows you to create expressive listeners for Hubot that leverage the power of AIML pattern matching.
<pattern>HOW MANY USERS # LOGGED IN #</pattern>
<template><srai>CURRENT USER QUERY</srai></template>
Today, we’ll set up a simple event listener for Hubot that forwards messages to Pandorabots, and returns the Pandorabot’s response.
You’ll need a few things in place before you can start with the integration:
- A Slack team and personal account. If you don’t already have this, Slack’s sign up procedure will guide you through the process.
- An account on Heroku. There are different payment plans, however, the free tier should be more than enough for your Hubot instance.
- The Heroku toolbelt, for which Heroku offers installers for OS X, Windows, and Ubuntu. This is a set of command line tools that allow you to easily work with your applications deployed to Heroku.
- A Pandorabot deployed on AIaaS.
- Node.js, for which there are installers for OS X and Windows, as well as the raw source.
You’ll ultimately need to host Hubot on a server of some sort. Hubot comes packaged with some code to make it very easy to deploy to Heroku. Since Heroku does not offer a secure shell like Amazon or Google, you’ll be doing all your development locally before actually deploying your application.
Redis is an in-memory key-value cache and store that Hubot requires for persistant memory. For local development, you’ll need to install redis on your machine.
$ brew install redis
$ sudo apt-get install redis-server
Unfortunately, Redis does not officially support windows, however, you can try using this port for Win64 created by MSOpenTech.
Create a new Hubot instance
There is a Yeoman generator that allows you to scaffold new Hubot projects.
$ npm install -g yo generator-hubot
By running the yo command, you can tell Yeoman you’d like to create a new Hubot project.:
$ mkdir mybot
$ cd mybot
mybot$ yo hubot
This will prompt you to answer a few questions about your bot. When prompted for the “Bot adapter” override the default selection by entering slack.
Once the prompts have completed, you should see that Yeoman has created a number of files and folders. To make sure your bot has been properly configured, you can run the binary from inside the mybot directory:
mybot> hubot ping
The command will print out some information about your instance of Hubot, and then present the Hubot shell adapter, which is a chat like interface that allows you to talk to Hubot from the command line.
Install Pandorabots script
Hubot operates on user-defined “scripts”, which tell Hubot to perform tasks when certain commands are issued to it. We have provided a script that allows you to forward inputs to Hubot to your Pandorabot. You can install the script using npm, along with a few dependencies:
mybot$ npm install --save pb-hubot
You’ll also want to add the Pandorabots script to the array in external-scripts.json:
In many of our other tutorials, we’ve stored Pandorabots API credentials in JSON files or as variables. But for our use on a server, we’re actually just going to set these as environment variables for Hubot to search for. Grab your app_id and user_key from the Developer Portal, and pick out one of your already deployed bot names. Make sure you are in the mybot directory:
mybot$ export app_id=**********
mybot$ export user_key=********
mybot$ export botname=*********
You are now ready to test your Pandorabot enabled Hubot from the shell interface. Run the binary, and try saying something to Hubot that you’ve programmed your Pandorabot to understand. Note that in the current implementation you must enter the pb prefix so Hubot knows to route the input to Pandorabots:
mybot> hubot pb hello
This response is just an example; your bot may have different responses depending on what categories it contains, and you can program your bot to know anything from company inside jokes to FAQs, etc.
Deploy to Heroku
Now that you’ve successfully configured Hubot to use the Pandorabots adapter along with one of your bots on chatbots.io, it is time to deploy Hubot to Heroku so that it may run continuously.
If you’ve signed up for a Heroku account and have the Heroku Toolbelt installed, you can login from the command line:
$ heroku login
You’ll be prompted for your credentials, and if the authentication succeeds, you’ll be able to make requests to Heroku without having to re-enter your information later.
Setup Heroku app
Heroku uses git for deployment. In other words, you can create a local repository and deploy your changes to Heroku using git push. From inside the mybot directory:
mybot$ git init
mybot$ git add .
mybot$ git commit -m 'first commit'
You can now initialize a Heroku app:
mybot$ heroku create
Because of the Hubot’s Redis dependency, you’ll need some instance of Redis running online for your Heroku application to connect to. Heroku offers easy setup with RedisToGo, a database-as-a-service that has a free plan to get you started:
mybot$ heroku addons:add redistogo:nano
You’ll also need to redefine your environment variables on Heroku:
mybot$ heroku config:set app_id=$app_id
mybot$ heroku config:set user_key=$user_key
mybot$ heroku config:set botname=$botname
To hook Hubot up with your Slack chatroom, you’re going to need an API token from Slack. From the Integrations page on Slack, add a new Hubot integration, copy the API token, and set it as an environment variable on Heroku:
mybot$ heroku config:set HUBOT_SLACK_TOKEN=xoxb-1234567890-XXXXXXXXXXXXXXXXXXX
Finally, let’s add one more environment variable to Heroku so that our Hubot never goes to sleep. First, get your application’s URL by running heroku apps:info, then:
mybot$ heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=xxx-yyyy.herokuapp.com
Push and test with Slack
You are now ready to deploy your Hubot to Heroku. This is as easy as:
mybot$ git push heroku master
And that’s it! If you visit your team’s Slack page, you should see that your bot has been added as a member. Try talking to the bot in the #general channel (Hubot uses this by default).
you: mybot pb hello!
mybot: Hi there!
You now have a fully functioning version of your bot deployed to a chatroom, able to converse with other members.
This is, however, just the beginning. As a next step, you might experiment with using <oob> tags in your bot’s response to actually provide further commands to Hubot. By combining the programmatic capabilities of Hubot and the conversational AI and NLP features of Pandorabots, you can create a variety of awesome chatbot applications… like an app for querying a database in natural language.
Stay tuned, and happy Hubotting!
Originally published at blog.pandorabots.com on April 6, 2015.