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
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)
def _register_listener( self, functions: Sequence[Callable[..., Awaitable[Optional[BoltResponse]]]], primary_matcher: AsyncListenerMatcher, matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]], middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]], auto_acknowledgement: bool = False, ) -> Optional[Callable[..., Awaitable[Optional[BoltResponse]]]]: value_to_return = None if not isinstance(functions, list): functions = list(functions) if len(functions) == 1: # In the case where the function is registered using decorator, # the registration should return the original function. value_to_return = functions[0] for func in functions: if not inspect.iscoroutinefunction(func): name = get_name_for_callable(func) raise BoltError(error_listener_function_must_be_coro_func(name)) listener_matchers = [ AsyncCustomListenerMatcher(app_name=self.name, func=f) for f in (matchers or []) ] listener_matchers.insert(0, primary_matcher) listener_middleware = [] for m in middleware or []: if isinstance(m, AsyncMiddleware): listener_middleware.append(m) elif isinstance(m, Callable) and inspect.iscoroutinefunction(m): listener_middleware.append( AsyncCustomMiddleware(app_name=self.name, func=m) ) else: raise ValueError(error_unexpected_listener_middleware(type(m))) self._async_listeners.append( AsyncCustomListener( app_name=self.name, ack_function=functions.pop(0), lazy_functions=functions, matchers=listener_matchers, middleware=listener_middleware, auto_acknowledgement=auto_acknowledgement, ) ) return value_to_return
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)
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)
def middleware(self, *args) -> Optional[Callable]: """Registers a new middleware to this Bolt app. :param args: a list of middleware. Passing a single middleware is supported. :return: None """ if len(args) > 0: middleware_or_callable = args[0] if isinstance(middleware_or_callable, AsyncMiddleware): self._async_middleware_list.append(middleware_or_callable) elif isinstance(middleware_or_callable, Callable): self._async_middleware_list.append( AsyncCustomMiddleware(app_name=self.name, func=middleware_or_callable)) return middleware_or_callable else: raise BoltError( f"Unexpected type for a middleware ({type(middleware_or_callable)})" ) return None
def _register_listener( self, func: Callable[..., Awaitable[BoltResponse]], primary_matcher: AsyncListenerMatcher, matchers: Optional[List[Callable[..., Awaitable[bool]]]], middleware: Optional[List[Union[Callable, AsyncMiddleware]]], auto_acknowledgement: bool = False, ) -> None: if not inspect.iscoroutinefunction(func): name = func.__name__ raise BoltError( f"The listener function ({name}) is not a coroutine function.") listener_matchers = [ AsyncCustomListenerMatcher(app_name=self.name, func=f) for f in (matchers or []) ] listener_matchers.insert(0, primary_matcher) listener_middleware = [] for m in middleware or []: if isinstance(m, AsyncMiddleware): listener_middleware.append(m) elif isinstance(m, Callable) and inspect.iscoroutinefunction(m): listener_middleware.append( AsyncCustomMiddleware(app_name=self.name, func=m)) else: raise ValueError( f"async function is required for AsyncApp's listener middleware: {type(m)}" ) self._async_listeners.append( AsyncCustomListener( app_name=self.name, func=func, matchers=listener_matchers, middleware=listener_middleware, auto_acknowledgement=auto_acknowledgement, ))
def middleware(self, *args): if len(args) > 0: func = args[0] self._async_middleware_list.append( AsyncCustomMiddleware(app_name=self.name, func=func))