How To Make a basic chat-bot with a JSON file using Python
TABLE OF CONTENTS
Create JSON File
Create python file
Postcel, Teknologi Ai - Chat bot ! -Hello and welcome to today's tutorial on how to make a chatbot in Python! Chatbots have become increasingly popular in recent years, and for good reason. They can provide users with instant responses to their questions and can be programmed to handle a wide variety of tasks. In this tutorial, we will walk you through the process of creating a chatbot using Python. We'll cover everything from setting up the necessary libraries and tools to building a basic chatbot that can respond to user input. By the end of this tutorial, you'll have a solid foundation in building chatbots with Python and will be able to continue exploring this exciting field on your own. So let's get started! Postcel Chat Excample "Hello World!"
Create JSON File
{
"greetings": [
"Hello!",
"Hi there!",
"Hey!",
"Greetings!"
],
"questions": [
{
"question": "What is your name?",
"answer": "My name is Chatbot."
},
{
"question": "How old are you?",
"answer": "I am a computer program, so I don't have an age."
},
{
"question": "What can you do?",
"answer": "I can answer your questions and have a conversation with you."
}
],
"farewells": [
"Goodbye!",
"See you later!",
"Bye!",
"Take care!"
]
}
Create python file
import json
import random
# Load the JSON file
with open('chatbot.json', 'r') as f:
chatbot_data = json.load(f)
# Define the chatbot function
def chatbot():
print(random.choice(chatbot_data['greetings']))
while True:
user_input = input("You: ")
if user_input.lower() in ['exit', 'bye', 'goodbye']:
print(random.choice(chatbot_data['farewells']))
break
for question in chatbot_data['questions']:
if user_input.lower() == question['question'].lower():
print(question['answer'])
break
else:
print("I'm sorry, I don't understand.")