Creative Coding · Session c08

Build WhinnyBot 🐴

A tiny chatbot with a big personality, powered by prompts and a dictionary of prepared answers.

Imagine a stable helper that knows four things perfectly: how to greet you, what horses like, whether carrots make good treats, and one truly terrible horse joke. It is not guessing and it is not using AI. You write every answer yourself, store the answers under useful names, and let the program fetch the right one.

🐴
WhinnyBotTry: hello, horse, carrot, joke
Neigh there! Type one of my secret keywords below.

Ingredient 1: the prompt

A prompt is the message a person sends to a program. In this lesson, it is whatever you type into WhinnyBot's input box. Our first bot is deliberately simple, so a good prompt is a short keyword that matches something the bot knows.

Prompt

carrot
the message sent by the user

Prepared answer

“Crunchy, orange, and best as a small treat!”
the reply written by you
Prompt design for a simple bot: decide what words people may type, make the words easy to remember, convert them to lowercase, remove spare spaces, and always include a fallback answer for an unknown prompt.

Ingredient 2: a dictionary

In JavaScript, a dictionary is usually called an object. It stores pairs: a key is the lookup word, and its value is the information stored under that word.

hello
Neigh there! Welcome to the stable.
horse
Horses can sleep standing up or lying down.
carrot
Carrots are treats, not a whole horse dinner.
default
I do not know that one yet. Teach me!
const answers = {
  "hello":  "Neigh there! Welcome to the stable.",
  "horse":  "Horses can sleep standing up or lying down.",
  "carrot": "Carrots are treats, not a whole horse dinner.",
  "joke":   "Why did the pony whisper? It was a little horse!",
  "default": "I do not know that one yet. Teach me!"
};

The colon means “stores this value”, and the comma separates one pair from the next. To look up an answer, put the prompt inside square brackets:

let answer = answers[prompt];

If prompt contains "carrot", JavaScript fetches the value stored at answers["carrot"].

Build it in p5.js

1

Create the dictionary

Write the object above your functions so every part of the program can use it.

2

Create the input and button

createInput() makes a place to type. createButton() makes the send button. This bot needs no canvas, so use noCanvas().

3

Clean the prompt

toLowerCase() makes HORSE match horse. trim() removes accidental spaces.

4

Look up the reply

Ask the dictionary for answers[prompt]. If the result is undefined, use answers.default.

const answers = {
  "hello":  "Neigh there! Welcome to the stable.",
  "horse":  "Horses can sleep standing up or lying down.",
  "carrot": "Crunchy, orange, and best as a small treat!",
  "joke":   "Why did the pony whisper? It was a little horse!",
  "default": "I do not know that one yet. Teach me!"
};

let promptInput;
let replyText;

function setup() {
  noCanvas();

  promptInput = createInput("");
  promptInput.attribute("placeholder", "Try: hello, horse, carrot, joke");

  let sendButton = createButton("Ask WhinnyBot");
  sendButton.mousePressed(answerPrompt);

  replyText = createP("Neigh there! Ask me something.");
}

function answerPrompt() {
  let prompt = promptInput.value().toLowerCase().trim();
  let answer = answers[prompt];

  if (answer === undefined) {
    answer = answers.default;
  }

  replyText.html(answer);
  promptInput.value("");
}
Important: this is a rule-based chatbot, not an AI chatbot. It can only return answers you placed in its dictionary. That limitation is useful because you can inspect exactly why every answer appeared.
Make WhinnyBot yours
  1. Add three new key-value pairs. Try drawing, martial arts, and a secret password.
  2. Give the bot a stronger personality. Is it dramatic, mysterious, ridiculously formal, or obsessed with carrots?
  3. Add a multi-word prompt such as "best horse". Remember that a key containing spaces needs quotation marks.
  4. Change the fallback answer so it suggests two prompts the user can try.
  5. Stretch: make the Enter key send the prompt too. Search the p5.js reference for keyboard input, then decide whether keyPressed() or an event listener fits best.
Open only if your bot is stuck

It always uses the fallback: check that the typed prompt exactly matches a key. Look for capital letters, spaces, and spelling.

The code stops immediately: check every comma, colon, quote, brace, and parenthesis. The browser console will usually point near the first broken symbol.

The button does nothing: write mousePressed(answerPrompt), without parentheses after answerPrompt. You are giving p5.js the function to run later.

Next session → c09: Randomness WhinnyBot currently gives the same prepared answer every time. Next you will use random() to make programs surprise you, and randomSeed() to make the surprise repeat exactly.
← All lessons
25:00

Ask your tutor