Module slack_bolt.listener.async_listener

Classes

class AsyncCustomListener (*, app_name: str, ack_function: Callable[..., Awaitable[Optional[BoltResponse]]], lazy_functions: Sequence[Callable[..., Awaitable[None]]], matchers: Sequence[AsyncListenerMatcher], middleware: Sequence[AsyncMiddleware], auto_acknowledgement: bool = False, base_logger: Optional[logging.Logger] = None)
Expand source code
class AsyncCustomListener(AsyncListener):
    app_name: str
    ack_function: Callable[..., Awaitable[Optional[BoltResponse]]]  # type: ignore[assignment]
    lazy_functions: Sequence[Callable[..., Awaitable[None]]]
    matchers: Sequence[AsyncListenerMatcher]
    middleware: Sequence[AsyncMiddleware]
    auto_acknowledgement: bool
    arg_names: MutableSequence[str]
    logger: Logger

    def __init__(
        self,
        *,
        app_name: str,
        ack_function: Callable[..., Awaitable[Optional[BoltResponse]]],
        lazy_functions: Sequence[Callable[..., Awaitable[None]]],
        matchers: Sequence[AsyncListenerMatcher],
        middleware: Sequence[AsyncMiddleware],
        auto_acknowledgement: bool = False,
        base_logger: Optional[Logger] = None,
    ):
        self.app_name = app_name
        self.ack_function = ack_function
        self.lazy_functions = lazy_functions
        self.matchers = matchers
        self.middleware = middleware
        self.auto_acknowledgement = auto_acknowledgement
        self.arg_names = get_arg_names_of_callable(ack_function)
        self.logger = get_bolt_app_logger(app_name, self.ack_function, base_logger)

    async def run_ack_function(
        self,
        *,
        request: AsyncBoltRequest,
        response: BoltResponse,
    ) -> Optional[BoltResponse]:
        return await self.ack_function(
            **build_async_required_kwargs(
                logger=self.logger,
                required_arg_names=self.arg_names,
                request=request,
                response=response,
                this_func=self.ack_function,
            )
        )

Ancestors

Class variables

var ack_function : Callable[..., Awaitable[Optional[BoltResponse]]]
var app_name : str
var arg_names : MutableSequence[str]
var auto_acknowledgement : bool
var lazy_functions : Sequence[Callable[..., Awaitable[None]]]
var logger : logging.Logger
var matchers : Sequence[AsyncListenerMatcher]
var middleware : Sequence[AsyncMiddleware]

Methods

async def run_ack_function(self, *, request: AsyncBoltRequest, response: BoltResponse) ‑> Optional[BoltResponse]

Runs all the registered middleware and then run the listener function.

Args

request
The incoming request
response
The current response

Returns

The processed response

class cls (*, app_name: str, ack_function: Callable[..., Awaitable[Optional[BoltResponse]]], lazy_functions: Sequence[Callable[..., Awaitable[None]]], matchers: Sequence[AsyncListenerMatcher], middleware: Sequence[AsyncMiddleware], auto_acknowledgement: bool = False, base_logger: Optional[logging.Logger] = None)
Expand source code
class AsyncCustomListener(AsyncListener):
    app_name: str
    ack_function: Callable[..., Awaitable[Optional[BoltResponse]]]  # type: ignore[assignment]
    lazy_functions: Sequence[Callable[..., Awaitable[None]]]
    matchers: Sequence[AsyncListenerMatcher]
    middleware: Sequence[AsyncMiddleware]
    auto_acknowledgement: bool
    arg_names: MutableSequence[str]
    logger: Logger

    def __init__(
        self,
        *,
        app_name: str,
        ack_function: Callable[..., Awaitable[Optional[BoltResponse]]],
        lazy_functions: Sequence[Callable[..., Awaitable[None]]],
        matchers: Sequence[AsyncListenerMatcher],
        middleware: Sequence[AsyncMiddleware],
        auto_acknowledgement: bool = False,
        base_logger: Optional[Logger] = None,
    ):
        self.app_name = app_name
        self.ack_function = ack_function
        self.lazy_functions = lazy_functions
        self.matchers = matchers
        self.middleware = middleware
        self.auto_acknowledgement = auto_acknowledgement
        self.arg_names = get_arg_names_of_callable(ack_function)
        self.logger = get_bolt_app_logger(app_name, self.ack_function, base_logger)

    async def run_ack_function(
        self,
        *,
        request: AsyncBoltRequest,
        response: BoltResponse,
    ) -> Optional[BoltResponse]:
        return await self.ack_function(
            **build_async_required_kwargs(
                logger=self.logger,
                required_arg_names=self.arg_names,
                request=request,
                response=response,
                this_func=self.ack_function,
            )
        )

Ancestors

Class variables

var ack_function : Callable[..., Awaitable[Optional[BoltResponse]]]
var app_name : str
var arg_names : MutableSequence[str]
var auto_acknowledgement : bool
var lazy_functions : Sequence[Callable[..., Awaitable[None]]]
var logger : logging.Logger
var matchers : Sequence[AsyncListenerMatcher]
var middleware : Sequence[AsyncMiddleware]

Inherited members

class AsyncListener
Expand source code
class AsyncListener(metaclass=ABCMeta):
    matchers: Sequence[AsyncListenerMatcher]
    middleware: Sequence[AsyncMiddleware]
    ack_function: Callable[..., Awaitable[BoltResponse]]
    lazy_functions: Sequence[Callable[..., Awaitable[None]]]
    auto_acknowledgement: bool

    async def async_matches(
        self,
        *,
        req: AsyncBoltRequest,
        resp: BoltResponse,
    ) -> bool:
        is_matched: bool = False
        for matcher in self.matchers:
            is_matched = await matcher.async_matches(req, resp)
            if not is_matched:
                return is_matched
        return is_matched

    async def run_async_middleware(
        self,
        *,
        req: AsyncBoltRequest,
        resp: BoltResponse,
    ) -> Tuple[Optional[BoltResponse], bool]:
        """Runs an async middleware.

        Args:
            req: The incoming request
            resp: The current response

        Returns:
            A tuple of the processed response and a flag indicating termination
        """
        for m in self.middleware:
            middleware_state = {"next_called": False}

            async def _next():
                middleware_state["next_called"] = True

            resp = await m.async_process(req=req, resp=resp, next=_next)  # type: ignore[assignment]
            if not middleware_state["next_called"]:
                # next() was not called in this middleware
                return (resp, True)
        return (resp, False)

    @abstractmethod
    async def run_ack_function(self, *, request: AsyncBoltRequest, response: BoltResponse) -> Optional[BoltResponse]:
        """Runs all the registered middleware and then run the listener function.

        Args:
            request: The incoming request
            response: The current response

        Returns:
            The processed response
        """
        raise NotImplementedError()

Subclasses

Class variables

var ack_function : Callable[..., Awaitable[BoltResponse]]
var auto_acknowledgement : bool
var lazy_functions : Sequence[Callable[..., Awaitable[None]]]
var matchers : Sequence[AsyncListenerMatcher]
var middleware : Sequence[AsyncMiddleware]

Methods

async def async_matches(self, *, req: AsyncBoltRequest, resp: BoltResponse) ‑> bool
async def run_ack_function(self, *, request: AsyncBoltRequest, response: BoltResponse) ‑> Optional[BoltResponse]

Runs all the registered middleware and then run the listener function.

Args

request
The incoming request
response
The current response

Returns

The processed response

async def run_async_middleware(self, *, req: AsyncBoltRequest, resp: BoltResponse) ‑> Tuple[Optional[BoltResponse], bool]

Runs an async middleware.

Args

req
The incoming request
resp
The current response

Returns

A tuple of the processed response and a flag indicating termination