Building a Simple Chatbot Without a Framework: Intents, State and Fallbacks

You can build a working chatbot with plain Python and no framework. Define intents, match user messages, track state, and add a fallback for the rest.

By Galactic News Editorial · Published 2026-11-22T22:28:13+00:00 · Updated 2026-11-22T22:28:13+00:00

Building simple chatbot without framework using intents and state

Building a Simple Chatbot Without a Framework: Intents, State and Fallbacks

You can build a working chatbot with plain Python and no framework. The design has three parts. Intents define what the user might mean. State tracks where the conversation stands. Fallbacks handle everything else. This guide walks through each part with a small FAQ bot as the example.

A framework would give you these parts ready-made. Building them yourself teaches you how they work. It also keeps the project small. A FAQ bot for a cafe, a club, or a small shop needs no heavy dependency. A few dozen lines of standard-library Python can run it.

How an Intent Chatbot Works

An intent chatbot does not write new sentences. It recognizes what the user means and picks a matching scripted answer. That is how many real FAQ and support bots work. The pipeline has three steps. First, classify the message into an intent such as greeting, hours, or menu. Second, pick one of that intent's responses. Third, if confidence is low, use a fallback instead of guessing.

Start with the training data. An intents file in JSON holds each intent as a tag, a list of example patterns, and a list of responses. A small starter file might hold 10 intents and 56 examples. Each pattern is a phrase a user might type. Each response is a reply the bot may give.

Tokenization turns text into words. Split the message into lowercase words with a regular expression. That step is small but it matters. Matching works on words, not on raw sentences.

Building Simple Chatbot Without Framework: Matching Intents

The simplest matcher uses keywords. Each intent holds a list of keywords. The bot lowercases the input and checks which intent's keywords appear. A message about opening hours matches the hours intent. A message about the menu matches the menu intent.

Regular expressions add power. A pattern can capture a name from "my name is Mia" and reuse it in the reply. A pattern can match several phrasings of the same question. Keep patterns simple. A pattern that tries to cover everything becomes hard to debug.

A scoring matcher is more robust. Count word overlap between the input and each intent's examples. Pick the intent with the highest score. A Naive Bayes classifier does this with probabilities. It counts how often each word appears in each intent's examples. Then it scores a new message against every intent. The math fits on one screen, and it runs on the standard library alone.

Confidence decides when to answer and when to fall back. Turn the scores into probabilities with a softmax step. If the top probability sits below a threshold, such as 0.25, the bot falls back. That threshold is a design choice. A low threshold answers more and guesses more. A high threshold falls back more and frustrates users.

Adding State: Memory and Multi-Turn Conversations

State is what turns a one-shot answer machine into a conversation. A simple state store is a dictionary keyed by user or session. It holds facts the bot has learned. A name captured in one turn can greet the user in the next.

Multi-turn flows need a state machine. Each state names what the bot expects next. A quiz bot moves from asking a question to waiting for an answer to giving feedback. A booking bot moves from asking a date to asking a time to confirming. The bot stores the current state and the facts collected so far.

Keep state small and explicit. Store the current state name and a few fields. Write the state to a file or a database between turns if the bot runs as a web service. A state that lives only in memory dies when the process restarts.

Timeouts matter. A conversation that stalls for an hour should not resume mid-flow as if nothing happened. Expire old sessions. When a session expires, reset the state and greet the user fresh.

Fallbacks: What Happens When the Bot Does Not Understand

Every chatbot needs a fallback. Users will type things outside the script. A good fallback says so plainly and offers a next step. "I am a simple bot. Try asking about the menu, hours, or location." That reply is honest and useful.

Design three levels of fallback. The first level asks a clarifying question. It offers the closest matching topics. The second level suggests the help command or a list of things the bot can do. The third level hands off to a human or a contact form. Escalation beats a loop of repeated failures.

Log every fallback. The log shows what users actually ask. Those phrases become new patterns or new intents. A bot improves fastest by studying its own failures.

Putting It Together: Numbered Steps

  1. Define 5 to 10 intents. Cover greetings, your core topics, and goodbye. Write 5 to 8 example patterns per intent.
  2. Write the intents file. Use JSON with tag, patterns, and responses. Keep responses short and varied.
  3. Build the tokenizer. Lowercase the input and split it into words with a regular expression.
  4. Build the matcher. Start with keyword scoring. Move to a Naive Bayes classifier when keyword matching feels brittle.
  5. Add a confidence threshold. Fall back below the threshold instead of guessing.
  6. Add a state store. Use a dictionary per session. Capture names and track multi-turn flows.
  7. Write the fallback replies. Offer topics, suggest help, and escalate to a human after repeated failures.
  8. Add a chat loop. Read input, classify, respond, and update state. Test with real phrases from real users.
  9. Serve it over HTTP. A small FastAPI app with a chat endpoint turns the console bot into a web widget.
  10. Log fallbacks and review weekly. Add patterns for the most common misses. Retrain or reload the intents file.

When to Stop Building and Use a Framework

A hand-built bot fits narrow domains. FAQ bots, menu bots, and simple quiz bots work well. The bot breaks when users phrase things in unexpected ways. Language has endless variety. A rule for every phrasing is a job that never ends.

That brittleness is the signal to grow. Add a TF-IDF matcher that compares the input to stored examples by cosine similarity. That step handles phrasing variation better than keywords. If the domain keeps growing, move to a framework or a language model for open-ended input. Keep the rule-based core for the narrow tasks it handles well.

Bottom Line

Building a simple chatbot without a framework teaches the core ideas. Intents classify meaning. State tracks the conversation. Fallbacks handle the unknown. Start with keyword matching and scripted replies. Add scoring and a confidence threshold. Add state for multi-turn flows. Log the failures and grow the intents file from real user phrases. When the domain outgrows rules, add a smarter matcher or a framework. The hand-built version stays useful as the prototype that taught you what to build.