Skip to content

Commit f11dbfb

Browse files
fix(assistant): get_thread_context calls store.find() for user_message events (#1453)
Co-authored-by: William Bergamin <wbergamin@salesforce.com>
1 parent 98a8f59 commit f11dbfb

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

slack_bolt/context/get_thread_context/async_get_thread_context.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ async def __call__(self) -> Optional[AssistantThreadContext]:
3131
if self.thread_context_loaded is True:
3232
return self._thread_context
3333

34-
if self.payload.get("assistant_thread") is not None:
34+
thread = self.payload.get("assistant_thread")
35+
if isinstance(thread, dict) and thread.get("context", {}).get("channel_id") is not None:
3536
# assistant_thread_started
36-
thread = self.payload["assistant_thread"]
37-
self._thread_context = (
38-
AssistantThreadContext(thread["context"])
39-
if thread.get("context", {}).get("channel_id") is not None
40-
else None
41-
)
37+
self._thread_context = AssistantThreadContext(thread["context"])
4238
# for this event, the context will never be changed
4339
self.thread_context_loaded = True
4440
elif self.payload.get("channel") is not None and self.payload.get("thread_ts") is not None:

slack_bolt/context/get_thread_context/get_thread_context.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ def __call__(self) -> Optional[AssistantThreadContext]:
3131
if self.thread_context_loaded is True:
3232
return self._thread_context
3333

34-
if self.payload.get("assistant_thread") is not None:
34+
thread = self.payload.get("assistant_thread")
35+
if isinstance(thread, dict) and thread.get("context", {}).get("channel_id") is not None:
3536
# assistant_thread_started
36-
thread = self.payload["assistant_thread"]
37-
self._thread_context = (
38-
AssistantThreadContext(thread["context"])
39-
if thread.get("context", {}).get("channel_id") is not None
40-
else None
41-
)
37+
self._thread_context = AssistantThreadContext(thread["context"])
4238
# for this event, the context will never be changed
4339
self.thread_context_loaded = True
4440
elif self.payload.get("channel") is not None and self.payload.get("thread_ts") is not None:

tests/scenario_tests/test_events_assistant.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def handle_bot_message():
133133

134134
app.assistant(assistant)
135135

136+
request = BoltRequest(body=user_message_event_body_with_action_token, mode="socket_mode")
137+
response = app.dispatch(request)
138+
assert response.status == 200
139+
assert listener_called.wait(timeout=0.1) is True
140+
listener_called.clear()
141+
136142
request = BoltRequest(body=message_changed_event_body, mode="socket_mode")
137143
response = app.dispatch(request)
138144
assert response.status == 200
@@ -332,6 +338,25 @@ def build_payload(event: dict) -> dict:
332338
}
333339
)
334340

341+
user_message_event_body_with_action_token = build_payload(
342+
{
343+
"user": "W222",
344+
"type": "message",
345+
"ts": "1726133700.887259",
346+
"text": "When Slack was released?",
347+
"team": "T111",
348+
"user_team": "T111",
349+
"source_team": "T222",
350+
"user_profile": {},
351+
"thread_ts": "1726133698.626339",
352+
"parent_user_id": "W222",
353+
"channel": "D111",
354+
"event_ts": "1726133700.887259",
355+
"channel_type": "im",
356+
"assistant_thread": {"action_token": "10647138185092.960436384805.afce3599"},
357+
}
358+
)
359+
335360
message_changed_event_body = build_payload(
336361
{
337362
"type": "message",

tests/scenario_tests_async/test_events_assistant.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ async def handle_bot_message():
157157

158158
app.assistant(assistant)
159159

160+
request = AsyncBoltRequest(body=user_message_event_body_with_action_token, mode="socket_mode")
161+
response = await app.async_dispatch(request)
162+
assert response.status == 200
163+
await asyncio.sleep(0.1)
164+
assert listener_called.is_set()
165+
listener_called.clear()
166+
160167
request = AsyncBoltRequest(body=message_changed_event_body, mode="socket_mode")
161168
response = await app.async_dispatch(request)
162169
assert response.status == 200
@@ -405,6 +412,25 @@ def build_payload(event: dict) -> dict:
405412
)
406413

407414

415+
user_message_event_body_with_action_token = build_payload(
416+
{
417+
"user": "W222",
418+
"type": "message",
419+
"ts": "1726133700.887259",
420+
"text": "When Slack was released?",
421+
"team": "T111",
422+
"user_team": "T111",
423+
"source_team": "T222",
424+
"user_profile": {},
425+
"thread_ts": "1726133698.626339",
426+
"parent_user_id": "W222",
427+
"channel": "D111",
428+
"event_ts": "1726133700.887259",
429+
"channel_type": "im",
430+
"assistant_thread": {"action_token": "10647138185092.960436384805.afce3599"},
431+
}
432+
)
433+
408434
message_changed_event_body = build_payload(
409435
{
410436
"type": "message",

0 commit comments

Comments
 (0)