forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_step.py
More file actions
407 lines (355 loc) · 13.7 KB
/
async_step.py
File metadata and controls
407 lines (355 loc) · 13.7 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from functools import wraps
from typing import Callable, Union, Optional, Awaitable, Sequence, List, Pattern
from slack_sdk.web.async_client import AsyncWebClient
from slack_bolt.context.async_context import AsyncBoltContext
from slack_bolt.listener.async_listener import AsyncListener, AsyncCustomListener
from slack_bolt.listener_matcher.builtins import (
workflow_step_edit,
workflow_step_save,
workflow_step_execute,
)
from slack_bolt.middleware.async_custom_middleware import AsyncCustomMiddleware
from slack_bolt.response import BoltResponse
from .internals import _is_used_without_argument
from .utilities.async_complete import AsyncComplete
from .utilities.async_configure import AsyncConfigure
from .utilities.async_fail import AsyncFail
from .utilities.async_update import AsyncUpdate
from ...error import BoltError
from ...listener_matcher.async_listener_matcher import (
AsyncListenerMatcher,
AsyncCustomListenerMatcher,
)
from ...middleware.async_middleware import AsyncMiddleware
class AsyncWorkflowStepBuilder:
callback_id: Union[str, Pattern]
_edit: Optional[AsyncListener]
_save: Optional[AsyncListener]
_execute: Optional[AsyncListener]
def __init__(
self,
callback_id: Union[str, Pattern],
app_name: Optional[str] = None,
):
"""This builder is supposed to be used as decorator.
my_step = AsyncWorkflowStep.builder("my_step")
@my_step.edit
async def edit_my_step(ack, configure):
pass
@my_step.save
async def save_my_step(ack, step, update):
pass
@my_step.execute
async def execute_my_step(step, complete, fail):
pass
app.step(my_step)
:param callback_id: the callback_id for the workflow
:param app_name: the application name mainly for logging
"""
self.callback_id = callback_id
self.app_name = app_name or __name__
self._edit = None
self._save = None
self._execute = None
def edit(
self,
*args,
matchers: Optional[
Union[Callable[..., Awaitable[bool]], AsyncListenerMatcher]
] = None,
middleware: Optional[Union[Callable, AsyncMiddleware]] = None,
lazy: Optional[List[Callable[..., Awaitable[None]]]] = None,
):
"""Register a new edit listener with details.
You can use this method as decorator as well.
@my_step.edit
def edit_my_step(ack, configure):
pass
It's also possible to add additional listener matchers and/or middleware
@my_step.edit(matchers=[is_valid], middleware=[update_context])
def edit_my_step(ack, configure):
pass
"""
if _is_used_without_argument(args):
func = args[0]
self._edit = self._to_listener("edit", func, matchers, middleware)
return func
def _inner(func):
functions = [func] + (lazy if lazy is not None else [])
self._edit = self._to_listener("edit", functions, matchers, middleware)
@wraps(func)
async def _wrapper(*args, **kwargs):
return await func(*args, **kwargs)
return _wrapper
return _inner
def save(
self,
*args,
matchers: Optional[
Union[Callable[..., Awaitable[bool]], AsyncListenerMatcher]
] = None,
middleware: Optional[Union[Callable, AsyncMiddleware]] = None,
lazy: Optional[List[Callable[..., Awaitable[None]]]] = None,
):
"""Register a new save listener with details.
You can use this method as decorator as well.
@my_step.save
def save_my_step(ack, step, update):
pass
It's also possible to add additional listener matchers and/or middleware
@my_step.save(matchers=[is_valid], middleware=[update_context])
def save_my_step(ack, step, update):
pass
"""
if _is_used_without_argument(args):
func = args[0]
self._save = self._to_listener("save", func, matchers, middleware)
return func
def _inner(func):
functions = [func] + (lazy if lazy is not None else [])
self._save = self._to_listener("save", functions, matchers, middleware)
@wraps(func)
async def _wrapper(*args, **kwargs):
return await func(*args, **kwargs)
return _wrapper
return _inner
def execute(
self,
*args,
matchers: Optional[
Union[Callable[..., Awaitable[bool]], AsyncListenerMatcher]
] = None,
middleware: Optional[Union[Callable, AsyncMiddleware]] = None,
lazy: Optional[List[Callable[..., Awaitable[None]]]] = None,
):
"""Register a new execute listener with details.
You can use this method as decorator as well.
@my_step.execute
def execute_my_step(step, complete, fail):
pass
It's also possible to add additional listener matchers and/or middleware
@my_step.save(matchers=[is_valid], middleware=[update_context])
def execute_my_step(step, complete, fail):
pass
"""
if _is_used_without_argument(args):
func = args[0]
self._execute = self._to_listener("execute", func, matchers, middleware)
return func
def _inner(func):
functions = [func] + (lazy if lazy is not None else [])
self._execute = self._to_listener(
"execute", functions, matchers, middleware
)
@wraps(func)
async def _wrapper(*args, **kwargs):
return await func(*args, **kwargs)
return _wrapper
return _inner
def build(self) -> "AsyncWorkflowStep":
"""Constructs a WorkflowStep object. This method may raise an exception
if the builder doesn't have enough configurations to build the object.
:return: WorkflowStep object
"""
if self._edit is None:
raise BoltError(f"edit listener is not registered")
if self._save is None:
raise BoltError(f"save listener is not registered")
if self._execute is None:
raise BoltError(f"execute listener is not registered")
return AsyncWorkflowStep(
callback_id=self.callback_id,
edit=self._edit,
save=self._save,
execute=self._execute,
app_name=self.app_name,
)
# ---------------------------------------
def _to_listener(
self,
name: str,
listener_or_functions: Union[AsyncListener, Callable, List[Callable]],
matchers: Optional[
Union[Callable[..., Awaitable[bool]], AsyncListenerMatcher]
] = None,
middleware: Optional[Union[Callable, AsyncMiddleware]] = None,
) -> AsyncListener:
return AsyncWorkflowStep.build_listener(
callback_id=self.callback_id,
app_name=self.app_name,
listener_or_functions=listener_or_functions,
name=name,
matchers=self.to_listener_matchers(self.app_name, matchers),
middleware=self.to_listener_middleware(self.app_name, middleware),
)
@staticmethod
def to_listener_matchers(
app_name: str,
matchers: Optional[
List[Union[Callable[..., Awaitable[bool]], AsyncListenerMatcher]]
],
) -> List[AsyncListenerMatcher]:
_matchers = []
if matchers is not None:
for m in matchers:
if isinstance(m, AsyncListenerMatcher):
_matchers.append(m)
elif isinstance(m, Callable):
_matchers.append(
AsyncCustomListenerMatcher(app_name=app_name, func=m)
)
else:
raise ValueError(f"Invalid matcher: {type(m)}")
return _matchers # type: ignore
@staticmethod
def to_listener_middleware(
app_name: str, middleware: Optional[List[Union[Callable, AsyncMiddleware]]]
) -> List[AsyncMiddleware]:
_middleware = []
if middleware is not None:
for m in middleware:
if isinstance(m, AsyncMiddleware):
_middleware.append(m)
elif isinstance(m, Callable):
_middleware.append(AsyncCustomMiddleware(app_name=app_name, func=m))
else:
raise ValueError(f"Invalid middleware: {type(m)}")
return _middleware # type: ignore
class AsyncWorkflowStep:
callback_id: Union[str, Pattern]
edit: AsyncListener
save: AsyncListener
execute: AsyncListener
def __init__(
self,
*,
callback_id: Union[str, Pattern],
edit: Union[
Callable[..., Awaitable[BoltResponse]], AsyncListener, Sequence[Callable]
],
save: Union[
Callable[..., Awaitable[BoltResponse]], AsyncListener, Sequence[Callable]
],
execute: Union[
Callable[..., Awaitable[BoltResponse]], AsyncListener, Sequence[Callable]
],
app_name: Optional[str] = None,
):
self.callback_id = callback_id
app_name = app_name or __name__
self.edit = self.build_listener(callback_id, app_name, edit, "edit")
self.save = self.build_listener(callback_id, app_name, save, "save")
self.execute = self.build_listener(callback_id, app_name, execute, "execute")
@classmethod
def builder(cls, callback_id: Union[str, Pattern]) -> AsyncWorkflowStepBuilder:
return AsyncWorkflowStepBuilder(callback_id)
@classmethod
def build_listener(
cls,
callback_id: Union[str, Pattern],
app_name: str,
listener_or_functions: Union[AsyncListener, Callable, List[Callable]],
name: str,
matchers: Optional[List[AsyncListenerMatcher]] = None,
middleware: Optional[List[AsyncMiddleware]] = None,
):
if listener_or_functions is None:
raise BoltError(f"{name} listener is required (callback_id: {callback_id})")
if isinstance(listener_or_functions, Callable):
listener_or_functions = [listener_or_functions]
if isinstance(listener_or_functions, AsyncListener):
return listener_or_functions
elif isinstance(listener_or_functions, list):
matchers = matchers if matchers else []
matchers.insert(0, cls._build_primary_matcher(name, callback_id))
middleware = middleware if middleware else []
middleware.insert(0, cls._build_single_middleware(name, callback_id))
functions = listener_or_functions
ack_function = functions.pop(0)
return AsyncCustomListener(
app_name=app_name,
matchers=matchers,
middleware=middleware,
ack_function=ack_function,
lazy_functions=functions,
auto_acknowledgement=name == "execute",
)
else:
raise BoltError(
f"Invalid {name} listener: {type(listener_or_functions)} detected (callback_id: {callback_id})"
)
@classmethod
def _build_primary_matcher(
cls, name: str, callback_id: str
) -> AsyncListenerMatcher:
if name == "edit":
return workflow_step_edit(callback_id, asyncio=True)
elif name == "save":
return workflow_step_save(callback_id, asyncio=True)
elif name == "execute":
return workflow_step_execute(callback_id, asyncio=True)
else:
raise ValueError(f"Invalid name {name}")
@classmethod
def _build_single_middleware(cls, name: str, callback_id: str) -> AsyncMiddleware:
if name == "edit":
return _build_edit_listener_middleware(callback_id)
elif name == "save":
return _build_save_listener_middleware()
elif name == "execute":
return _build_execute_listener_middleware()
else:
raise ValueError(f"Invalid name {name}")
#######################
# Edit
#######################
def _build_edit_listener_middleware(callback_id: str) -> AsyncMiddleware:
async def edit_listener_middleware(
context: AsyncBoltContext,
client: AsyncWebClient,
body: dict,
next: Callable[[], Awaitable[BoltResponse]],
):
context["configure"] = AsyncConfigure(
callback_id=callback_id,
client=client,
body=body,
)
return await next()
return AsyncCustomMiddleware(app_name=__name__, func=edit_listener_middleware)
#######################
# Save
#######################
def _build_save_listener_middleware() -> AsyncMiddleware:
async def save_listener_middleware(
context: AsyncBoltContext,
client: AsyncWebClient,
body: dict,
next: Callable[[], Awaitable[BoltResponse]],
):
context["update"] = AsyncUpdate(
client=client,
body=body,
)
return await next()
return AsyncCustomMiddleware(app_name=__name__, func=save_listener_middleware)
#######################
# Execute
#######################
def _build_execute_listener_middleware() -> AsyncMiddleware:
async def execute_listener_middleware(
context: AsyncBoltContext,
client: AsyncWebClient,
body: dict,
next: Callable[[], Awaitable[BoltResponse]],
):
context["complete"] = AsyncComplete(
client=client,
body=body,
)
context["fail"] = AsyncFail(
client=client,
body=body,
)
return await next()
return AsyncCustomMiddleware(app_name=__name__, func=execute_listener_middleware)