-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
121 lines (90 loc) · 2.45 KB
/
Copy pathchatbot.py
File metadata and controls
121 lines (90 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Basic Chatbot
CodeAlpha Python Programming Internship
Author: Helina Leul
Description:
A console-based rule-based chatbot that interacts with
users using predefined responses.
Features:
- Greets the user
- Responds to predefined questions
- Handles unknown inputs gracefully
- Saves chat history to a text file
- Clean and modular design
"""
# ==========================
# Imports
# ==========================
from datetime import datetime
from responses import RESPONSES
# ==========================
# Welcome Message
# ==========================
def display_welcome():
"""
Display the chatbot welcome message.
"""
print("\n" + "=" * 50)
print(" CODEALPHA BASIC CHATBOT")
print("=" * 50)
print("Hello! I'm your friendly chatbot.")
print("Type 'bye' anytime to end the conversation.")
print("=" * 50)
# ==========================
# Get Bot Response
# ==========================
def get_bot_response(user_message):
"""
Return the chatbot's response based on the user's message.
"""
user_message = user_message.lower().strip()
if user_message in RESPONSES:
return RESPONSES[user_message]
return (
"I'm sorry, I don't understand that yet. "
"Please try another question."
)
# ==========================
# Save Chat History
# ==========================
def save_chat_history(user_message, bot_response):
"""
Save each conversation to chat_history.txt.
"""
with open("chat_history.txt", "a") as file:
file.write(f"\nDate: {datetime.now()}\n")
file.write(f"You: {user_message}\n")
file.write(f"Bot: {bot_response}\n")
file.write("-" * 50 + "\n")
# ==========================
# Chat Loop
# ==========================
def chat():
"""
Start the chatbot conversation.
"""
while True:
user_message = input("\nYou: ").strip()
bot_response = get_bot_response(user_message)
print(f"Bot: {bot_response}")
save_chat_history(
user_message,
bot_response
)
if user_message.lower() == "bye":
break
# ==========================
# Main Program
# ==========================
def main():
"""
Main function that runs the chatbot.
"""
display_welcome()
chat()
print("\nThank you for chatting with me!")
# ==========================
# Run Program
# ==========================
if __name__ == "__main__":
main()