Hubot tips & tricks

Lukasz Lenart
SoftwareMill Tech Blog
3 min readApr 3, 2018

--

I would like to share three hacks that you should consider when implementing a bot using Hubot. They can make your daily use of Hubot a better experience.

Photo by Matan Segev from Pexels

What’s Hubot?

Hubot is a bot created by Github which can be used with broad number of chat platforms. You can use it with Slack, Campfire, and many others. All you need to do is to use a proper adapter.

There is also a plenty of ready to use scripts that can be easily installed and they will extend Hubot’s functions. You can choose from simple scripts that will allow Hubot posts images to scripts that can handle integration with Jenkins or Target Process.

Obviously, Hubot is written in CoffeeScript and you can use the same language to write your own scripts or use a pure vanilla JavaScript.

Tip #1

First and the most important tip when implementing your own script is to use a .finish() function once a command was matched and handled by the bot.

Matching message as done allows to catch commands addressed to the bot that weren’t properly handled. This gives you an opportunity to add some more advanced functions like using a Natural Language Processing service to support users. I use this technique to integrate our hiring bot with IBM Watson.

So, call .finish() as soon as the bot was able to match the user’s command:

Tip #2

This tip is very specific to Slack but you can use it with your chat platform of choice as well. Just check API of the platform to see what JSON you need to prepare to enable additional functionality of your chat.

In the simplest form, Hubot can send only a text message when responding to a user action. This can be easily overcome by preparing a custom JSON and sending it as an answer instead of the text message. In this case, Hubot’s Slack adapter is using achat.postMessage method to send messages to the Slack API. Investigating a description of the method you see that you can use attachments to send more complicated reponses than just a text.

Here is an example of using the attachments to generate a list of items in a Slack way:

and the result looks like this:

instead of a bare list of links which just looks bad:

Tip #3

Hubot allows to send a message to itself. So you can catch a message, change it, and send it back to Hubot to process it again. I use this technique to allow the bot to react on different names, not just the one used when the bot was started.

All you have to do is to define a matcher that will catch all the messages starting with an alias, then replace the alias with a real bot’s name and send it back. See the example below:

and now you can use the alias when you want to interact with the bot:

Comments are welcome

I would like to hear your ideas and tips & tricks you use to improve your Hubot powered bot. Please do not hesitate to contact me directly or post a comment below the post. Also when you have an idea how to improve the presented tips. Maybe yours will be better than mine :)

--

--

OSS enthusiast, ASF committer, Apache Struts lead, developer, husband and father and biker :-)