Skip to content

Commit

Permalink
Fix #1414 botMessage handler in Assistant middleware does not work wh…
Browse files Browse the repository at this point in the history
…en other event listeners do not exist (#1415)
  • Loading branch information
seratch authored Jan 11, 2025
1 parent c662295 commit 48df343
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.ExecutorService;

import static com.slack.api.bolt.util.EventsApiPayloadParser.buildEventPayload;
import static com.slack.api.bolt.util.EventsApiPayloadParser.getEventTypeAndSubtype;

public class Assistant implements Middleware {

Expand All @@ -51,6 +52,17 @@ public class Assistant implements Middleware {
private AssistantEventHandler<MessageFileShareEvent> userMessageWithFiles;
private AssistantEventHandler<MessageEvent> botMessage;

static {
// *** Building event type cache data ***
// app.event / app.message handlers invoke getEventTypeAndSubtype method when their listeners are added.
// However, Assistant middleware does not, since it operates as a global middleware.
// This workaround ensures that the event type cache data is created before receiving requests.
getEventTypeAndSubtype(AssistantThreadStartedEvent.class);
getEventTypeAndSubtype(AssistantThreadContextChangedEvent.class);
getEventTypeAndSubtype(MessageEvent.class);
getEventTypeAndSubtype(MessageFileShareEvent.class);
}

public Assistant() {
this(null, buildDefaultExecutorService(), buildDefaultLogger());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ public EventRequest(
JsonObject context = thread.get("context").getAsJsonObject();
if (context != null) {
AssistantThreadContext threadContext = AssistantThreadContext.builder()
.enterpriseId(context.get("enterprise_id") != null ? context.get("enterprise_id").getAsString() : null)
.teamId(context.get("team_id") != null ? context.get("team_id").getAsString() : null)
.channelId(context.get("channel_id") != null ? context.get("channel_id").getAsString() : null)
// enterprise_id here can be a null value
// others cannot be null as of Jan 2025, but added the same logic to all for future safety
.enterpriseId(context.get("enterprise_id") != null && !context.get("enterprise_id").isJsonNull() ? context.get("enterprise_id").getAsString() : null)
.teamId(context.get("team_id") != null && !context.get("team_id").isJsonNull() ? context.get("team_id").getAsString() : null)
.channelId(context.get("channel_id") != null && !context.get("channel_id").isJsonNull() ? context.get("channel_id").getAsString() : null)
.threadEntryPoint(context.get("thread_entry_point") != null && !context.get("thread_entry_point").isJsonNull() ? context.get("thread_entry_point").getAsString() : null)
.build();
this.getContext().setThreadContext(threadContext);
}
Expand All @@ -134,9 +137,12 @@ public EventRequest(
// message events (user replies)
this.getContext().setAssistantThreadEvent(true);
this.getContext().setChannelId(event.get("channel").getAsString());
if (event.get("thread_ts") != null) {
if (event.get("thread_ts") != null
&& !event.get("thread_ts").isJsonNull()) {
this.getContext().setThreadTs(event.get("thread_ts").getAsString());
} else if (event.get("message") != null && event.get("message").getAsJsonObject().get("thread_ts") != null) {
} else if (event.get("message") != null
&& event.get("message").getAsJsonObject().get("thread_ts") != null
&& !event.get("message").getAsJsonObject().get("thread_ts").isJsonNull()) {
// message_changed
this.getContext().setThreadTs(event.get("message").getAsJsonObject().get("thread_ts").getAsString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class AssistantThreadContext {
private String enterpriseId;
private String teamId;
private String channelId;
private String threadEntryPoint; // "sunroof" etc.

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("enterpriseId", this.enterpriseId);
map.put("teamId", this.teamId);
map.put("channelId", this.channelId);
map.put("thread_entry_point", this.threadEntryPoint);
return map;
}
}

0 comments on commit 48df343

Please sign in to comment.