Skip to content

Commit

Permalink
Fix a bug where parsing assistant thread message event fails for beta…
Browse files Browse the repository at this point in the history
… feature enabled apps
  • Loading branch information
seratch committed Oct 22, 2024
1 parent de2b9e2 commit fe5443d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ public Response apply(Request req, Response resp, MiddlewareChain chain) throws
if (elements.length == 1) {
loadCurrentThreadContext(context);
getExecutorService().submit(() -> {
EventsApiPayload<MessageEvent> payload = buildEventPayload(request);
if (payload.getEvent().getBotId() != null) {
if (context.getBotId() != null) {
try {
EventsApiPayload<MessageEvent> payload = buildEventPayload(request);

Check warning on line 145 in bolt/src/main/java/com/slack/api/bolt/middleware/builtin/Assistant.java

View check run for this annotation

Codecov / codecov/patch

bolt/src/main/java/com/slack/api/bolt/middleware/builtin/Assistant.java#L145

Added line #L145 was not covered by tests
this.botMessage.apply(payload, context);
} catch (Exception e) {
getLogger().error("Failed to run botMessage handler: {e}", e);
}
} else {
try {
EventsApiPayload<MessageEvent> payload = buildEventPayload(request);
this.userMessage.apply(payload, context);
} catch (Exception e) {
getLogger().error("Failed to run userMessage handler: {e}", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public EventRequest(
this.getContext().setChannelId(event.get("channel_id").getAsString());
}
// assistant thread events
if (event.get("assistant_thread") != null) {
if (EventsApiPayloadParser.isAssistantThreadStartedOrContextChangedEvent(event)) {
this.getContext().setAssistantThreadEvent(true);
// assistant_thread_started, assistant_thread_context_changed events
JsonObject thread = event.get("assistant_thread").getAsJsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public static final <E extends Event> Class<E> getEventClass(String eventType) {
return null;
}

public static boolean isAssistantThreadStartedOrContextChangedEvent(JsonObject event) {
return event.get("assistant_thread") != null
&& event.get("assistant_thread").getAsJsonObject().get("channel_id") != null
&& event.get("assistant_thread").getAsJsonObject().get("thread_ts") != null;
}

public static boolean isMessageEventInAssistantThread(JsonObject event) {
if (event.get("channel_type") != null && event.get("channel_type").getAsString().equals("im")) {
if (event.get("thread_ts") != null) return true;
Expand Down
22 changes: 22 additions & 0 deletions bolt/src/test/java/test_locally/app/EventAssistantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ public void test() throws Exception {
response = app.run(new EventRequest(userMessage, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());

String userMessageWithAssistantThread = buildPayload("{\n" +
" \"user\": \"W222\",\n" +
" \"type\": \"message\",\n" +
" \"ts\": \"1726133700.887259\",\n" +
" \"text\": \"When Slack was released?\",\n" +
" \"team\": \"T111\",\n" +
" \"user_team\": \"T111\",\n" +
" \"source_team\": \"T222\",\n" +
" \"user_profile\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"parent_user_id\": \"W222\",\n" +
" \"channel\": \"D111\",\n" +
" \"event_ts\": \"1726133700.887259\",\n" +
" \"assistant_thread\": {\"XXX\": \"YYY\"},\n" +
" \"channel_type\": \"im\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(userMessageWithAssistantThread, rawHeaders, timestamp);
response = app.run(new EventRequest(userMessageWithAssistantThread, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());

String assistantMessageChanged = buildPayload("{\n" +
" \"type\": \"message\",\n" +
" \"subtype\": \"message_changed\",\n" +
Expand Down

0 comments on commit fe5443d

Please sign in to comment.