Skip to content
Return to course

Week 4 · Lesson 1 · 50-60 minutes

Build a Rule-Based Chatbot

Design a chatbot the old-fashioned way — with keywords, intents, and a decision tree — and discover why it needs a fallback for everything it wasn't built to handle.

Materials
This lesson in a web browser · Paper and pencil, or a notes app
This lesson
In progress

Auto-saves on this device

What you'll be able to do

  • Explain how a rule-based chatbot uses keywords to guess a user's intent.
  • Map a set of intents and replies as a decision tree.
  • Explain why a chatbot needs a fallback for messages it doesn't understand.
  • Compare a rule-based chatbot with an AI that learns from examples.

Think about it

A pizza shop's chat window answers 'What time do you close?' instantly, but replies 'Sorry, I didn't understand' when you type 'when do you shut?' Both questions mean the same thing. Why does one work and the other fail?

Hold on to your guess — by the end you'll be able to explain exactly what went wrong.

Make a prediction

Predict: if a chatbot only knows the keyword 'close', how many different ways could a customer ask about closing time without ever using that word?

Keyword
An important word the chatbot looks for in a message to figure out what the person wants, like 'hours' or 'price'.
Intent
What the person is actually trying to do or ask, such as 'find the opening hours' — even if they word it many different ways.
Decision tree
A branching set of yes/no or if/then choices that leads from a message to a reply, like a flowchart.
Fallback
The safety reply a chatbot gives when it doesn't match any keyword or intent, such as 'Sorry, I didn't understand that.'

Keywords: the words the chatbot watches for

A rule-based chatbot doesn't understand language the way you do. Instead, a person gives it a list of keywords to look for. When your message contains a keyword like 'hours', 'open', or 'close', the bot picks the reply that a human wrote for that word.

This is fast and predictable, but fragile. If you ask the same thing using different words, the keyword isn't there, and the bot misses it completely.

  • Keyword 'hours' → reply with the opening hours
  • Keyword 'price' or 'cost' → reply with the menu prices
  • Keyword 'refund' → reply with the return policy

Intent: what the person actually wants

Behind the words is an intent — the thing the person is really trying to do. 'What time do you close?', 'Are you open late?', and 'When do you shut?' are three different sentences with one intent: find out the hours.

Good chatbot design groups many keywords and phrasings under the same intent. The more keywords you attach to an intent, the more ways of asking the bot can catch — but a person still has to think of them in advance.

  • Intent 'get hours' matches keywords: hours, open, close, closing, late, when
  • Intent 'find location' matches: where, address, directions, located
  • Intent 'place order' matches: order, buy, delivery, pickup

Decision tree and fallback: routing a message to a reply

A rule-based chatbot follows a decision tree: check for the first intent's keywords; if found, give that reply; if not, check the next intent; and so on down the branches. Each branch ends in a reply a person wrote.

But no list of keywords covers every possible message. When nothing matches, the tree reaches its last branch: the fallback. The fallback is an honest 'I didn't understand that' plus, in good designs, a hint about what the bot can help with. Without a fallback, an unmatched message would get no reply at all.

  • Message 'when do you shut?' with no 'hours' keyword → falls through to the fallback
  • Fallback reply: 'Sorry, I can help with hours, location, and orders. Try asking about one of those.'

Worked example

Tracing a message through the tree

  1. The bot has three intents set up by a person: 'get hours' (keywords: hours, open, close), 'find location' (where, address), and 'place order' (order, delivery).
  2. A customer types: 'How much is a large pizza?'
  3. Branch 1 — does it contain hours, open, or close? No. Move on.
  4. Branch 2 — does it contain where or address? No. Move on.
  5. Branch 3 — does it contain order or delivery? No. Move on.
  6. No branch matched, so the tree reaches the fallback: 'Sorry, I didn't understand. I can help with hours, location, and orders.' The customer really wanted the price — an intent the bot was never given.

Takeaway: A rule-based bot can only answer intents someone built in; anything else falls through to the fallback, even when the question is perfectly clear to a human.

How a rule-based chatbot routes a message

Incoming message

  • If it mentions hours, open, or close:

    Reply: "We're open 11am to 10pm."

  • If it mentions where or address:

    Reply: "We're at 5 Main Street."

  • If it mentions order or delivery:

    Reply: "Order online or call 555-1234."

  • If none of the above match:

    Fallback: "Sorry, I didn't understand. I can help with hours, location, and orders."

A decision tree that starts at the top with the incoming message and flows downward through one check per intent. Node 1 asks: 'Does the message contain hours, open, or close?' If yes, the branch ends in the reply 'We're open 11am to 10pm.' If no, it flows down to Node 2: 'Does it contain where or address?' If yes, the branch ends in 'We're at 5 Main Street.' If no, it flows to Node 3: 'Does it contain order or delivery?' If yes, the branch ends in 'Order online or call 555-1234.' If no, the final branch is the fallback: 'Sorry, I didn't understand. I can help with hours, location, and orders.' Every yes-branch is a leaf with a human-written reply; the very last no-branch is always the fallback so no message is left unanswered.

Each intent is one check; the last branch is the fallback that catches everything unmatched.

Same intent, many wordings
Customer messageKeyword found?Result
What time do you close?closeMatches 'get hours'
Are you open late?openMatches 'get hours'
When do you shut?noneFalls to fallback
Still serving food?noneFalls to fallback

A table showing that one intent, 'get hours', can be asked many ways. Rows: 'What time do you close?' contains the keyword 'close', so it matches. 'Are you open late?' contains 'open', so it matches. 'When do you shut?' contains no listed keyword, so it misses and hits the fallback. 'Still serving?' contains no listed keyword, so it also misses. The lesson: adding more keywords catches more wordings, but a human must think of them all.

Activity

Rule-Based Chatbot Builder

Build a working rule-based chatbot from a template — intents, keywords, a follow-up branch, and a fallback — and test it in a live preview that shows exactly which rule matched.

  1. Pick a template, then edit the welcome, intents (keywords + response), a follow-up branch, and the fallback.
  2. Watch the decision-tree outline and the validation checks for dead ends, unreachable nodes, and a missing fallback.
  3. Test your bot in the live preview — try normal questions, unexpected input, and asking for a human.

Start from a template

Answers questions about hours, borrowing, and library cards.

Build your bot

Intents (checked in order)

  1. Follow-up branch

Decision-tree outline

  • ▸ Welcome: Welcome to the school library helper! I can help with hours, renewing a book, or getting a library card.
  • If keyword [hours, open, close, time] → Opening hours
  • If keyword [renew, overdue, extend, keep] → Renew a book
    • ? Is your book already overdue? (type 'yes' or 'no')
    • → [yes, overdue, late] Overdue
    • → [no, not] Not overdue
  • If keyword [card, join, sign up, register] → Library card
  • ▸ Fallback: Sorry, I didn't catch that. I can only help with a few topics — try a keyword, or type 'help' to reach a person.

Checks

All checks pass — no dead ends, unreachable nodes, or missing fallback.

Live preview

Please don't type personal information (full name, address, phone). I only answer general questions.

Send a message to test your bot. Try a keyword, an unexpected message, or “talk to a person”.

Check your understanding

Answer these to check that you understand how a rule-based chatbot works.

  1. Question 1. What is an 'intent' in a chatbot?

  2. Question 2. Decide if the statement is true or false.

    A rule-based chatbot needs a fallback so that messages it doesn't match still get some reply.

  3. Question 3. Put the steps of how a rule-based chatbot answers a message in the right order.

    1. 1.A message arrives from the person
    2. 2.The bot checks the message for each intent's keywords, branch by branch
    3. 3.If a branch matches, the bot sends that branch's reply
    4. 4.If no branch matched, the bot sends the fallback reply

0 of 3 answered

Try it yourself

Break your own chatbot

Take the chatbot you designed and try to fool it. Find three messages a real person might send that your keywords miss.

  1. Write three messages that mean something your bot should handle, but avoid all of your keywords.
  2. Trace each one through your tree and confirm it hits the fallback.
  3. For each, decide: could you fix it by adding a keyword, or is it too different to catch with rules?

Success looks like

  • Three messages that reach the fallback even though a human would understand them.
  • For each, a note on whether a keyword could fix it.
  • One clear example that keywords alone cannot solve.

Reflection

Saved on this device as you type

Please don't include private information. Your answers are saved only in this browser and are never sent anywhere.

Recap

A rule-based chatbot matches keywords to guess a user's intent, routes the message through a decision tree, and uses a fallback for anything it can't match.

  • Keywords are the words the bot watches for; intents are what the person really wants.
  • A decision tree checks one intent per branch and ends in a fallback.
  • Rule-based bots only handle intents someone built in — everything else falls through.

Grades 7–8 extension

From rules to learning

Big real chatbots don't rely on keyword lists alone. Many use machine learning trained on thousands of real messages, so they can recognize an intent even from wordings no one typed in by hand.

Describe one advantage and one risk of a learning-based chatbot compared with your rule-based one. Think about what happens when the training messages don't include the way some people speak.

Finish this lesson

  • Attempt the knowledge check — not done
  • Save a reflection (recommended) — not done

Attempt the knowledge check to finish this lesson.