-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
152 lines (103 loc) · 5.05 KB
/
parser.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
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
import html
import json
from urllib.parse import parse_qs
import requests
class Action:
clear = 'clear'
edit = 'edit'
order = 'order'
list = 'list'
class CallbackAction:
selected_dish = 'selected_dish'
remove = 'remove'
cancel = 'cancel'
class CallbackId:
edit = 'edit'
clear = 'clear'
AVAILABLE_ACTIONS = ('starter', 'main', 'dessert', 'extra', 'edit', 'clear', 'list', 'order')
AVAILABLE_DISH_TYPES = ('starter', 'main', 'dessert', 'extra')
class CommandParser():
def __init__(self, event, db_layer, response_layer):
self.body = parse_qs(event['body'])
if 'payload' in self.body: # Callback
self.payload = json.loads(self.body['payload'][0])
self.callback_id = self.payload['callback_id']
self.callback_action = self.payload['actions'][0]
self.team_id = self.payload['team']['id']
self.user_id = self.payload['user']['id']
self.user_name = self.payload['user']['name']
self.response_url = self.payload['response_url']
else: # Command
self.payload = None
self.team_id = self.body['team_id'][0]
self.user_id = self.body['user_id'][0]
self.user_name = self.body['user_name'][0]
self.command = self.body['text'][0]
self.db_layer = db_layer
self.response_layer = response_layer
def parse(self):
if self.payload:
result = self._parseCallback()
else:
result = self._parseCommand()
return result
def _parseCallback(self):
if self.callback_id == CallbackId.edit:
callback_action_name = self.callback_action['name']
if callback_action_name == CallbackAction.selected_dish:
data = self.db_layer.get_user_item(self.team_id, self.user_id)
selected_dish = html.unescape(self.callback_action['selected_options'][0]['value'])
callback_response_body = self.response_layer.edit_response(data, selected_dish)
elif callback_action_name == CallbackAction.remove:
selected_dish = html.unescape(self.callback_action['value'])
self.db_layer.remove_dish(self.team_id, self.user_id, selected_dish)
data = self.db_layer.get_user_item(self.team_id, self.user_id)
callback_response_body = self.response_layer.edit_response(data)
elif callback_action_name == CallbackAction.cancel:
data = self.db_layer.get_user_item(self.team_id, self.user_id)
callback_response_body = self.response_layer.list_user_dishes(data)
elif self.callback_id == CallbackId.clear:
callback_action_name = self.callback_action['name']
if callback_action_name == CallbackAction.cancel:
callback_response_body = self.response_layer.cancel_clear_response()
elif callback_action_name == CallbackAction.remove:
self.db_layer.delete_team(self.team_id)
callback_response_body = self.response_layer.confirm_clear_response()
requests.post(self.response_url, json=callback_response_body)
return self.response_layer.format_response()
def _parseCommand(self):
body = {}
if '/frichti' in self.command:
body = self.response_layer.several_commands_response()
else:
args = self.command.split(' ')
action = args[0]
if action not in AVAILABLE_ACTIONS:
body = self.response_layer.not_available_action()
if action in AVAILABLE_DISH_TYPES:
if len(args) == 1:
body = self.response_layer.no_dish()
else:
dishes = ' '.join(args[1:]).split(';')
dishes = [item.strip() for item in dishes]
self.db_layer.add_dish(self.team_id, self.user_id, self.user_name, action, *dishes)
body = self.response_layer.dishes_added_response()
elif action == Action.edit:
data = self.db_layer.get_user_item(self.team_id, self.user_id)
body = self.response_layer.edit_response(data)
elif action == Action.list:
if len(args) == 1:
data = self.db_layer.get_user_item(self.team_id, self.user_id)
body = self.response_layer.list_user_dishes(data)
else:
if args[1] == 'team':
data = self.db_layer.get_team_items(self.team_id)
body = self.response_layer.list_team_dishes(data)
else:
body = self.response_layer.not_available_list_action()
elif action == Action.order:
data = self.db_layer.get_team_items(self.team_id)
body = self.response_layer.order_response(data)
elif action == Action.clear:
body = self.response_layer.clear_db()
return self.response_layer.format_response(body)