Originally published on CodingCush.
It is 11 at night. You message a small tea shop to ask the price of a cup of chai, and a reply pops up in two seconds. No one is awake at the shop. The reply came from a chatbot. A chatbot is a computer program that talks with people through messages. It reads what you type, picks the best reply it knows, and sends it back, day or night. In this guide, we will build a tiny one together, a little tea stall bot in Python, and you will see exactly how it understands you.
Our Running Project: A Tea Stall Bot That Never Sleeps
Our bot works for a made up shop, Kiran's Tea Stall. It is fictional, invented for this guide, but the code is real and you can run it yourself. The bot will start with one trick, saying hello, and then grow. First it answers questions about price and timings, then it keeps chatting until you say bye. Every step adds one feature to the same small program, so you can watch a chatbot grow from a single reply into a real conversation. If you have never written Python before, read our guide to what Python is, explained simply first, then come back here.
Step 1: Teach It to Say Hello
Every chatbot starts with one job: turn a message into a reply. In Python, that job is a function called reply. It takes the message, looks at it, and returns an answer. Here is the smallest version of our bot:
def reply(message): message = message.lower() if "hello" in message or "hi" in message: return "Hello! Welcome to Kiran's Tea Stall. How can I help you today?" return "Sorry, I did not understand that. Try asking about the price or our timings." print(reply("hello")) The first line inside the function, message.lower(), turns the message into small letters, so Hello, HELLO and hello all look the same to the bot. Then the if line checks whether the word hello or hi sits inside the message. If it does, the bot returns its welcome. If not, it falls through to the last line and admits it did not understand. Run it, and you get exactly this:
Hello! Welcome to Kiran's Tea Stall. How can I help you today? That is already a chatbot, just a very shy one. Now let us teach it more.
Step 2: Teach It to Answer Real Questions
A shop bot that only says hello is not much use. Customers ask about price and timings, so we add two more checks to the same function. Nothing else changes:
def reply(message): message = message.lower() if "hello" in message or "hi" in message: return "Hello! Welcome to Kiran's Tea Stall. How can I help you today?" if "price" in message or "cost" in message: return "A cup of masala chai costs 20 rupees." if "open" in message or "timing" in message: return "We are open every day from 7 in the morning to 9 at night." if "bye" in message: return "Bye! Come back soon for a hot cup of chai." return "Sorry, I did not understand that. Try asking about the price or our timings." Each if is one little rule: if the message holds a keyword, send the matching reply. The order matters, because the bot returns the first match it finds and stops. That is why this kind of bot is called a rule-based chatbot. It follows rules a human wrote, one by one. Real chatbots that understand meaning use a field called natural language processing, which you can read about in our guide to what natural language processing is. For now, try the new version with four test messages:
print(reply("hello")) print(reply("What is the price of chai?")) print(reply("Are you open now?")) print(reply("Do you sell cake?")) Hello! Welcome to Kiran's Tea Stall. How can I help you today? A cup of masala chai costs 20 rupees. We are open every day from 7 in the morning to 9 at night. Sorry, I did not understand that. Try asking about the price or our timings. Three good answers and one honest shrug. The cake question has no rule, so the bot falls through to its last line. That fallback is not a bug. It is the polite way to say "I do not know" instead of guessing.
Step 3: Let It Chat Until You Say Bye
So far we have called reply by hand, one message at a time. A real bot keeps listening. A short loop does that:
print("Bot: Hello! Welcome to Kiran's Tea Stall. Type 'bye' to leave.") while True: user = input("You: ") answer = reply(user) print("Bot:", answer) if "bye" in user.lower(): break The while True loop runs until break stops it. Each round it waits for your message with input, passes it to reply, and prints the answer. When your message holds the word bye, the loop breaks and the program ends. Here is a real session with this exact code:
Bot: Hello! Welcome to Kiran's Tea Stall. Type 'bye' to leave. You: hello Bot: Hello! Welcome to Kiran's Tea Stall. How can I help you today? You: What is the price? Bot: A cup of masala chai costs 20 rupees. You: bye Bot: Bye! Come back soon for a hot cup of chai. That is the whole bot: a function full of rules, plus a loop that keeps it talking. Try adding one more if line, for the word menu, and run the program again.
Rule-Based Chatbots vs AI Chatbots
Our tea stall bot is rule-based. The chatbots on big websites are often the other kind, AI chatbots. The difference is simple. A rule-based bot matches words you planned for. An AI bot learns patterns from lots of example conversations, so it can handle messages you never planned for. Machine learning, explained simply, is the engine behind that learning, and it is worth a read once you are comfortable with rules.
Google Cloud's AI chatbot page, which shows how to build custom and pre-built AI-powered chatbots.
| Type | How it answers | Best for | Watch out for |
|---|---|---|---|
| Rule-based | Matches keywords with rules you wrote | Small shops, FAQs, simple help | Gets stuck on new wording |
| AI chatbot | Predicts the best reply from training examples | Big support desks, many topics | Needs lots of data, can still get things wrong |
Most real chatbots sit in the middle. They use rules for the common questions, like price and timings, and an AI model for the rest. The IBM chatbot explainer is a good next read if you want the business side of this in plain words, and the Google Cloud AI chatbot guide shows how the bigger platforms build them. Start with rules, like we did here. Add AI only when your list of rules starts to feel too long to manage.
When the Bot Gets Confused
Almost every broken bot is one of four mistakes. First, the bot answers everything with "Sorry, I did not understand". Check that message.lower() is really there, because without it a capital Hello will not match the small word hello. Second, the bot answers the wrong question. Remember the first matching rule wins, so a message with two keywords gets the first rule's reply. Third, the bot never stops chatting. The loop breaks only when your message holds the word bye, so check the break line and its indentation. Fourth, Python shows a NameError. That usually means a typo in the function name, or calling reply before it is defined. Run it with python3 chatbot_demo.py and read the exact error line.
Questions Beginners Actually Ask
What is a chatbot in simple words?
A chatbot is a program that chats with people through messages. It reads what you type and sends back the best reply it knows, like the tea stall bot in this guide.
Are chatbots real AI?
Some are, and some are not. A rule-based bot just follows written rules, so it is not really AI. An AI chatbot learns from example conversations, so it can handle new messages it was never told about.
Do I need to know coding to build a chatbot?
For a tiny one like this, a little Python is enough, and you have just seen the whole program. No-code chatbot builders exist too, but building it yourself teaches you far more.
Can a chatbot replace a human?
Not really. A chatbot is great at repeat questions like price and timings, any time of day. A human is still better for the tricky questions, the complaints, and anything that needs a real judgement call.
A Short Ending
A chatbot is just a program that reads a message and picks a reply. Ours started with one greeting, learned to answer about price and timings, and then kept chatting until someone said bye. That is the whole trick. Now add one rule of your own, maybe for the word menu, and your bot will already know something ours does not.

