Skip to content

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

Description

@mgallifrey-dropbox

Reproducible in:

slack bolt 1.24.0

Steps to reproduce:

importos, loggingfromslack_bolt.async_appimportAsyncApp, AsyncAssistantfromslack_bolt.authorizationimportAuthorizeResultfromslack_bolt.context.async_contextimportAsyncBoltContextfromslack_bolt.context.assistant.thread_context_store.default_async_storeimport (
DefaultAsyncAssistantThreadContextStore,
)
fromslack_sdk.web.async_clientimportAsyncWebClientlogging.basicConfig(level=logging.INFO)
log=logging.getLogger(__name__)
asyncdefauthorize(enterprise_id, team_id, user_id, client: AsyncWebClient) ->AuthorizeResult:
# typical custom authorize: resolve identity, then set Authorization resultauth=awaitclient.auth_test(token=os.environ["SLACK_BOT_TOKEN"])
returnAuthorizeResult.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_startedasyncdefstarted(
say, get_thread_context, context: AsyncBoltContext,
):
# This works (as long as you're coming from a DM or channel)tc=awaitget_thread_context()
awaitsay(
":wave: Hi! here's you're thread context: {tx}"
)
@assistant.user_messageasyncdefon_msg(
say, context: AsyncBoltContext, get_thread_context,
):
# Prove authorize() ranlog.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=awaitget_thread_context()
log.info(f"get_thread_context() -> {tc_via_helper!r}")
# 2) Fresh store built *now* (post-auth): returns contextstore=DefaultAsyncAssistantThreadContextStore(context)
tc_via_store=awaitstore.find(channel_id=context.channel_id, thread_ts=context.thread_ts)
log.info(f"DefaultAsyncAssistantThreadContextStore.find(...) -> {tc_via_store!r}")
awaitsay(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 DefaultAssistantThreadContextStoreis initialized with a context that does not have a bot_user_id in it. This happens because init_contextrunsbefore 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