Code-Along: Chatbot
In this activity, create a simple chatbot that can respond to some basic messages.
ELIZA
Start by talking to ELIZA, a "Rogerian Therapist" chatbot from the 1960s. This is a small example of what is possible with chatbots.
Basic Setup
To get started, copy this project. It contains some basic HTML and CSS, but no JavaScript yet! Manually adding some of the basic starter code will be helpful practice.
Add a new JavaScript file to the project, and link it from the index.html file.
- In the Files area of the project, click the "+" button
- Name the new file script.js
- Open the index.html file for editing
- Between the
<head>and</head>, under the<link>, make a new line - There, add a
<script></script>element - Set the
srcattribute of thescripttoscript.js
The added HTML should look something like this:
<script src="script.js"></script>
Function & Button
Next, it's time to define a JavaScript function, and hook it up to a button in HTML.
Defining the Function
Start by defining the function.
- Open the script.js file for editing
- Add code to define a function named
chat- Keyword:
function - Function name:
chat - Parentheses:
() - Curly brackets:
{}
- Keyword:
- In the body of the function (between
{and}), make a new line - Add an
alertstatement that says "Welcome to the Friend Proxy Chatbot!"
The code in the script.js file should look something like this:
function chat() {
alert("Welcome to the Friend Proxy Chatbot!");
}
Now the function is defined, but there is no way to run it yet!
Creating the Button
Create a button and hook it up to the function.
- Open the index.html file for editing
- Between the
<body>and</body>, under the<p></p>, make a new line - There, add a
<button></button>element - Set the
onclickattribute of thebuttonto call thechatfunctionchat()
- Make the text of the button say "Chat"
The added HTML should look something like this:
<button onclick="chat()">Chat</button>
Run the code. Click the "Chat" button on the page, and verify that a message appears!
Prompt for Message
Now, all new changes for the site will happen in the script.js file, within the chat function body. Start by prompting for the user to enter a message.
- Open the script.js file for editing
- Make a new line between the
{and}, under thealertstatement - Create a new variable named
message - Set the
messagevariable to aprompt - Make the text for the prompt say "Please enter a message..."
The added JavaScript should look something like this:
let message = prompt("Please enter a message...");
Run the code. Click the button, and verify that a prompt appears asking for a message. Nothing should happen when entering a message yet though!
Responding to the Message
Next, the chatbot should respond to certain messages the user enters. This is where if statements come in: the response should be different depending on the message.
To start, have the chatbot respond to a message of "Hello" from the user.
- Open the script.js file for editing
- Make a new line between the
{and}, under themessagevariable - There, create an
ifstatement- Keyword:
if - Parentheses:
() - Curly brackets:
{}
- Keyword:
- For the
ifcondition, check if the user said "Hello"- Condition goes between the parentheses
- Use
messagevariable,===, and"Hello"to compare
- In the body of the
if, display a message saying "Well hi there!"- Body is between the curly brackets
- Use
alert("Well hi there!");
The added JavaScript should look something like this:
if (message === "Hello") {
alert("Well hi there!");
}
Run the code. Click the button, enter "Hello" as the message, and verify that the proper response appears! If any other message is entered, no response should appear.
More Responses
The chatbot is functional, but it is not very powerful yet. It needs to be able to respond to some more messages! Add a couple more responses using if statements.
| Message | Response |
|---|---|
| I love you | That's nice |
| Are you my friend? | Yes, of course... |
These if statements will look very similar to the previous if statement. For each of the responses, follow these steps:
- Have the script.js file open for editing
- Make a new line at the bottom of the
chatbody (above}) - Add an
ifstatement:if () {} - For the condition (between
(and)), check ifmessage ===the given message - In the body (between
{and}),alertwith the response
The added JavaScript should look something like this:
if (message === "I love you") {
alert("That's nice");
}
if (message === "Are you my friend?") {
alert("Yes, of course...");
}
Run the code. Click the button, and try out each of the messages. Make sure to enter them exactly right, with proper punctuation/capitalization/etc. Each message should produce the correct response!
Final Code
By the end of the activity, the code should look something like this:
index.html
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet">
<script src="script.js"></script>
</head>
<body>
<h1>Friend Proxy Chatbot 🗣️🤖</h1>
<p>Press the "Chat" button to talk to the friend proxy chatbot.</h1>
<button onclick="chat()">Chat</button>
</body>
</html>
script.js
function chat() {
alert("Welcome to the Friend Proxy Chatbot!");
let message = prompt("Please enter a message...");
if (message === "Hello") {
alert("Well hi there!");
}
if (message === "I love you") {
alert("That's nice");
}
if (message === "Are you my friend?") {
alert("Yes, of course...");
}
}