Module slack_bolt.context.async_context

Classes

class AsyncBoltContext (*args, **kwargs)

Context object associated with a request from Slack.

Expand source code
class AsyncBoltContext(BaseContext):
    """Context object associated with a request from Slack."""

    def to_copyable(self) -> "AsyncBoltContext":
        new_dict = {}
        for prop_name, prop_value in self.items():
            if prop_name in self.copyable_standard_property_names:
                # all the standard properties are copiable
                new_dict[prop_name] = prop_value
            elif prop_name in self.non_copyable_standard_property_names:
                # Do nothing with this property (e.g., listener_runner)
                continue
            else:
                try:
                    copied_value = create_copy(prop_value)
                    new_dict[prop_name] = copied_value
                except TypeError as te:
                    self.logger.debug(
                        f"Skipped setting '{prop_name}' to a copied request for lazy listeners "
                        f"as it's not possible to make a deep copy (error: {te})"
                    )
        return AsyncBoltContext(new_dict)

    # The return type is intentionally string to avoid circular imports
    @property
    def listener_runner(self) -> "AsyncioListenerRunner":  # type: ignore[name-defined]
        """The properly configured listener_runner that is available for middleware/listeners."""
        return self["listener_runner"]

    @property
    def client(self) -> AsyncWebClient:
        """The `AsyncWebClient` instance available for this request.

            @app.event("app_mention")
            async def handle_events(context):
                await context.client.chat_postMessage(
                    channel=context.channel_id,
                    text="Thanks!",
                )

            # You can access "client" this way too.
            @app.event("app_mention")
            async def handle_events(client, context):
                await client.chat_postMessage(
                    channel=context.channel_id,
                    text="Thanks!",
                )

        Returns:
            `AsyncWebClient` instance
        """
        if "client" not in self:
            self["client"] = AsyncWebClient(token=None)
        return self["client"]

    @property
    def ack(self) -> AsyncAck:
        """`ack()` function for this request.

            @app.action("button")
            async def handle_button_clicks(context):
                await context.ack()

            # You can access "ack" this way too.
            @app.action("button")
            async def handle_button_clicks(ack):
                await ack()

        Returns:
            Callable `ack()` function
        """
        if "ack" not in self:
            self["ack"] = AsyncAck()
        return self["ack"]

    @property
    def say(self) -> AsyncSay:
        """`say()` function for this request.

            @app.action("button")
            async def handle_button_clicks(context):
                await context.ack()
                await context.say("Hi!")

            # You can access "ack" this way too.
            @app.action("button")
            async def handle_button_clicks(ack, say):
                await ack()
                await say("Hi!")

        Returns:
            Callable `say()` function
        """
        if "say" not in self:
            self["say"] = AsyncSay(client=self.client, channel=self.channel_id, thread_ts=self.thread_ts)
        return self["say"]

    @property
    def respond(self) -> Optional[AsyncRespond]:
        """`respond()` function for this request.

            @app.action("button")
            async def handle_button_clicks(context):
                await context.ack()
                await context.respond("Hi!")

            # You can access "ack" this way too.
            @app.action("button")
            async def handle_button_clicks(ack, respond):
                await ack()
                await respond("Hi!")

        Returns:
            Callable `respond()` function
        """
        if "respond" not in self:
            self["respond"] = AsyncRespond(
                response_url=self.response_url,
                proxy=self.client.proxy,
                ssl=self.client.ssl,
            )
        return self["respond"]

    @property
    def complete(self) -> AsyncComplete:
        """`complete()` function for this request. Once a custom function's state is set to complete,
        any outputs the function returns will be passed along to the next step of its housing workflow,
        or complete the workflow if the function is the last step in a workflow. Additionally,
        any interactivity handlers associated to a function invocation will no longer be invocable.

            @app.function("reverse")
            async def handle_button_clicks(ack, complete):
                await ack()
                await complete(outputs={"stringReverse":"olleh"})

            @app.function("reverse")
            async def handle_button_clicks(context):
                await context.ack()
                await context.complete(outputs={"stringReverse":"olleh"})

        Returns:
            Callable `complete()` function
        """
        if "complete" not in self:
            self["complete"] = AsyncComplete(client=self.client, function_execution_id=self.function_execution_id)
        return self["complete"]

    @property
    def fail(self) -> AsyncFail:
        """`fail()` function for this request. Once a custom function's state is set to error,
        its housing workflow will be interrupted and any provided error message will be passed
        on to the end user through SlackBot. Additionally, any interactivity handlers associated
        to a function invocation will no longer be invocable.

            @app.function("reverse")
            async def handle_button_clicks(ack, fail):
                await ack()
                await fail(error="something went wrong")

            @app.function("reverse")
            async def handle_button_clicks(context):
                await context.ack()
                await context.fail(error="something went wrong")

        Returns:
            Callable `fail()` function
        """
        if "fail" not in self:
            self["fail"] = AsyncFail(client=self.client, function_execution_id=self.function_execution_id)
        return self["fail"]

    @property
    def set_title(self) -> Optional[AsyncSetTitle]:
        return self.get("set_title")

    @property
    def set_status(self) -> Optional[AsyncSetStatus]:
        return self.get("set_status")

    @property
    def set_suggested_prompts(self) -> Optional[AsyncSetSuggestedPrompts]:
        return self.get("set_suggested_prompts")

    @property
    def get_thread_context(self) -> Optional[AsyncGetThreadContext]:
        return self.get("get_thread_context")

    @property
    def save_thread_context(self) -> Optional[AsyncSaveThreadContext]:
        return self.get("save_thread_context")

Ancestors

Instance variables

prop ackAsyncAck

ack() function for this request.

@app.action("button")
async def handle_button_clicks(context):
    await context.ack()

# You can access "ack" this way too.
@app.action("button")
async def handle_button_clicks(ack):
    await ack()

Returns

Callable ack() function

Expand source code
@property
def ack(self) -> AsyncAck:
    """`ack()` function for this request.

        @app.action("button")
        async def handle_button_clicks(context):
            await context.ack()

        # You can access "ack" this way too.
        @app.action("button")
        async def handle_button_clicks(ack):
            await ack()

    Returns:
        Callable `ack()` function
    """
    if "ack" not in self:
        self["ack"] = AsyncAck()
    return self["ack"]
prop client : slack_sdk.web.async_client.AsyncWebClient

The AsyncWebClient instance available for this request.

@app.event("app_mention")
async def handle_events(context):
    await context.client.chat_postMessage(
        channel=context.channel_id,
        text="Thanks!",
    )

# You can access "client" this way too.
@app.event("app_mention")
async def handle_events(client, context):
    await client.chat_postMessage(
        channel=context.channel_id,
        text="Thanks!",
    )

Returns

AsyncWebClient instance

Expand source code
@property
def client(self) -> AsyncWebClient:
    """The `AsyncWebClient` instance available for this request.

        @app.event("app_mention")
        async def handle_events(context):
            await context.client.chat_postMessage(
                channel=context.channel_id,
                text="Thanks!",
            )

        # You can access "client" this way too.
        @app.event("app_mention")
        async def handle_events(client, context):
            await client.chat_postMessage(
                channel=context.channel_id,
                text="Thanks!",
            )

    Returns:
        `AsyncWebClient` instance
    """
    if "client" not in self:
        self["client"] = AsyncWebClient(token=None)
    return self["client"]
prop completeAsyncComplete

complete() function for this request. Once a custom function's state is set to complete, any outputs the function returns will be passed along to the next step of its housing workflow, or complete the workflow if the function is the last step in a workflow. Additionally, any interactivity handlers associated to a function invocation will no longer be invocable.

@app.function("reverse")
async def handle_button_clicks(ack, complete):
    await ack()
    await complete(outputs={"stringReverse":"olleh"})

@app.function("reverse")
async def handle_button_clicks(context):
    await context.ack()
    await context.complete(outputs={"stringReverse":"olleh"})

Returns

Callable complete() function

Expand source code
@property
def complete(self) -> AsyncComplete:
    """`complete()` function for this request. Once a custom function's state is set to complete,
    any outputs the function returns will be passed along to the next step of its housing workflow,
    or complete the workflow if the function is the last step in a workflow. Additionally,
    any interactivity handlers associated to a function invocation will no longer be invocable.

        @app.function("reverse")
        async def handle_button_clicks(ack, complete):
            await ack()
            await complete(outputs={"stringReverse":"olleh"})

        @app.function("reverse")
        async def handle_button_clicks(context):
            await context.ack()
            await context.complete(outputs={"stringReverse":"olleh"})

    Returns:
        Callable `complete()` function
    """
    if "complete" not in self:
        self["complete"] = AsyncComplete(client=self.client, function_execution_id=self.function_execution_id)
    return self["complete"]
prop failAsyncFail

fail() function for this request. Once a custom function's state is set to error, its housing workflow will be interrupted and any provided error message will be passed on to the end user through SlackBot. Additionally, any interactivity handlers associated to a function invocation will no longer be invocable.

@app.function("reverse")
async def handle_button_clicks(ack, fail):
    await ack()
    await fail(error="something went wrong")

@app.function("reverse")
async def handle_button_clicks(context):
    await context.ack()
    await context.fail(error="something went wrong")

Returns

Callable fail() function

Expand source code
@property
def fail(self) -> AsyncFail:
    """`fail()` function for this request. Once a custom function's state is set to error,
    its housing workflow will be interrupted and any provided error message will be passed
    on to the end user through SlackBot. Additionally, any interactivity handlers associated
    to a function invocation will no longer be invocable.

        @app.function("reverse")
        async def handle_button_clicks(ack, fail):
            await ack()
            await fail(error="something went wrong")

        @app.function("reverse")
        async def handle_button_clicks(context):
            await context.ack()
            await context.fail(error="something went wrong")

    Returns:
        Callable `fail()` function
    """
    if "fail" not in self:
        self["fail"] = AsyncFail(client=self.client, function_execution_id=self.function_execution_id)
    return self["fail"]
prop get_thread_context : Optional[AsyncGetThreadContext]
Expand source code
@property
def get_thread_context(self) -> Optional[AsyncGetThreadContext]:
    return self.get("get_thread_context")
prop listener_runner : AsyncioListenerRunner

The properly configured listener_runner that is available for middleware/listeners.

Expand source code
@property
def listener_runner(self) -> "AsyncioListenerRunner":  # type: ignore[name-defined]
    """The properly configured listener_runner that is available for middleware/listeners."""
    return self["listener_runner"]
prop respond : Optional[AsyncRespond]

respond() function for this request.

@app.action("button")
async def handle_button_clicks(context):
    await context.ack()
    await context.respond("Hi!")

# You can access "ack" this way too.
@app.action("button")
async def handle_button_clicks(ack, respond):
    await ack()
    await respond("Hi!")

Returns

Callable respond() function

Expand source code
@property
def respond(self) -> Optional[AsyncRespond]:
    """`respond()` function for this request.

        @app.action("button")
        async def handle_button_clicks(context):
            await context.ack()
            await context.respond("Hi!")

        # You can access "ack" this way too.
        @app.action("button")
        async def handle_button_clicks(ack, respond):
            await ack()
            await respond("Hi!")

    Returns:
        Callable `respond()` function
    """
    if "respond" not in self:
        self["respond"] = AsyncRespond(
            response_url=self.response_url,
            proxy=self.client.proxy,
            ssl=self.client.ssl,
        )
    return self["respond"]
prop save_thread_context : Optional[AsyncSaveThreadContext]
Expand source code
@property
def save_thread_context(self) -> Optional[AsyncSaveThreadContext]:
    return self.get("save_thread_context")
prop sayAsyncSay

say() function for this request.

@app.action("button")
async def handle_button_clicks(context):
    await context.ack()
    await context.say("Hi!")

# You can access "ack" this way too.
@app.action("button")
async def handle_button_clicks(ack, say):
    await ack()
    await say("Hi!")

Returns

Callable say() function

Expand source code
@property
def say(self) -> AsyncSay:
    """`say()` function for this request.

        @app.action("button")
        async def handle_button_clicks(context):
            await context.ack()
            await context.say("Hi!")

        # You can access "ack" this way too.
        @app.action("button")
        async def handle_button_clicks(ack, say):
            await ack()
            await say("Hi!")

    Returns:
        Callable `say()` function
    """
    if "say" not in self:
        self["say"] = AsyncSay(client=self.client, channel=self.channel_id, thread_ts=self.thread_ts)
    return self["say"]
prop set_status : Optional[AsyncSetStatus]
Expand source code
@property
def set_status(self) -> Optional[AsyncSetStatus]:
    return self.get("set_status")
prop set_suggested_prompts : Optional[AsyncSetSuggestedPrompts]
Expand source code
@property
def set_suggested_prompts(self) -> Optional[AsyncSetSuggestedPrompts]:
    return self.get("set_suggested_prompts")
prop set_title : Optional[AsyncSetTitle]
Expand source code
@property
def set_title(self) -> Optional[AsyncSetTitle]:
    return self.get("set_title")

Methods

def to_copyable(self) ‑> AsyncBoltContext

Inherited members