DEV Community

loading...

Your very own speaking chatbot

Victor Ribeiro
Software Engineering.
Originally published at github.com ・2 min read

myChatBot

A simple JavaScript chatbot using Regular expression and Web Speech API

A live version is hosted here

Basic rules

Right now there's only a few rules written. The bot can answer "what is your name", "how old are you", "what day is today" and "what day is tomorrow". But you can, of course, increment that.

How to write rules

I do recommend to take a look at regular expression and see how it works. But, let's pretend we are interested in knowing if the user are asking about our age (we being the chat bot). So, a questio like "How old are you?" you translate in the following regular expression:

/.*how.*old.*you.*/

The bot would understand the rule as anything how anything old anything you anything.

Very simple, eh?!

So, the bot is really just a collection of rules, that are defined as objects:

{rule: /.*how.*old.*you.*/, response: "I don't know how old I am"}

you can get fancy, but setting the response to the number 1 and writting a function to deal with the respose:

{rule: /.*what.*do.*you.*think.*about(.*)/, response: 1, action: dealWithQuestion}

function dealWithQuestion(question){
  return "I don't like "+ question + " very much!";
}

In this case, the parentesis means that you want to capture whatever comes after what do you think about. So, if the question is "what do you think about coconuts?" the answer would be "I don't like coconuts very much!".

The code is here

Discussion (0)