Skip to content

Commit dd05b5d

Browse files
authored
Fix a bug where parsing assistant thread message event fails for beta feature enabled apps (#1184)
1 parent 5aabb9d commit dd05b5d

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

slack_bolt/context/assistant/assistant_utilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from slack_bolt.context.context import BoltContext
99
from slack_bolt.context.say import Say
10+
from .internals import has_channel_id_and_thread_ts
1011
from ..get_thread_context.get_thread_context import GetThreadContext
1112
from ..save_thread_context import SaveThreadContext
1213
from ..set_status import SetStatus
@@ -32,7 +33,7 @@ def __init__(
3233
self.client = context.client
3334
self.thread_context_store = thread_context_store or DefaultAssistantThreadContextStore(context)
3435

35-
if self.payload.get("assistant_thread") is not None:
36+
if has_channel_id_and_thread_ts(self.payload):
3637
# assistant_thread_started
3738
thread = self.payload["assistant_thread"]
3839
self.channel_id = thread["channel_id"]

slack_bolt/context/assistant/async_assistant_utilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from slack_bolt.context.async_context import AsyncBoltContext
1212
from slack_bolt.context.say.async_say import AsyncSay
13+
from .internals import has_channel_id_and_thread_ts
1314
from ..get_thread_context.async_get_thread_context import AsyncGetThreadContext
1415
from ..save_thread_context.async_save_thread_context import AsyncSaveThreadContext
1516
from ..set_status.async_set_status import AsyncSetStatus
@@ -35,7 +36,7 @@ def __init__(
3536
self.client = context.client
3637
self.thread_context_store = thread_context_store or DefaultAsyncAssistantThreadContextStore(context)
3738

38-
if self.payload.get("assistant_thread") is not None:
39+
if has_channel_id_and_thread_ts(self.payload):
3940
# assistant_thread_started
4041
thread = self.payload["assistant_thread"]
4142
self.channel_id = thread["channel_id"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def has_channel_id_and_thread_ts(payload: dict) -> bool:
2+
"""Verifies if the given payload has both channel_id and thread_ts under assistant_thread property.
3+
This data pattern is available for assistant_* events.
4+
"""
5+
return (
6+
payload.get("assistant_thread") is not None
7+
and payload["assistant_thread"].get("channel_id") is not None
8+
and payload["assistant_thread"].get("thread_ts") is not None
9+
)

slack_bolt/request/internals.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ def extract_thread_ts(payload: Dict[str, Any]) -> Optional[str]:
220220
# Thus, blindly setting this thread_ts to say utility can break existing apps' behaviors.
221221
if is_assistant_event(payload):
222222
event = payload["event"]
223-
if event.get("assistant_thread") is not None:
223+
if (
224+
event.get("assistant_thread") is not None
225+
and event["assistant_thread"].get("channel_id") is not None
226+
and event["assistant_thread"].get("thread_ts") is not None
227+
):
224228
# assistant_thread_started, assistant_thread_context_changed
229+
# "assistant_thread" property can exist for message event without channel_id and thread_ts
230+
# Thus, the above if check verifies these properties exist
225231
return event["assistant_thread"]["thread_ts"]
226232
elif event.get("channel") is not None:
227233
if event.get("thread_ts") is not None:

tests/scenario_tests/test_events_assistant.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def handle_user_message(say: Say, set_status: SetStatus, context: BoltContext):
8282
assert response.status == 200
8383
assert_target_called()
8484

85+
request = BoltRequest(body=user_message_event_body_with_assistant_thread, mode="socket_mode")
86+
response = app.dispatch(request)
87+
assert response.status == 200
88+
assert_target_called()
89+
8590
request = BoltRequest(body=message_changed_event_body, mode="socket_mode")
8691
response = app.dispatch(request)
8792
assert response.status == 200
@@ -163,6 +168,25 @@ def build_payload(event: dict) -> dict:
163168
)
164169

165170

171+
user_message_event_body_with_assistant_thread = build_payload(
172+
{
173+
"user": "W222",
174+
"type": "message",
175+
"ts": "1726133700.887259",
176+
"text": "When Slack was released?",
177+
"team": "T111",
178+
"user_team": "T111",
179+
"source_team": "T222",
180+
"user_profile": {},
181+
"thread_ts": "1726133698.626339",
182+
"parent_user_id": "W222",
183+
"channel": "D111",
184+
"event_ts": "1726133700.887259",
185+
"channel_type": "im",
186+
"assistant_thread": {"XXX": "YYY"},
187+
}
188+
)
189+
166190
message_changed_event_body = build_payload(
167191
{
168192
"type": "message",

tests/scenario_tests_async/test_events_assistant.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ async def handle_user_message(say: AsyncSay, set_status: AsyncSetStatus, context
9797
assert response.status == 200
9898
await assert_target_called()
9999

100+
request = AsyncBoltRequest(body=user_message_event_body_with_assistant_thread, mode="socket_mode")
101+
response = await app.async_dispatch(request)
102+
assert response.status == 200
103+
await assert_target_called()
104+
100105
request = AsyncBoltRequest(body=message_changed_event_body, mode="socket_mode")
101106
response = await app.async_dispatch(request)
102107
assert response.status == 200
@@ -178,6 +183,26 @@ def build_payload(event: dict) -> dict:
178183
)
179184

180185

186+
user_message_event_body_with_assistant_thread = build_payload(
187+
{
188+
"user": "W222",
189+
"type": "message",
190+
"ts": "1726133700.887259",
191+
"text": "When Slack was released?",
192+
"team": "T111",
193+
"user_team": "T111",
194+
"source_team": "T222",
195+
"user_profile": {},
196+
"thread_ts": "1726133698.626339",
197+
"parent_user_id": "W222",
198+
"channel": "D111",
199+
"event_ts": "1726133700.887259",
200+
"channel_type": "im",
201+
"assistant_thread": {"XXX": "YYY"},
202+
}
203+
)
204+
205+
181206
message_changed_event_body = build_payload(
182207
{
183208
"type": "message",

0 commit comments

Comments
 (0)