Skip to content

Commit 382aaec

Browse files
authored
Resolve #1279 by adjusting the bolt internal logic (#1281)
1 parent 6986d8f commit 382aaec

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

bolt/src/main/java/com/slack/api/bolt/request/builtin/EventRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.slack.api.bolt.request.Request;
88
import com.slack.api.bolt.request.RequestHeaders;
99
import com.slack.api.bolt.request.RequestType;
10+
import com.slack.api.model.event.MessageEvent;
1011
import com.slack.api.util.json.GsonFactory;
1112
import lombok.ToString;
1213

@@ -133,7 +134,11 @@ public String getEventType() {
133134
}
134135

135136
public String getEventTypeAndSubtype() {
136-
if (eventSubtype == null) {
137+
// Since the data structure of the "message" type event significantly varies among subtypes,
138+
// This key string needs to consider the subtype for the event.
139+
// Other events with subtypes (e.g., "app_mention") do not have such differences,
140+
// so we can reuse the same data classes for them.
141+
if (!eventType.equals(MessageEvent.TYPE_NAME) || eventSubtype == null) {
137142
return eventType;
138143
} else {
139144
return eventType + ":" + eventSubtype;

bolt/src/test/java/test_locally/app/EventTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ public void appMention() throws Exception {
7474
assertTrue(userMessageReceived.get());
7575
}
7676

77+
String appMentionPayload2 = "{\"token\":\"legacy-fixed-value\",\"team_id\":\"T123\",\"api_app_id\":\"A123\",\"event\":{\"client_msg_id\":\"3fd13273-5a6a-4b5c-bd6f-109fd697038c\",\"type\":\"app_mention\",\"subtype\": \"bot_message\",\"text\":\"<@U123> test\",\"bot_id\":\"B123\",\"ts\":\"1583636399.000700\",\"team\":\"T123\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"FMAzp\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"user\",\"user_id\":\"U123\"},{\"type\":\"text\",\"text\":\" test\"}]}]}],\"channel\":\"C123\",\"event_ts\":\"1583636399.000700\"},\"type\":\"event_callback\",\"event_id\":\"EvV1KV8BM3\",\"event_time\":1583636399,\"authed_users\":[\"U123\"]}";
78+
79+
@Test
80+
public void appMention2() throws Exception {
81+
App app = buildApp();
82+
AtomicBoolean userMessageReceived = new AtomicBoolean(false);
83+
app.event(AppMentionEvent.class, (req, ctx) -> {
84+
userMessageReceived.set(req.getEvent().getBotId().equals("B123"));
85+
return ctx.ack();
86+
});
87+
88+
Map<String, List<String>> rawHeaders = new HashMap<>();
89+
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
90+
setRequestHeaders(appMentionPayload2, rawHeaders, timestamp);
91+
92+
EventRequest req = new EventRequest(appMentionPayload2, new RequestHeaders(rawHeaders));
93+
Response response = app.run(req);
94+
assertEquals(200L, response.getStatusCode().longValue());
95+
96+
assertTrue(userMessageReceived.get());
97+
}
98+
7799
String messagePayload = "{\"token\":\"legacy-fixed-value\",\"team_id\":\"T123\",\"api_app_id\":\"A123\",\"event\":{\"client_msg_id\":\"3fd13273-5a6a-4b5c-bd6f-109fd697038c\",\"type\":\"message\",\"text\":\"<@U123> test\",\"user\":\"U234\",\"ts\":\"1583636399.000700\",\"team\":\"T123\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"FMAzp\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"user\",\"user_id\":\"U123\"},{\"type\":\"text\",\"text\":\" test\"}]}]}],\"channel\":\"C123\",\"event_ts\":\"1583636399.000700\",\"channel_type\":\"channel\"},\"type\":\"event_callback\",\"event_id\":\"EvV1KA7U3A\",\"event_time\":1583636399,\"authed_users\":[\"U123\"]}";
78100

79101
@Test
@@ -425,4 +447,5 @@ public void retryHeaders() throws Exception {
425447
assertTrue(numReceived.get());
426448
assertTrue(reasonReceived.get());
427449
}
450+
428451
}

json-logs/samples/events/AppMentionPayload.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"client_msg_id": "",
2929
"user": "",
3030
"username": "",
31+
"app_id": "",
3132
"bot_id": "",
3233
"bot_profile": {
3334
"id": "",

slack-api-model/src/main/java/com/slack/api/model/event/AppMentionEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class AppMentionEvent implements Event {
3434
private String clientMsgId;
3535
private String user;
3636
private String username;
37+
private String appId;
3738
private String botId;
3839
private BotProfile botProfile;
3940
private String subtype;

slack-api-model/src/test/java/test_locally/api/model/event/AppMentionEventTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import test_locally.unit.GsonFactory;
77

88
import static org.hamcrest.CoreMatchers.is;
9+
import static org.hamcrest.CoreMatchers.nullValue;
910
import static org.hamcrest.MatcherAssert.assertThat;
1011

1112
public class AppMentionEventTest {
@@ -98,4 +99,43 @@ public void serialize() {
9899
assertThat(generatedJson, is(expectedJson));
99100
}
100101

102+
@Test
103+
public void botMessage() {
104+
String json = "{\n" +
105+
" \"type\": \"app_mention\",\n" +
106+
" \"subtype\": \"bot_message\",\n" +
107+
" \"text\": \"<@U111> hey\",\n" +
108+
" \"ts\": \"1707288078.718469\",\n" +
109+
" \"username\": \"Hey bot\",\n" +
110+
" \"bot_id\": \"B111\",\n" +
111+
" \"app_id\": \"A111\",\n" +
112+
" \"blocks\": [\n" +
113+
" {\n" +
114+
" \"type\": \"rich_text\",\n" +
115+
" \"block_id\": \"9fyvt\",\n" +
116+
" \"elements\": [\n" +
117+
" {\n" +
118+
" \"type\": \"rich_text_section\",\n" +
119+
" \"elements\": [\n" +
120+
" {\n" +
121+
" \"type\": \"user\",\n" +
122+
" \"user_id\": \"U111\"\n" +
123+
" },\n" +
124+
" {\n" +
125+
" \"type\": \"text\",\n" +
126+
" \"text\": \" hey\"\n" +
127+
" }\n" +
128+
" ]\n" +
129+
" }\n" +
130+
" ]\n" +
131+
" }\n" +
132+
" ],\n" +
133+
" \"channel\": \"C111\",\n" +
134+
" \"event_ts\": \"1707288078.718469\"\n" +
135+
"}";
136+
AppMentionEvent event = GsonFactory.createSnakeCase().fromJson(json, AppMentionEvent.class);
137+
assertThat(event.getType(), is("app_mention"));
138+
assertThat(event.getUser(), is(nullValue()));
139+
}
140+
101141
}

0 commit comments

Comments
 (0)