-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot2.py
60 lines (49 loc) · 1.96 KB
/
chatbot2.py
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
from datetime import datetime
import time
class EventDrivenChatBot:
def __init__(self):
# accepted_messages maps incoming messages to
# list of callback functions
self.accepted_messages = {}
# time of instantiation
self.birth_time = datetime.now()
# "registering" all callbacks
self.register_callback("hi",
self.respond_to_greeting)
self.register_callback("bye",
self.respond_to_departure)
self.register_callback("age?",
self.respond_to_age_request)
self.register_callback("age?",
self.respond_to_age_request_detailed)
def register_callback(self, message, callback):
"""
Registers a callback to a message.
"""
if message not in self.accepted_messages:
self.accepted_messages[message] = []
self.accepted_messages[message].append(callback)
def respond_to_greeting(self):
print("Hello!")
def respond_to_departure(self):
print("Nice chatting with you!")
def respond_to_age_request(self):
age = datetime.now() - self.birth_time
print("I am", age.seconds, "seconds old.")
def respond_to_age_request_detailed(self):
age = datetime.now() - self.birth_time
micros = age.microseconds
print("Technically, I'm", age.seconds, "seconds and",
micros, "microseconds old")
def handle_message(self, message):
if message not in self.accepted_messages:
print("sorry, I don't understand", message)
else:
callbacks = self.accepted_messages[message]
for callback in callbacks:
callback()
bot = EventDrivenChatBot()
bot.handle_message("hi")
time.sleep(2.2)
bot.handle_message("age?")
bot.handle_message("bye")