Build a Chatbot

Write a program that responds to the user's text input.


How to Use This Template

Fork this template!

The template has several functions that allow you to focus on programming the chatbot's behavoir. It will read in the user's strings from the input box. It will keep a record of every message and display the last few messages above the input box.

To control what the chatbot will say make changes to the javascript function chatbotResponse() . You can also change the HTML or CSS. There is no need to keep these directions.

The variable lastUserMessage is a string that records the last thing typed.

The variable botMessage is a string that controls what the chatbot will say.

Example:

if (lastUserMessage === 'hi'){
  botMessage = 'Howdy!';
}
if (lastUserMessage === 'what is your name'){
  botMessage = 'My name is' + botName;
}


Ideas

Narrow the range of topics the chatbot responds to, like an adventure time chatbot.

Customize the html and CSS to fit your theme.

If you only have a few responses, tell the user what commands work.

Use a switch statement to simplify a large number of if else branches.



Advanced Ideas

Use the .tolowercase() command to ignore capitalization.

Make variables to keep track of what's been said. Use those variables in an If stement to produce a new set of responses.

Respond to the users by using what they said. For example:

User: I like puppies
Chatbot: Tell me more about puppies.

Use the date function to answer questions like "what time is it?".

Quizbot?

Calculator Mathbot?

Canvas drawbot!

Write a text adventure game. Like these.

Build an array with several similar responses and have the chatbot pick one at random, like this:

var hi = ['hi','howdy','hello','hey'];
botMessage = hi[Math.floor(Math.random()*(hi.length))];

Use regular expressions for powerful searches.

Regular expressions can search in a way similar to a google search. This example searches for words that are cat-like. It also ignores capitalization and looks for the words preceded and followed by spaces.

var n = lastUserMessage.search(/\b(cat|cats|kitten|feline)\b/i);
if (n !== -1) {
  botMessage = 'I hate cats!';
} 

Here is an example that looks for ways to say dog as singular or plural. It then repeats back the dog related word to the user.

var patt = /\b(dogs?|pup(s|py|pies?)?|canines?)\b/i;
var result = patt.exec(lastUserMessage);
if (result) {
  botMessage = 'I love ' + result[0];
} 


Links: