def _register_listener( self, func: Callable[..., BoltResponse], primary_matcher: ListenerMatcher, matchers: Optional[List[Callable[..., bool]]], middleware: Optional[List[Union[Callable, Middleware]]], auto_acknowledgement: bool = False, ) -> None: listener_matchers = [ CustomListenerMatcher(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, Middleware): listener_middleware.append(m) elif isinstance(m, Callable): listener_middleware.append( CustomMiddleware(app_name=self.name, func=m)) else: raise ValueError( f"Unexpected value for a listener middleware: {type(m)}") self._listeners.append( CustomListener( app_name=self.name, func=func, matchers=listener_matchers, middleware=listener_middleware, auto_acknowledgement=auto_acknowledgement, ))
def to_listener_middleware( app_name: str, middleware: Optional[List[Union[Callable, Middleware]]] ) -> List[Middleware]: _middleware = [] if middleware is not None: for m in middleware: if isinstance(m, Middleware): _middleware.append(m) elif isinstance(m, Callable): _middleware.append(CustomMiddleware(app_name=app_name, func=m)) else: raise ValueError(f"Invalid middleware: {type(m)}") return _middleware # type: ignore
def _build_save_listener_middleware() -> Middleware: def save_listener_middleware( context: BoltContext, client: WebClient, body: dict, next: Callable[[], BoltResponse], ): context["update"] = Update( client=client, body=body, ) return next() return CustomMiddleware(app_name=__name__, func=save_listener_middleware)
def _build_edit_listener_middleware(callback_id: str) -> Middleware: def edit_listener_middleware( context: BoltContext, client: WebClient, body: dict, next: Callable[[], BoltResponse], ): context["configure"] = Configure( callback_id=callback_id, client=client, body=body, ) return next() return CustomMiddleware(app_name=__name__, func=edit_listener_middleware)
def _build_execute_listener_middleware() -> Middleware: def execute_listener_middleware( context: BoltContext, client: WebClient, body: dict, next: Callable[[], BoltResponse], ): context["complete"] = Complete( client=client, body=body, ) context["fail"] = Fail( client=client, body=body, ) return next() return CustomMiddleware(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, Middleware): self._middleware_list.append(middleware_or_callable) elif isinstance(middleware_or_callable, Callable): self._middleware_list.append( CustomMiddleware(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, functions: Sequence[Callable[..., Optional[BoltResponse]]], primary_matcher: ListenerMatcher, matchers: Optional[Sequence[Callable[..., bool]]], middleware: Optional[Sequence[Union[Callable, Middleware]]], auto_acknowledgement: bool = False, ) -> Optional[Callable[..., 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] listener_matchers = [ CustomListenerMatcher(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, Middleware): listener_middleware.append(m) elif isinstance(m, Callable): listener_middleware.append( CustomMiddleware(app_name=self.name, func=m)) else: raise ValueError(error_unexpected_listener_middleware(type(m))) self._listeners.append( CustomListener( 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 middleware(self, *args): if len(args) > 0: func = args[0] self._middleware_list.append( CustomMiddleware(app_name=self.name, func=func))