github slackapi/bolt-python v1.21.0
version 1.21.0

latest releases: v1.21.2, v1.21.1
one month ago

New Features

Agents & Assistants

A better Agents & Assistants support in Bolt is now available!

While you already can implement your agents using app.event(...) listeners for assistant_thread_started, assistant_thread_context_changed, and message events, Bolt offers a simpler approach. You just need to create an Assistant instance, attach the needed event handlers to it, and then add the assistant to your App instance.

assistant = Assistant()

# This listener is invoked when a human user opened an assistant thread
@assistant.thread_started
def start_assistant_thread(say: Say, set_suggested_prompts: SetSuggestedPrompts):
    # Send the first reply to the human who started chat with your app's assistant bot
    say(":wave: Hi, how can I help you today?")

    # Setting suggested prompts is optional
    set_suggested_prompts(
        prompts=[
            # If the suggested prompt is long, you can use {"title": "short one to display", "message": "full prompt"} instead
            "What does SLACK stand for?",
            "When Slack was released?",
        ],
    )

# This listener is invoked when the human user sends a reply in the assistant thread
@assistant.user_message
def respond_in_assistant_thread(
    payload: dict,
    logger: logging.Logger,
    context: BoltContext,
    set_status: SetStatus,
    say: Say,
    client: WebClient,
):
    try:
        # Tell the human user the assistant bot acknowledges the request and is working on it
        set_status("is typing...")

        # Collect the conversation history with this user
        replies_in_thread = client.conversations_replies(
            channel=context.channel_id,
            ts=context.thread_ts,
            oldest=context.thread_ts,
            limit=10,
        )
        messages_in_thread: List[Dict[str, str]] = []
        for message in replies_in_thread["messages"]:
            role = "user" if message.get("bot_id") is None else "assistant"
            messages_in_thread.append({"role": role, "content": message["text"]})

        # Pass the latest prompt and chat history to the LLM (call_llm is your own code)
        returned_message = call_llm(messages_in_thread)

        # Post the result in the assistant thread
        say(text=returned_message)

    except Exception as e:
        logger.exception(f"Failed to respond to an inquiry: {e}")
        # Don't forget sending a message telling the error
        # Without this, the status 'is typing...' won't be cleared, therefore the end-user is unable to continue the chat
        say(f":warning: Sorry, something went wrong during processing your request (error: {e})")

# Enable this assistant middleware in your Bolt app
app.use(assistant)

Please refer to https://tools.slack.dev/bolt-python/concepts/assistant/ and https://github.com/slack-samples/bolt-python-assistant-template for more details.

Changes

  • #1162 Add Agents & Assistants support - Thanks @seratch
  • #1142 Add listener_runner to context object to enable developers to leverage lazy listeners in middleware - Thanks @seratch
  • #1170 Fix double quoted img alt text in the default OAuth page rendering - @toofishes
  • #1173 Expose auto_acknowledge option for custom function handlers - Thanks @WilliamBergamin
  • #1143 Remove Optional typing of context.client - Thanks @WilliamBergamin
  • #1164 Simplify Python code snippets in authorization.md - Thanks @arkid15r

References

Don't miss a new bolt-python release

NewReleases is sending notifications on new releases.