Implementing Chat Bots - A comparison of frameworks
???? Jairo Andrés Correa Pérez
Head of Applications Management & Infrastructure @Endava Colombia
Picture this: It’s 1976 and you are a student in some university that has access to a PDP-10 Mainframe, one of those room size computers, with 1MHz top speed and 36 bit architecture. There’s a buzz in the hallways about a new game called ADVENT (It was actually called Colossal Cave Adventure, but the file name was only 6 character long) where you can use plain English commands to explore a cavern and find a treasure. It was written in roughly 700 lines of FORTRAN and some other Mainframe code.
ADVENT was one of the first of many things: the first interactive fiction, one of the first text adventure games and the prelude to the Adventure game genre. For me it’s also one of the first Chat Bots*. It inspired many game developers like the ones who wrote the Atari 2600 Adventure, and it’s on the top of the charts for us abandonware lovers.
A simple definition of Chatbot is a computer program which can emulate a conversation. The concepts of developing them have been around for years, but the expansion of mobile networks and messaging platforms have turned technology eyes back into conversation based interfaces. Statistics show that messaging it’s becoming fast the preferred form of communication to anyone with a cellphone.
“Luckily” we don’t have to use FORTRAN to create an scripted conversation. There are many frameworks available to help us create conversational agents. Here I’m going to focus on 3: Hubot, BotKit and Errbot.
Hubot
Hubot was created internally by they teams at Github.com. They rewrote it and made it open source, and the community has helped it become one of the most popular platforms. It’s written in Node.js and the execution scripts are in CoffeeScript.
Installation was simple, as the project has adopted Yeoman as the scaffolding tool, installation was ready with a couple of commands:
npm install -g yo generator-hubot
mkdir myhubot
cd myhubot
yo hubot
I’m using Slack as the channel to test these frameworks, and Slack embraced Hubot’s popularity making it fast to connect to your workspaces. It could had taken me less than 5 minutes to have it up and running. A Hubot script looks like this:
rules = [
"0. A robot may not harm humanity, or, by inaction, allow humanity to come to harm.",
"1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.",
"2. A robot must obey any orders given to it by human beings, except where such orders would conflict with the First Law.",
"3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law."
]
otherRules = [
"A developer may not injure Apple or, through inaction, allow Apple to come to harm.",
"A developer must obey any orders given to it by Apple, except where such orders would conflict with the First Law.",
"A developer must protect its own existence as long as such protection does not conflict with the First or Second Law."
]
module.exports = (robot) ->
robot.respond /(what are )?the (three |3 )?(rules|laws)/i, (msg) ->
text = msg.message.text
if text.match(/apple/i) or text.match(/dev/i)
msg.send otherRules.join('\n')
else
msg.send rules.join('\n')
Many of the community plugins work like that, a regex evaluation to trigger some response. There’s an impressive amount of scripts out there, but if you can’t find what you need, CoffeeScript isn’t hard to extend if you know a little bit of JavaScript and have some googling skills. There are many listings to hubot scripts like this one https://bit.ly/2xUoWl4 , from very interesting complex worfklows solvers, to some that just add a little bit of personality to your bot.
Botkit
Botkit is a product of the teams of Howdy.ai, but has an MIT license. I really like the way they approach the problem of creating chatbots in this statement: “Bots can hear things, say things and reply to what they hear”.
In that line of thought, Botkit is written in Node.js and their methods reflect how they approach the solutions. A sample script looks like this:
module.exports = function(controller) {
controller.hears(['color'], 'direct_message,direct_mention', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.say('This is an example of using convo.ask with a single callback.');
convo.ask('What is your favorite color?', function(response, convo) {
convo.say('Cool, I like ' + response.text + ' too!');
convo.next();
});
});
});
I had to struggle a little more with the Slack connection, but it’s well documented. You have to set up OAuth, permissions, and I had to use ngrok to proxy requests to my laptop as Slack needed a webhoook. It gave me a good insight of the inner workings of Slack but had nothing to do with Botkit itself.
They offer a couple of alternatives to host your bot, which can be useful on some cases. Microsoft, IBM, Api.Ai and other major players are working to allow their platforms to be easely deployed on BotKit, and Howdy.ai have some additional offerings to help your business. I actually heard about Botkit the first time as a deploy option for Watson Conversation Service.
Errbot
Errbot is written in python and that gives a more “pythonic” style in scripts. I would say, for the documentation style and the scripts I found, that it’s a bot made by developers for developers. That basically means that documentation is good, but it has a low level feeling to the whole operation that appeals to other IT professionals. It’s feels more practical, and even though you can make pretty much everything the other platforms allow, it has some of that coding purity python has. I think it appeals to all of those who prefer VIM to any other GUI based editor.
To be fair, installation could be among the easiest with a simple pip command. I was stubborn and tried to make virtualenv work on a Windows 10 laptop, fixing environment variables and paths, but resigned that path and created a dockerfile to run Errbot inside a python base container. It had a bug, preventing me to run it on text mode, but when configured to run with Slack as backend, it ran smoothly.
A sample plugin in Errbot looks like this.
from errbot import BotPlugin, botcmd, arg_botcmd, webhook
class Sample(BotPlugin):
@botcmd(split_args_with=None)
def glover(self, message, args):
return "I'm too old for this ****"
This sample was extended on a template created by a wizard. The great thing about it is that your documentation is part of the !help command once you publish it.
As with Hubot, you can find all sorts of plugins for many different actions. One of the good features out of the box is the possibility of administer and install new actions by the same chat interface with commands available to administer roles.
Final Thoughts
All these frameworks allow us to create chatbots by abstracting the problem into classes and methods that help us focus on the implementation rather than how to connect to back end messaging systems. It helps us to ease our way into creating conversation agents for many use cases, and stop worrying about the channels to engage conversations.
My personal feeling is that both errbot and hubot evolved for ChatOps, a term popularized to describe IT Operations teams working together through messaging, in a very DevOps fashion to reduce waste time in communications. Both seem to be more mature to work with command like conversations, and even though they can be tweaked to use some Natural Language Processing, the community has created tons of plugins to tackle sysadmins daily struggles in structured commands. The real choice here seems to be if your teams preffer CoffeeScript over Python.
Botkit felt a little more featured to engage in more conversational style of interactions. It’s becomming popular to use cases outside the IT scope, and it’s being featured for many AI companies. I only used Slack integrations, but for Botkit felt a bit more ready to tackle End User based architectures via webhooks. NLP seems also very
Finally, all three were pretty well documented and I found no excuse to start using them on my daily work. I would choose Hubot for my DevOps daily work just because I want to learn more Javascript, and Botkit if I need some conversational agent and with some NLP, probably on more engaging platforms like Facebook.
So, what's stopping you from building conversation interfaces into your apps? Let me know your thoughts.
* Eliza, developed by Joseph Weizenbaum in 1964 for the IBM 7094 at MIT is one of the first documented “chatterbots”. ADVENT code can be found here https://jerz.setonhill.edu/intfic/colossal-cave-adventure-source-code/