Module slack_bolt.context.context

Classes

class BoltContext (*args, **kwargs)

Context object associated with a request from Slack.

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

    def to_copyable(self) -> "BoltContext":
        new_dict = {}
        for prop_name, prop_value in self.items():
            if prop_name in self.standard_property_names:
                # all the standard properties are copiable
                new_dict[prop_name] = prop_value
            else:
                try:
                    copied_value = create_copy(prop_value)
                    new_dict[prop_name] = copied_value
                except TypeError as te:
                    self.logger.warning(
                        f"Skipped setting '{prop_name}' to a copied request for lazy listeners "
                        "due to a deep-copy creation error. Consider passing the value not as part of context object "
                        f"(error: {te})"
                    )
        return BoltContext(new_dict)

    @property
    def client(self) -> Optional[WebClient]:
        """The `WebClient` instance available for this request.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        Returns:
            Callable `respond()` function
        """
        if "respond" not in self:
            self["respond"] = Respond(
                response_url=self.response_url,
                proxy=self.client.proxy,  # type: ignore[union-attr]
                ssl=self.client.ssl,  # type: ignore[union-attr]
            )
        return self["respond"]

    @property
    def complete(self) -> Complete:
        """`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")
            def handle_button_clicks(ack, complete):
                ack()
                complete(outputs={"stringReverse":"olleh"})

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

        Returns:
            Callable `complete()` function
        """
        if "complete" not in self:
            self["complete"] = Complete(
                client=self.client, function_execution_id=self.function_execution_id  # type: ignore[arg-type]
            )
        return self["complete"]

    @property
    def fail(self) -> Fail:
        """`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")
            def handle_button_clicks(ack, fail):
                ack()
                fail(error="something went wrong")

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

        Returns:
            Callable `fail()` function
        """
        if "fail" not in self:
            self["fail"] = Fail(
                client=self.client, function_execution_id=self.function_execution_id  # type: ignore[arg-type]
            )
        return self["fail"]

Ancestors

Instance variables

prop ackAck

ack() function for this request.

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

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

Returns

Callable ack() function

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

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

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

    Returns:
        Callable `ack()` function
    """
    if "ack" not in self:
        self["ack"] = Ack()
    return self["ack"]
prop client : Optional[slack_sdk.web.client.WebClient]

The WebClient instance available for this request.

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

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

Returns

WebClient instance

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

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

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

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

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")
def handle_button_clicks(ack, complete):
    ack()
    complete(outputs={"stringReverse":"olleh"})

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

Returns

Callable complete() function

Expand source code
@property
def complete(self) -> Complete:
    """`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")
        def handle_button_clicks(ack, complete):
            ack()
            complete(outputs={"stringReverse":"olleh"})

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

    Returns:
        Callable `complete()` function
    """
    if "complete" not in self:
        self["complete"] = Complete(
            client=self.client, function_execution_id=self.function_execution_id  # type: ignore[arg-type]
        )
    return self["complete"]
prop failFail

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")
def handle_button_clicks(ack, fail):
    ack()
    fail(error="something went wrong")

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

Returns

Callable fail() function

Expand source code
@property
def fail(self) -> Fail:
    """`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")
        def handle_button_clicks(ack, fail):
            ack()
            fail(error="something went wrong")

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

    Returns:
        Callable `fail()` function
    """
    if "fail" not in self:
        self["fail"] = Fail(
            client=self.client, function_execution_id=self.function_execution_id  # type: ignore[arg-type]
        )
    return self["fail"]
prop respond : Optional[Respond]

respond() function for this request.

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

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

Returns

Callable respond() function

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

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

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

    Returns:
        Callable `respond()` function
    """
    if "respond" not in self:
        self["respond"] = Respond(
            response_url=self.response_url,
            proxy=self.client.proxy,  # type: ignore[union-attr]
            ssl=self.client.ssl,  # type: ignore[union-attr]
        )
    return self["respond"]
prop saySay

say() function for this request.

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

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

Returns

Callable say() function

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

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

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

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

Methods

def to_copyable(self) ‑> BoltContext

Inherited members