Categories We Write About

Build a chatbot with predefined replies

Creating a simple chatbot with predefined replies can be done easily with various programming languages. Here’s a straightforward example in Python that you can expand upon. It matches user input to predefined keywords or phrases and returns specific responses.

python
# Simple chatbot with predefined replies def chatbot(): print("Hello! I'm your chatbot. Type 'exit' to quit.") # Predefined replies dictionary replies = { "hi": "Hello! How can I help you today?", "hello": "Hi there! What can I do for you?", "how are you": "I'm just a bot, but thanks for asking!", "bye": "Goodbye! Have a great day!", "help": "Sure! You can ask me about our products, services, or hours of operation.", "thank you": "You're welcome!", "thanks": "No problem!", } while True: user_input = input("You: ").strip().lower() if user_input == "exit": print("Chatbot: Goodbye!") break # Check if any keyword in user input matches the dictionary keys response = None for key in replies: if key in user_input: response = replies[key] break if response: print("Chatbot:", response) else: print("Chatbot: Sorry, I don't understand that. Can you try something else?") if __name__ == "__main__": chatbot()

How this works:

  • The bot waits for user input.

  • It looks for keywords in the input matching predefined keys.

  • If a match is found, it replies with the corresponding predefined answer.

  • If no match, it asks the user to try something else.

  • Typing “exit” ends the conversation.

Would you like a version in another language or with more advanced features like regex matching or context awareness?

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About