How may I help you?

Introduction:

What are chatbots? Why are they such a big oppurtunity? How do they work? How can I create one? All of these questions are going to get answered soon. “~90% of our time on mobile is spent on email and messaging plat – forms. I would love to back teams that build stuff for places where the consumers hang out!”

– Niko Bonatsos, Managing Director at General Catalyst

What is a chatbot?

A chat bot is a service, powered by rules and sometimes arti- ficial intelligence, that you interact with using a messaging interface. The service could be any number of things, ranging from functional to fun and it could be live in many chat chan- nels (Facebook messenger, Slack, Telegram, Vine, Viber etc) For example, suppose you are willing to do some transaction via a bank. So you have to go to their site and perform the neces – sary actions. But what if you could just go to Facebook and use the same messenger that you use to talk to your friends to tell a bot to do that transaction. That is where chatbots are taking the lead. You don’t have to find any links in the website. You have to just tell the bot what to do in basic language. Like any other conversation you do with a service executive but completely automated. This indeed mirrors the experience of going to the bank branch but without any sort of human intervention. Not only does this reduce the scope of errors, but allows companies to put their man hours to better use.

What are the different example of chatbots? Some use cases please.

Apart from what is mentioned above the chatbots can be used anywhere – here are couple of examples of the same:

  • Weather Bot – hiponcho – Get weather report whenever you ask.
  • News bot – CNN – Ask it to tell you about something when – ever something interesting will happen.
  • Banking Bots – ICICI Bots – Solves some FAQ Problem
  • Forever Alone’s Helping Hand – Xiaoice – NLP Based bot in China with which 20 million people talk to.

Why chat bots are going to take over everything?

  1. Every business is going to have a messaging bot beacause messaging apps will become the #1 way to communicate with other people in near future.
  2. Bots will be faster than websites and mobile apps.
  3. Bots are easier to use because of NLP. You don’t need to find – just ask.

How can I create my own chatbot?

There are many platforms where you can build your own chatbot. They provide great user interface, drag-drop facility or an IDE to code your bot. Indian chat bot market is growing rapidly and people are engaging themselves to create more user-friendly ways to create chatbots. Below are the list of few companies those are doing great in chatbot market.

  • Gupshup – Raised $22m funding in the year 2011.
  • Glomantra – Raised $1.6m in 2008
  • CogniCor – Raised $1.07m in 2011

But can I make my bot intelligent enough to make it respond to any sort of queries?

If you want to create a simple chat bot that can do with exact word to word matching, that’s not too difficult – but to under- stand the intent of the user and then progressing with the desired path of communication is the best way to go ahead. For this you are going to get help from various AI based platforms that are there to help you on this. If you are interested you can go and learn about

  • api.ai
  • wit.ai

Hubot

What is Hubot?

Hubot is a chatbot developed by GitHub, Inc to automate their everyday work that followed some specific steps to finish up. It also was a source of fun. Eventually they rewrote it and made it a open-source platform to develop bots. Today’s version of hubot are written on coffescript (that doesn’t mean you have to know coffeescript to learn this) on Node.js and can be easily deployed using Heroku. More importantly hubot is a standard – ized way to share between everyone’s bots.

Why do I need hubot?

Hubot in you company’s chat room can help to perform many automation and monitoring related tasks, which you would have to do manually otherwise, undertaking steps that can be avoided with a bot . Yes, I know you can write program for that but hubot is also a program but in that you communicate via natural language – isn’t that better?

Pre-requisites

As hubot is written on node.js you need to know about Node. js in a very basic level. Nothing else is required.

Getting started

You will need node and npm to install hubot. Once these two are properly installed then we can work with the hubot yeoman generator.

> npm install -g yo generator-hubot

Now from this we can create a folder and generate a new instance of hubot in it.

> mkdir jarvis

> cd jarvis

> yo jarvis

At this moment you are going to asked some question, like who is creating the bot and what adapter are you going to use. Adapters are hubots way to integrate with chat providers. Please note – don’t name your bot the ‘default hubot’. This will break your application. The cause is not clear – may be some unidentified bug that hasn’t been solve yet. And that’s all. Nothing else is required to setup your first bot. Now you can start up hubot by,

> bin/hubot

{{your_bot_name}}>

Now hubot comes with a set of pre-installed scripts, along with that there is a complete community of developers who are developing scripts for hubot. You can use NPM search term to search the npm registry > npm search hubot-scripts {{your_search_term}}

Adapters

Adapters are the sets of code that allows your bot to inte – grate with different platforms. Hubot comes with two official adapters and quite a handful of third-party adapters including all of the popular channels.

Writing your own custom Script for hubot

Hubot out of the box doesn’t do too much but it is an extensible, scriptable robot friend. There are hundreds of scripts written and maintained by the community and it’s easy to write your own. You can create a custom script in hubot’s scripts directory or create a script package for sharing with the community!

How to Script in hubot?

When you created your hubot, the generator also created a scripts directory. If you peek around there, you will see some examples of scripts. For a script to be a script, it needs to:

  • live in a directory on the hubot script load path (src/scripts and scripts by default)
  • be a .coffee or .js file
  • export a function

By export a function, we just mean:

module.exports = function(jarvis){

/* Your bot code goes here */}

Each hubot script is basically a node module and the export function is bot’s behivour descriptor. In the export function you will get the bot object as a function argument. This bot object can be used to do various tasks. Here jarvis resembels the bot object. This object contains various utility method like,

  • hear – will listen to all messages sent and check whether it matches before firing the callback
  • respond – will only analyze the message if only it is directly addressed to jarvis. i.e. starts with your hubot’s name “jarvis what’s the weather of Kolkata”.

The callback function will have a mes – sage object and using that, the user can do one of the following three things:

  • send – Sends a message to the chat
  • reply – replies to the user directly
  • emote – those familiar with IRC will know this to be equivalent to /me – kind of like an inline status message, and will work only where such a message is supported.

Hear and Respond

Since this is a chat bot, the most common interactions are based on messages. Hubot can hear messages said in a room or respond to messages directly addressed at it. Both methods take a regular expression and a call – back function as parameters. For example:

alfred

jarvis.hear(“hello”, function(jar_resp){

jar_resp.send(“Hello

”.concat(process.env.USER));

});

This will indeed be a basic hi-hello bot, where in hearing hello the bot will respond with the message Hello {{user_name}}.

jarvis.respond(/make me

coffee/i, function(jar_resp){

jar_resp.send(“Making

you coffee”):

});

The

robot.respond /make me

coffee/i

callback is only called for mes- sages that are immediately preceded by the robot’s name or alias. If the robot’s name is JARVIS and alias is /, then this callback would be triggered for:

  • jarvis make me coffee
  • JARVIS: make me coffee
  • @JARVIS make me coffee
  • /make me coffee

It wouldn’t be called for:

  • Jarvis please make me coffee.

Send & reply

The jar_message parameter is an instance of Response (historically, this parameter was msg and you may see other scripts use it   this way). With it, you can send a message back to the room from where the jar_ message came from, emote a message to a room (If the given adapter supports it), or reply to the person that sent the message. For example:

module.exports = function(jarvis){

jarvis.hear(/badger/i,

function(jar_resp){

jar_resp.

send(“Badgers? BADGERS? WE DON’T

NEED NO STINKIN BADGERS”);

});

jarvis.respond(/make me

coffee/i, function(jar_resp){

jar_resp.reply(‘Yes ‘ +

process.env.USER + ‘ making you coffee’);

});

jarvis.hear(/I like

pie/i, function(jar_resp){

jar_resp.

emote(‘makes a freshly baked pie’);

});

}

The robot.hear /badgers/callback sends a message exactly as specified regard – less of who said it, “Badgers? BADGERS? WE DON’T NEED NO STINKIN BADGERS”.

If a user Dave says “HAL: make me

coffee”,

robot.respond /make me

coffee/i

callback sends a message “Yes

Agnibha making you coffee.”

Capturing data

So far, our scripts have had static responses, which, while amusing, are boring func – tionality-wise. Res.match has the result of matching the incoming message against the regular expression. This is just a JavaScript thing, which ends up being an array with index 0 being the full text matching the expression. If you include capture groups, those will be populated res.match . For example, if we update a script like:

jarvis.respond(/make me (.)+/i,

function(jar_resp){

drinkType = jar_resp.match(1);

switch(drinkType){

case “coffee”:

jar_resp.

send(“Yes Making you brazillian Coffee”);

break;

case “tea”:

jar_resp.

send(“Making you darjeeling tea”);

break;}});

Making Http Calls

Hubot can make HTTP calls on your behalf to integrate & consume third party APIs. This can be through an instance of node-scoped-http-client available at robot.http. The simplest case looks like:

jarvis.http(“https://midnight-train”).get()

(function(err, res, body) {});

A post call looks like:

var data;

data = JSON.stringify({

foo: ‘bar’});

jarvis.http(“https://midnight-train”).header(‘Content-Type’,

‘application/json’).post(data)(function(err, res, body) {

/* Your bot code here */

});

err is an error encountered on the way, if one was encoun- tered. You’ll generally want to check for this and handle accord – ingly:

jarvis.http(“https://midnight-train”).

get()(function(err, res, body) {

if (err) {

res.send(“Encountered an error :( “ + err);

}});

res is an instance of node’s

http.ServerResponse.

Most of the methods don’t matter as much when using node-scoped-http-client, but of interest are sta- tusCode and getHeader . Use statusCode to check for the HTTP status code, where usually non-200 means something bad happened. Use getHeader for peeking at the header, for example to check for rate limiting:

jarvis.http(“https://midnight-train”).

get()(function(err, res, body) {

var rateLimitRemaining;

if (res.statusCode !== 200) {

res.send(“Request

didn’t come back HTTP 200 :(“);

return;}

if (res.getHeader(‘X-RateLimit-Limit’)) {

rateLimitRemaining =

parseInt(res.getHeader(‘X-RateLimit-Limit’));}

if (rateLimitRemaining && rateLimitRemaining < 1) {

return res.send(“Rate

Limit hit, stop believing for awhile”);}});

body is the response’s body as a string, the thing you prob- ably care about the most:

jarvis.http(“https://midnight-train”).

get()(function(err, res, body) {

return res.send(“Got back “ + body);});

The Hubot Library also supports various other things like custom listeners, creating a topic and a room where you can chat with multiple users. You can have http listeners, persist data and do a lot more stuff.

Combining all of the things we have learned till now, let us create a weather bot that will tell us the average temparature of a place that we are asking for. The key phrase for our bot will be “weather of {{place_name}}”. You can further extend this code by adding nlp based parsing support and create intents based on that. Here I’m using openweathermap’s api for my purpose.

module.exports = function (jarvis) {

jarvis.hear(/weather of (\w+)/i, function (jar_resp) {

var city_name = jar_resp.match[1];

var url = “http://api.openweathermap.org/

data/2.5/weather?q=”.concat(city_name,

“&appid=”, api_key, “&units=metric”);

jarvis.http(url).get()(function (err, res, body) {

jar_resp.send(“The average temparature of “ + city_name

+ “ at this moment is “ + JSON.parse(body).main.temp + “°C”);

});

});

};

Conclusion

The bot space is currently growing in the world and many other players are there to help you with your bot making. This is one way to make bots but other companies like Gupshup, Glomantra, Congnicor, Magicx, Arya.ai etc are helping users to make bot without having the knowledge of any language and

deploy across multiple channel with the help of a button click. And pretty soon, almost every conceivable action that needs you to interact with a human being to obtain certain services – it will all be done by bots before you know it.

Leave a Comment

Your email address will not be published. Required fields are marked *