forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload_utils.py
More file actions
240 lines (167 loc) · 5.48 KB
/
payload_utils.py
File metadata and controls
240 lines (167 loc) · 5.48 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from typing import Dict, Any, Optional
# ------------------------------------------
# Public Utilities
# ------------------------------------------
# -------------------
# Events API
# -------------------
def to_event(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
return body["event"] if is_event(body) else None
def to_message(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if is_event(body) and body["event"]["type"] == "message":
return to_event(body)
return None
def is_event(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "event_callback")
and "event" in body
and "type" in body["event"]
)
def is_workflow_step_execute(body: Dict[str, Any]) -> bool:
return (
is_event(body)
and body["event"]["type"] == "workflow_step_execute"
and "workflow_step" in body["event"]
)
# -------------------
# Slash Commands
# -------------------
def to_command(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
return body if is_slash_command(body) else None
def is_slash_command(body: Dict[str, Any]) -> bool:
return body is not None and "command" in body
# -------------------
# Actions
# -------------------
def to_action(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if is_action(body):
if is_block_actions(body) or is_attachment_action(body):
return body["actions"][0]
else:
return body
return None
def is_action(body: Dict[str, Any]) -> bool:
return (
is_attachment_action(body)
or is_block_actions(body)
or is_dialog_submission(body)
or is_dialog_cancellation(body)
or is_workflow_step_edit(body)
)
def is_attachment_action(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "interactive_message")
and "callback_id" in body
)
def is_block_actions(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "block_actions")
and "actions" in body
)
def is_dialog_submission(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "dialog_submission")
and "callback_id" in body
)
def is_dialog_cancellation(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "dialog_cancellation")
and "callback_id" in body
)
def is_workflow_step_edit(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "workflow_step_edit")
and "callback_id" in body
)
# -------------------
# Options
# -------------------
def to_options(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if is_options(body):
return body
return None
def is_options(body: Dict[str, Any]) -> bool:
return is_block_suggestion(body) or is_dialog_suggestion(body)
def is_block_suggestion(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "block_suggestion")
and "action_id" in body
)
def is_dialog_suggestion(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "dialog_suggestion")
and "callback_id" in body
)
# -------------------
# Shortcut
# -------------------
def to_shortcut(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if is_shortcut(body):
return body
return None
def is_shortcut(body: Dict[str, Any]) -> bool:
return is_global_shortcut(body) or is_message_shortcut(body)
def is_global_shortcut(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "shortcut")
and "callback_id" in body
)
def is_message_shortcut(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "message_action")
and "callback_id" in body
)
# -------------------
# View
# -------------------
def to_view(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if is_view(body):
return body["view"]
return None
def is_view(body: Dict[str, Any]) -> bool:
return is_view_submission(body) or is_view_closed(body)
def is_view_submission(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "view_submission")
and "view" in body
and "callback_id" in body["view"]
)
def is_view_closed(body: Dict[str, Any]) -> bool:
return (
body is not None
and _is_expected_type(body, "view_closed")
and "view" in body
and "callback_id" in body["view"]
)
def is_workflow_step_save(body: Dict[str, Any]) -> bool:
return is_view_submission(body) and body["view"]["type"] == "workflow_step"
# -------------------
# Workflow Steps
# -------------------
def to_step(body: Dict[str, Any]) -> Optional[Dict[str, Any]]:
# edit
if is_workflow_step_edit(body):
return body["workflow_step"]
# save
if is_workflow_step_save(body):
return body["workflow_step"]
# execute
if is_workflow_step_execute(body):
return body["event"]["workflow_step"]
return None
# ------------------------------------------
# Internal Utilities
# ------------------------------------------
def _is_expected_type(body: dict, expected: str) -> bool:
return body is not None and "type" in body and body["type"] == expected