Chat bots have always seemed so complex. They process natural language from text so it must be hard right? After all, how can you make sense of loose words into computer instructions and then back? It must be hard.

Well, it is hard. Kinda. A lot has already been solved around natural language processing so the amount you have to do to get started has reduced by significant amount. Tools are already there, you just have to use them.

I used those tools and still found it to be difficult. I wanted to make the process of building chat bots as easy as getting started with web development. So, I built Talkify.

Takify is an Open Source framework for building chat bots. It is written in node.js and here’s how you can build your very own chat bot in a couple of minutes.

We’ll be building a chat bot that I like to call “sidekick”. This is a simple bot that tells you knock knock and chuck norris jokes.

The template for this already exists on my GitHub so lets just clone it for now. Assuming you have git command line installed, run:

This will create a folder called talkify-example-sidekick within your current working directory. Go into that folder using the cd command.

You’ll now need to install module dependencies. This should take a couple of seconds depending on your internet connection. Run the following command to tell npm to do this for you.

Now if you list your current directory (using ls if using linux/mac or dir for windows), you’ll notice two JavaScript files. These are index.js and cli.js. The index.js file contains the main bot code while the cli.js  provides an interface to talk the bot via the command line interface. For this tutorial, lets run the cli.js file using node.

This should give you a prompt.

Give it a try. Ask the bot to tell you a knock knock joke. Or ask it to tell you a Chuck Norris joke. You should get responses like so:

If you keep asking for knock knock jokes, it will eventually run out. Try it!

Awesome work! You’ve setup the bot correctly!

Now lets look under the hood inside the index.js to better understand whats going on. First couple of lines in this file are a bunch of requires.

This is like an “import” statement where its initialising libraries like the knock-knock-jokes, chuck-norris-api and talkify.

The next couple of lines are importing template functions from Talkify for us to use later on in the code.

If the previous require statements were loading libraries, this is equivalent to loading specific things that we will be needing from those libraries.

In the next line we are initialising the bot itself using the previously loaded Bot function.

The constructor for Bot can accept an optional configuration object but to keep things simple, we’re just going to use default configuration.

It might look like a lot is happening in the following lines but in essence, we’re simply training the bot to learn a couple of words and associate them with topics. To do that, we are using the trainAll function of the bot and are passing in an array of objects as well as a callback function. Each of these objects are of TrainingDocument type.

As you can see, each TrainingDocument object accepts two parameters. The first parameter is the topic name and the second is text that the bot will learn to associate with the previously mentioned topic.

The callback function that is passed to the trainAll function is executed when the training for the bot has finished. Here, we’re just printing out a message indicating that the bot is ready.

Next, we’re defining two skill objects by utilising the Skill constructor.

As you can see, this accepts three parameters, unique name of the skill, topic to associate the skill with and a function to execute as part of that skill.

This function is called an apply function and it must accept at least three parameters. These are context, request and response.

The context parameter is the context that the bot loads for you automatically. This is request driven context and is associated with an end user.  Think of this as how a web session works.

The request parameter contains metadata about the request like the actual raw sentence in the request, topic resolution confidence level etc.

The response parameter contains a bunch of methods that you can use to set states and respond to the user. More information on this entire section is available in the Skills wiki page.

Within the skill functions we are using the knock knock joke and the chuck norris library to get jokes that the bot can respond with.

Next two lines is where we are making the bot aware of the skills by adding them to it.

The last line exports the bot so that it can be used by other files within this project. We are doing this so that we can separate the bot logic from the logic of getting the raw request from the user and then responding with it. The latter logic in this case can be found from cli.js file.

This means that you can quite easily replace the cli.js file with a web hook from Facebook messenger, Apple iMessage, Skype or even a web request from an iPhone app that works with Siri to enable voice.

So that’s it. That was a quick whistle stop guide to writing your own first chat bot. Hope you enjoyed it.

The project is available on my GitHub page: https://github.com/manthanhd/talkify

If you want to request a feature or file a bug, please feel free to raise an issue on the GitHub repository link above. I am also open to contributors so feel free to fork the repository and contribute.

Just for reference, here’s the NPM package link: https://www.npmjs.com/package/talkify

2 thoughts on “Getting started with chat bots using Talkify

  1. Thanks a bunch for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange arrangement between us!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.