Over the next 10 updates you will design and code your own chatbot in JavaScript: a personality, keyword matching, random answers, memory and real conversations. At the Bot Expo the class chats with every bot and rates it. You are the designer. You are the writer. You are the coder.
reply(message). It runs every time someone sends a message, and whatever it returns is what your bot says back..html file to Downloads. Start of every update: Open and choose that file. The Studio keeps a draft in this browser too, but your Downloads file is the safe copy..html file that runs in any browser. At the Bot Expo it runs on your laptop as a station, and the class chats with it and rates it.Need to log into Minecraft Education, Canva, or any other app that asks you to sign in? Most school and app accounts use the same login: your username is [email protected] (add a number if your name needs one, e.g. firstname.lastname2), and your password is the same one you use to log into your school computer.
Every project has at least one thing to hand in. Log into connect.det.wa.edu.au with your firstname.lastname and computer password, then head to your class and open the Submission section. Handing in more than one file? Zip them together first - total size must stay under 50MB.
Design and code a chatbot with a clear personality and purpose that understands keywords, gives varied answers, remembers what you tell it and can hold a short conversation, then put it in front of the class at the Bot Expo.
| Criteria | Offline (0-1) |
Booting (2-3) |
Online (4) |
Smart (5) |
Score |
|---|---|---|---|---|---|
| Personality & Purpose | No clear purpose or voice | A name, a greeting and a topic | A clear purpose, and every reply sounds like the same character | A bot people enjoy talking to, with a voice that fits its job perfectly | / 5 |
| Code | Bot only works with heavy teacher help | reply() answers a few keywords using the Studio code | Keywords, random answers, memory and a two-step conversation all work in your own bot | Your own changes or new features, and you can explain every function you use | / 5 |
| Conversation | Most real questions get the fallback | Handles the obvious questions | Handles different ways of asking, with a helpful fallback | Feels natural: remembers, follows up and rarely gets stuck | / 5 |
| Test & Expo | No testing, bot not ready | Tested once, some fixes made | Test log with fixes backed by real chats, bot ready on the day | Polished, honest and safe bot, great at the Expo, clear reflection | / 5 |
| Total | / 20 | ||||
Run restarts your bot, so it forgets everything. Save downloads one .html file (your whole bot). Open loads it back. Play opens your bot full screen in a new tab. Reset brings back the demo bot - press it twice to confirm.
botName is the name at the top, greet() is the first message, and reply(message) decides every answer using if / else ifreply and press Run. That's the whole workflow: edit, Run, chatbotName holds one, and greet() returns one. If your text has an apostrophe in it, like I'm, wrap it in double quotes: "I'm Sparky"reply so it sounds like your character.msg.bot is your bot's bubbles, .msg.you is the person'sbody {
background: #1b1026;
color: #f4ecff;
font-family: 'Trebuchet MS', Arial, sans-serif;
}
#chatHead {
background: #ff7ab6;
color: #2a0b1c;
letter-spacing: 1px;
}
.msg.bot {
background: #33204a;
color: #f4ecff;
border-bottom-left-radius: 2px;
}
.msg.you {
background: #ff7ab6;
color: #2a0b1c;
border-bottom-right-radius: 2px;
}
#msg {
background: #33204a;
color: #f4ecff;
border-color: #ff7ab6;
}
#sendBtn {
background: #ff7ab6;
color: #2a0b1c;
}
const botName = 'Captain Byte';
function greet() {
return "Ahoy! I'm Captain Byte, the friendliest pirate bot on the seven seas. Ask me about treasure, ships or the weather!";
}
greet(), before its return, add const hour = new Date().getHours(); then if (hour < 12) { return 'Good morning! ...'; }. Before midday your bot says good morning.message.toLowerCase() makes the message all lowercase, so ROBOTS and robots look the same. That's why every keyword you check must be lowercasetext.includes('ship') is true if the word appears anywhere in the message. || means or: text.includes('ship') || text.includes('boat') catches bothelse if checks each rule in order. The first one that matches wins, and the final else is the fallback when nothing matcheselse if for each of your five topicsfunction reply(message) {
const text = message.toLowerCase();
if (text.includes('hello') || text.includes('hey')) {
return 'Ahoy there, matey!';
} else if (text.includes('treasure')) {
return 'X marks the spot! Mine is buried on Skull Island.';
} else if (text.includes('ship') || text.includes('boat')) {
return 'My ship is called the Rusty Byte. She leaks a bit.';
} else if (text.includes('weather')) {
return 'Calm seas and a good breeze today.';
} else if (text.includes('bye')) {
return 'Fair winds, matey!';
} else {
return "Arr, I don't understand. Try asking about treasure or ships.";
}
}
'hi' is inside "this" and "which", so "What is this?" would get your hello answer. Pick keywords that can't hide in other words, like 'hello'.} else if (text.includes('help')) { return 'You can ask me about: treasure, ships, weather.'; just before your final } else {, and list your own topics. Now anyone stuck can type help.['a', 'b', 'c']. Each item has a position number, starting at 0pick(list) uses Math.random() to choose a random position, then returns that itemreply, then paste the joke topic inside reply on the line just before } else {pick() for both. Your fallback can use pick() too// Lists of answers. pick() chooses one at random.
const jokes = [
'Why did the pirate buy a laptop? For the sea drive!',
"What's a pirate's favourite letter? You'd think R, but it's the C!",
'Why are pirates bad at cards? They sit on the deck.'
];
function pick(list) {
return list[Math.floor(Math.random() * list.length)];
}
} else if (text.includes('joke')) {
return pick(jokes);
reply add let lastPick = ''; and a function pickNew(list) that calls pick(list) again while the choice equals lastPick, then saves it in lastPick and returns it. Use pickNew instead of pick.let at the top of the JS tab keeps its value between messages. That's your bot's memoryreply, like text, is brand new every message, so it can't remember anythingconst text, and the name answers just before } else {startsWith('my name is ') checks how the message begins, and message.slice(11) cuts off the first 11 characters to leave the name. It uses message, not text, so the capital letters are kept// Memory: these keep their values between messages
let userName = '';
let messageCount = 0;
messageCount = messageCount + 1;
} else if (text.startsWith('my name is ')) {
userName = message.slice(11).trim();
return 'Nice to meet you, ' + userName + '!';
} else if (text.includes('what is my name')) {
if (userName === '') {
return "You haven't told me yet. Say 'my name is ...'";
} else {
return 'Your name is ' + userName + '.';
}
} else if (text.includes('how many')) {
return 'You have sent me ' + messageCount + ' messages.';
+ needs spaces inside the quotes: 'Hi ' + userName + '!'. Without the space after Hi, you get "HiSam!".return 'Bye, ' + (userName || 'friend') + '!';. If your bot knows the name it uses it, and if not it says friend. || picks the first one that isn't empty.waitingFor holds that: '' means nothing, 'quiz' means "the next message is the answer to my quiz question"reply, straight after const text. It has to come first, so the answer is checked before any other rule// What the bot is waiting for. '' means nothing.
let waitingFor = '';
if (waitingFor === 'quiz') {
waitingFor = '';
if (text.includes('8') || text.includes('eight')) {
return 'Correct! Spiders have 8 legs.';
} else {
return 'Not quite. Spiders have 8 legs.';
}
}
if (text.includes('quiz')) {
waitingFor = 'quiz';
return 'Quick quiz: how many legs does a spider have?';
}
waitingFor = ''; is what ends the conversation. Without it, your bot treats every message from then on as the quiz answer.waitingFor = 'more'; and return the joke plus " Want another one?". At the top of reply, if waitingFor === 'more', reset it, then return another joke if the text includes yes, or 'Okay, no more jokes.' if not.else if gets hard to read. A rules list keeps every topic in one place: an array of objects, each with its words and its answerfindRule uses a loop inside a loop: for every rule, check every word. The first match wins. If nothing matches it returns null, which means nothingreply, then paste the second box inside reply just above the first if// Each rule: the words to look for, and the answer to give
const rules = [
{ words: ['price', 'cost', 'how much'], answer: 'A ticket costs $10.' },
{ words: ['open', 'hours', 'time'], answer: "We're open 9am to 5pm, every day." },
{ words: ['where', 'address', 'find you'], answer: "We're at 1 Harbour Road." }
];
// Check every word of every rule. Return the first answer that matches.
function findRule(text) {
for (const rule of rules) {
for (const word of rule.words) {
if (text.includes(word)) {
return rule.answer;
}
}
}
return null;
}
const answer = findRule(text);
if (answer !== null) {
return answer;
}
{ words: ['park', 'parking', 'car'], answer: 'Free parking out the back.' },. No new code needed - that's the point of a rules list.'jok' matches joke, jokes and joking. Find two of your keywords where a shorter version still can't hide inside other words, and shorten them.reply, straight after const text, so these checks win over everything else if (text.includes('are you human') || text.includes('are you real') || text.includes('are you a bot')) {
return "I'm a bot, made with JavaScript. A human wrote my answers.";
}
if (text.includes('help me') || text.includes('i feel sad') || text.includes('unsafe')) {
return "I'm only a bot, so I can't help with that properly. Please talk to someone you trust, like a teacher, a parent or student services.";
}
| Greeting | Says who the bot is and what to ask |
| Every topic | Answers at least two different ways of asking |
| Memory | Remembers your name and uses it |
| Conversation | Asks a question, understands the answer, then goes back to normal |
| Honest and safe | Admits it's a bot, points serious problems to real people |
After 10 updates your bot goes live. Every bot gets chatted with, every bot gets rated, and the class votes for the bots that deserve the Hall of Fame.
Chat with the demo bot, plan your own // Update 01