Today, I am going to teach you guys how to build a chat bot using Hubot. So, what is a Hubot? Hubot is a chat bot, modeled after GitHub’s Campfire bot. He is really cool. He can do a lot of things. For example, sending a message to someone as soon as they enter the chat room, or queries for the status of AWS services, or automate your deployment process, or display app performace status from New Relic, or warn the developer whenever a build failed in git, and many more! He is extendable with scripts and can work on many different chat services. In order to keep this post short and simple, we are not going to cover how to install hubot on your machine. Instead, we are going straight into the implementation on how to build a simple chat bot using Hubot. For people who aren’t familar with setting up the environment for hubot, you may check out this YouTube video as it gives step by step on how to install hubot from scratch. Okay, that’s get it started. The following code is the hello world version of using Hubot.

Hello world version of Hubot

  module.exports = (robot) ->
    robot.hear /hi/i, (res) ->
      res.send "Hello there"

In the chat room, a possible chat would be like this,

chat example for hello world version

Pretty simple hul?!
But, I have a question through. Is it possible that Hubot can grab what I just typed like when I said “hi Hubot”, can the Hubot get the word “Hubot”?
Yes, indeed. You can do that. Just watch!

Example on how to pass parameter to hubot

  module.exports = (robot) ->
    robot.hear /hi hubot my name is (.*)/i, (res) ->
      personName = escape(res.match[1])
      res.send "Hello " + personName

In the chat room, a possible chat would be like this,

chat example for pass parameter to hubot

Not bad hul?!
Okay, now before we begin. I would like to introduce an amazing REST API that we will be using is called Genderize.io. A REST API that determines the gender of a first name which will help us to make this tutorial a lot easier. Thanks to Genderize.io. The REST API that we will be calling is https://api.genderize.io. An example on how this would be used.

Example on GET request to https://api.genderize.io

  GET https://api.genderize.io/?name=peter

Example on the response for https://api.genderize.io/?name=peter

  {"name":"peter","gender":"male","probability":"1.00","count":796}

Now, with this REST API. We can easily build a Hubot that allow the user to ask them if a given first name is a male or a female.

Example on calling a GET request in Hubot

  module.exports = (robot) ->
    robot.hear /check gender for (.*)/i, (res) ->
      personName = escape(res.match[1])
      res.http("https://api.genderize.io/?name=" + personName)
        .get() (error, response, body) ->
        try
          json = JSON.parse(body)
          res.send "Probability of " + "#{json.probability}" + " that " + personName + " is a " + "#{json.gender}.\n"
        catch error
          res.send "something went wrong..."

source code hosted on GitHub

In the chat room, a possible chat would be like this,

chat example for checking gender for peter

or

chat example for checking gender for belle

Normally, the higher the probability, the greater chance that the response is correct!

Wrapping Up

Hopefully this guide has given you the confidence to do things with Hubot. If you would like to learn more, please go to their offical site for more example. I am sure that you will start getting the hang of it while you start playing around with Hubot. I hope that this post has helped you and thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started