-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa.py
33 lines (26 loc) · 926 Bytes
/
alexa.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
class RequestHandler:
def can_handle(self, request):
return False
def handle(self, request):
raise NotImplementedError
class ErrorHandler:
def handle(self, request, exception):
raise NotImplementedError
# Not really necessary for such a small project
# But it's a way of making it easier to add new ways to interact with a skill
class Alexa:
def __init__(self):
self.handlers = []
def add_handler(self, handler):
self.handlers.append(handler)
return self
def add_error_handler(self, handler):
self.error_handler = handler
return self
def handle_request(self, request):
try:
for handler in self.handlers:
if handler.can_handle(request):
return handler.handle(request)
except Exception as e:
return self.error_handler.handle(request, e)