-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrokBot.py
More file actions
96 lines (83 loc) · 3.64 KB
/
GrokBot.py
File metadata and controls
96 lines (83 loc) · 3.64 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
import sys
import re
import random
from datetime import datetime
# Check Python version
if sys.version_info < (3, 6):
print("Error: Python 3.6 or higher is required.")
sys.exit(1)
# Define pattern-response pairs for the chatbot
patterns = [
# Greetings
(r'(hi|hello|hey|greetings)(\W|$)',
['Hello! How can I assist you today?', 'Hi there! What’s on your mind?', 'Hey! Ready to chat?']),
# Farewells
(r'(bye|goodbye|see you|exit|quit)(\W|$)',
['Goodbye! Have a great day!', 'See you later!', 'Bye bye!']),
# Questions about the chatbot
(r'(who|what) (are|is) (you|this)(\W|$)',
['I’m GrokBot, a simple chatbot built with Python!', 'I’m a Python-powered chatbot here to answer your questions!']),
# Questions about time
(r'(what|tell) (is|me) (the|) (time|date)(\W|$)',
[f"The current time is {datetime.now().strftime('%H:%M:%S')} on {datetime.now().strftime('%Y-%m-%d')}."]),
# Questions about weather (mock responses, as no API is used)
(r'(weather|temperature) (in|for|at) (\w+)',
['I’m not connected to a weather API, but it’s probably sunny somewhere!',
'Weather info isn’t available, but imagine a nice breeze in {}!',
'Can’t fetch the weather for {}, but it’s perfect for coding!']),
# General knowledge questions about Python
(r'(what|who) is (python|Python)(\W|$)',
['Python is a versatile programming language used for web development, data science, and more!',
'Python? It’s the cool snake of programming languages, super easy to learn!']),
# Fallback for unknown queries
(r'.*',
['I’m not sure about that one. Try asking about the weather, time, or Python!',
'Hmm, could you rephrase that? I’m all ears… or rather, all text!',
'Not sure how to answer that. What else can I help with?'])
]
# Function to preprocess user input (simple lowercase conversion)
def preprocess_input(user_input):
try:
return user_input.lower().strip()
except Exception as e:
print(f"Error processing input: {e}")
return user_input.lower()
# Function to find a response based on user input
def get_response(user_input):
processed_input = preprocess_input(user_input)
for pattern, responses in patterns:
match = re.search(pattern, processed_input, re.IGNORECASE)
if match:
response = random.choice(responses)
if '{}' in response and len(match.groups()) >= 3:
return response.format(match.group(3))
return response
return random.choice(patterns[-1][1])
# Main chatbot function
def chatbot():
print("Welcome to GrokBot! Type 'exit' or 'quit' to stop.")
print(f"Current time: {datetime.now().strftime('%H:%M:%S %Z')} on {datetime.now().strftime('%Y-%m-%d')}")
while True:
try:
user_input = input("You: ")
if user_input.lower() in ['exit', 'quit', 'bye', 'goodbye']:
print("GrokBot: Goodbye! Thanks for chatting!")
break
response = get_response(user_input)
print(f"GrokBot: {response}")
except (EOFError, KeyboardInterrupt):
print("GrokBot: Goodbye! Thanks for chatting!")
break
except Exception as e:
print(f"Error: {e}")
print("Please try again.")
continue
# Run the chatbot
if __name__ == "__main__":
print("Starting GrokBot...")
try:
chatbot()
except Exception as e:
print(f"Fatal error: {e}")
print("Please ensure you are running this script in a terminal or command prompt with Python 3.6+.")
sys.exit(1)