-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.py
More file actions
153 lines (117 loc) · 5.42 KB
/
Copy pathBot.py
File metadata and controls
153 lines (117 loc) · 5.42 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import datetime
import time
import sys
# ─────────────────────────────────────────────
# CONFIGURATION
# ─────────────────────────────────────────────
TYPING_SPEED = 0.03 # seconds per character (set to 0 to disable)
RESPONSES = {
# Greetings
"hello": "Hi there! How can I help you today?",
"hi": "Hey! Great to see you. What's on your mind?",
"hey": "Hey! How can I assist you?",
# Status
"how are you": "I'm doing great, thanks for asking! How about you?",
"what's up": "Not much, just here to help you! What do you need?",
# Identity
"who are you": "I'm a smart AI chatbot, here to assist you with your questions!",
"what is your name": "You can call me TaskBot. Nice to meet you!",
"what can you do": "I can answer questions, motivate you, and have a conversation. Try me!",
# Motivation
"motivate me": "Every bug you fix makes you a better developer. Keep pushing — greatness is just one commit away! 🚀",
"i am sad": "I'm sorry to hear that. Remember, tough times don't last but tough people do. You've got this! 💪",
"i am tired": "Rest is part of the process. Take a break, recharge, and come back stronger!",
"i give up": "Don't give up! The most successful people failed the most. Try one more time.",
# Emotions
"happy": "That's amazing! Keep that energy going! 😊",
"i am happy": "Wonderful! Happiness looks great on you. 😄",
"i am bored": "Let's fix that! Ask me something interesting, or try: 'motivate me'.",
"i am excited": "Love the energy! What are you excited about? 🎉",
# Tech / Programming
"functions kya hote hai": "Functions are reusable blocks of code that perform a specific task. Chapter 7 mein detail se padho! 📖",
"what is python": "Python is a high-level, easy-to-learn programming language used in web dev, AI, automation, and much more!",
"what is a loop": "A loop repeats a block of code multiple times. Python has 'for' and 'while' loops.",
"what is a variable": "A variable is a container that stores data values. Example: name = 'Kunal'",
# Farewells
"bye": "Goodbye! It was great chatting with you. Take care! 👋",
"goodbye": "See you soon! Have a wonderful day! 😊",
"see you": "See you later! Come back anytime. 👋",
# Thanks
"thank you": "You're welcome! Happy to help anytime. 😊",
"thanks": "Anytime! That's what I'm here for.",
}
FALLBACK_RESPONSES = [
"Hmm, I'm not sure about that yet. I'm still learning!",
"That's a great question, but it's beyond my knowledge for now.",
"I don't have an answer for that yet. Try asking something else!",
"I'm still growing! Ask me something else.",
]
_fallback_index = 0
# ─────────────────────────────────────────────
# UTILITIES
# ─────────────────────────────────────────────
def type_print(text: str, speed: float = TYPING_SPEED):
"""Print text with a typewriter effect."""
if speed == 0:
print(text)
return
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(speed)
print()
def get_greeting(hour: int) -> str:
"""Return a time-appropriate greeting."""
if 5 <= hour < 12:
return "Good morning"
elif 12 <= hour < 17:
return "Good afternoon"
elif 17 <= hour < 21:
return "Good evening"
else:
return "Good night"
def get_bot_response(user_input: str) -> str:
"""Match user input against known responses using keyword matching."""
global _fallback_index
normalized = user_input.lower().strip()
for keyword, response in RESPONSES.items():
if keyword in normalized:
return response
# Rotate through fallback responses
reply = FALLBACK_RESPONSES[_fallback_index % len(FALLBACK_RESPONSES)]
_fallback_index += 1
return reply
def print_divider():
print("─" * 45)
# ─────────────────────────────────────────────
# MAIN
# ─────────────────────────────────────────────
def main():
# Welcome & name
print_divider()
name = input(" Swagat hai! Enter your name: ").strip() or "Friend"
hour = datetime.datetime.now().hour
greeting = get_greeting(hour)
print_divider()
type_print(f" {greeting}, {name}! 👋")
type_print(" Namaste! Welcome to TaskBot 🤖")
type_print(" Ask me anything. Type 'bye' to exit.")
print_divider()
print()
# Conversation loop
while True:
try:
user_input = input("You: ").strip()
except (KeyboardInterrupt, EOFError):
print()
type_print("Bot: Goodbye! Take care. 👋")
break
if not user_input:
print("Bot: Please type something!\n")
continue
response = get_bot_response(user_input)
type_print(f"Bot: {response}\n")
if any(word in user_input.lower() for word in ["bye", "goodbye", "see you"]):
break
if __name__ == "__main__":
main()