DEV Community

Victor Ribeiro
Victor Ribeiro

Posted on • Originally published at github.com

4

Your very own speaking chatbot

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →