Skip to content

get_thread_context returns None for assistant.user_message handlers when using custom authorize function #1368

@mgallifrey-dropbox

Description

@mgallifrey-dropbox

Reproducible in:

slack bolt 1.24.0

Steps to reproduce:

import os, logging
from slack_bolt.async_app import AsyncApp, AsyncAssistant
from slack_bolt.authorization import AuthorizeResult
from slack_bolt.context.async_context import AsyncBoltContext
from slack_bolt.context.assistant.thread_context_store.default_async_store import (
    DefaultAsyncAssistantThreadContextStore,
)
from slack_sdk.web.async_client import AsyncWebClient

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

async def authorize(enterprise_id, team_id, user_id, client: AsyncWebClient) -> AuthorizeResult:
    # typical custom authorize: resolve identity, then set Authorization result
    auth = await client.auth_test(token=os.environ["SLACK_BOT_TOKEN"])
    return AuthorizeResult.from_auth_test_response(
        auth_test_response=auth, bot_token=os.environ["SLACK_BOT_TOKEN"]
    )

app = AsyncApp(authorize=authorize)
assistant = AsyncAssistant()
app.use(assistant)

@assistant.thread_started
async def started(
    say, get_thread_context, context: AsyncBoltContext,
):
     # This works (as long as you're coming from a DM or channel)
    tc = await get_thread_context()
    await say(
        ":wave: Hi! here's you're thread context: {tx}"
    )

@assistant.user_message
async def on_msg(
    say, context: AsyncBoltContext, get_thread_context,
):
    # Prove authorize() ran
    log.info(f"bot_user_id={context.bot_user_id}, channel={context.channel_id}, ts={context.thread_ts}")

    # 1) returns None (likely because context store was created pre-auth)
    tc_via_helper = await get_thread_context()
    log.info(f"get_thread_context() -> {tc_via_helper!r}")

    # 2) Fresh store built *now* (post-auth): returns context
    store = DefaultAsyncAssistantThreadContextStore(context)
    tc_via_store = await store.find(channel_id=context.channel_id, thread_ts=context.thread_ts)
    log.info(f"DefaultAsyncAssistantThreadContextStore.find(...) -> {tc_via_store!r}")

    await say(f"helper={tc_via_helper!r} | via_store={tc_via_store!r}")

Expected result:

get_thread_context should work in user_message threads regardless of authorization method used.

Actual result:

get_thread_context returns None in user messages if using a custom authorize

Analysis

This seems to happen because the DefaultAssistantThreadContextStore is initialized with a context that does not have a bot_user_id in it. This happens because init_context runs before middlewares (of which the custom authorize is one), runs.

#1346 seems like it may be related

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

(Tell what actually happened with logs, screenshots)

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions