Skip to content

Commit 2c8ddba

Browse files
committed
Fix #477 by adding default_to_current_conversation to conv elements
1 parent 310543c commit 2c8ddba

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package samples;
2+
3+
import com.slack.api.bolt.App;
4+
import com.slack.api.bolt.AppConfig;
5+
import com.slack.api.methods.response.views.ViewsOpenResponse;
6+
import com.slack.api.model.view.View;
7+
import util.ResourceLoader;
8+
import util.TestSlackAppServer;
9+
10+
import java.net.URLDecoder;
11+
12+
public class ViewDefaultToCurrentConversationSample {
13+
14+
public static void main(String[] args) throws Exception {
15+
AppConfig config = ResourceLoader.loadAppConfig("appConfig_defaultToCurrentConversation.json");
16+
App app = new App(config);
17+
app.use((req, resp, chain) -> {
18+
String body = req.getRequestBodyAsString();
19+
String payload = body.startsWith("payload=") ? URLDecoder.decode(body.split("payload=")[1], "UTF-8") : body;
20+
req.getContext().logger.info(payload);
21+
return chain.next(req);
22+
});
23+
24+
// src/test/resources
25+
app.command("/view", (req, ctx) -> {
26+
String view = ResourceLoader.load("views/view5.json");
27+
ViewsOpenResponse apiResponse = ctx.client().viewsOpen(r ->
28+
r.triggerId(ctx.getTriggerId()).viewAsString(view));
29+
if (apiResponse.isOk()) {
30+
return ctx.ack();
31+
} else {
32+
return ctx.ackWithJson(apiResponse);
33+
}
34+
});
35+
36+
app.viewSubmission("view-callback-id", (req, ctx) -> {
37+
View view = req.getPayload().getView();
38+
ctx.logger.info("state - {}, private_metadata - {}", view.getState(), view.getPrivateMetadata());
39+
String conversationId = view.getState().getValues().get("b1").get("a").getSelectedConversation();
40+
ctx.say(r -> r.channel(conversationId).text("Thank you for submitting the data!"));
41+
return ctx.ack(); // just close the view
42+
});
43+
44+
app.viewClosed("view-callback-id", (req, ctx) -> ctx.ack());
45+
46+
TestSlackAppServer server = new TestSlackAppServer(app);
47+
server.start();
48+
}
49+
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"callback_id": "view-callback-id",
3+
"type": "modal",
4+
"notify_on_close": true,
5+
"title": {
6+
"type": "plain_text",
7+
"text": "My App",
8+
"emoji": true
9+
},
10+
"submit": {
11+
"type": "plain_text",
12+
"text": "Submit",
13+
"emoji": true
14+
},
15+
"close": {
16+
"type": "plain_text",
17+
"text": "Cancel",
18+
"emoji": true
19+
},
20+
"blocks": [
21+
{
22+
"type": "input",
23+
"block_id": "b1",
24+
"element": {
25+
"type": "conversations_select",
26+
"action_id": "a",
27+
"default_to_current_conversation": true
28+
},
29+
"label": {
30+
"type": "plain_text",
31+
"text": "Single"
32+
}
33+
},
34+
{
35+
"type": "input",
36+
"block_id": "b2",
37+
"element": {
38+
"type": "multi_conversations_select",
39+
"action_id": "a",
40+
"default_to_current_conversation": true
41+
},
42+
"label": {
43+
"type": "plain_text",
44+
"text": "Multi"
45+
}
46+
}
47+
]
48+
}

slack-api-model/src/main/java/com/slack/api/model/block/element/ConversationsSelectElement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public class ConversationsSelectElement extends BlockElement {
3535
*/
3636
private String initialConversation;
3737

38+
/**
39+
* Pre-populates the select menu with the conversation that the user was viewing when they opened the modal,
40+
* if available. If initial_conversation is also supplied, it will be ignored. Default is false.
41+
*/
42+
private Boolean defaultToCurrentConversation;
43+
3844
/**
3945
* A confirm object that defines an optional confirmation dialog that appears after a menu item is selected.
4046
*/

slack-api-model/src/main/java/com/slack/api/model/block/element/MultiConversationsSelectElement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class MultiConversationsSelectElement extends BlockElement {
3737
*/
3838
private List<String> initialConversations;
3939

40+
/**
41+
* Pre-populates the select menu with the conversation that the user was viewing when they opened the modal,
42+
* if available. If initial_conversation is also supplied, it will be ignored. Default is false.
43+
*/
44+
private Boolean defaultToCurrentConversation;
45+
4046
/**
4147
* A confirm object that defines an optional confirmation dialog that appears
4248
* before the multi-select choices are submitted.

slack-api-model/src/test/java/test_locally/api/model/block/BlocksTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,54 @@ public void testResponseUrlEnabled_channels() {
322322
}
323323
}
324324

325+
@Test
326+
public void defaultToCurrentConversation() {
327+
String json = "{\n" +
328+
" \"blocks\": [\n" +
329+
" {\n" +
330+
" \"type\": \"section\",\n" +
331+
" \"text\": {\n" +
332+
" \"type\": \"mrkdwn\",\n" +
333+
" \"text\": \"Test block with multi conversations select\"\n" +
334+
" },\n" +
335+
" \"accessory\": {\n" +
336+
" \"type\": \"multi_conversations_select\",\n" +
337+
" \"placeholder\": {\n" +
338+
" \"type\": \"plain_text\",\n" +
339+
" \"text\": \"Select conversations\",\n" +
340+
" \"emoji\": true\n" +
341+
" },\n" +
342+
" \"default_to_current_conversation\": true\n" +
343+
" }\n" +
344+
" },\n" +
345+
" {\n" +
346+
" \"type\": \"section\",\n" +
347+
" \"text\": {\n" +
348+
" \"type\": \"mrkdwn\",\n" +
349+
" \"text\": \"Test block with multi conversations select\"\n" +
350+
" },\n" +
351+
" \"accessory\": {\n" +
352+
" \"type\": \"conversations_select\",\n" +
353+
" \"placeholder\": {\n" +
354+
" \"type\": \"plain_text\",\n" +
355+
" \"text\": \"Select conversations\",\n" +
356+
" \"emoji\": true\n" +
357+
" },\n" +
358+
" \"default_to_current_conversation\": true\n" +
359+
" }\n" +
360+
" }\n" +
361+
" ]\n" +
362+
"}";
363+
Message message = GsonFactory.createSnakeCase().fromJson(json, Message.class);
364+
assertNotNull(message);
365+
assertEquals(2, message.getBlocks().size());
366+
SectionBlock section1 = (SectionBlock) message.getBlocks().get(0);
367+
MultiConversationsSelectElement elem1 = (MultiConversationsSelectElement) (section1).getAccessory();
368+
assertTrue(elem1.getDefaultToCurrentConversation());
369+
SectionBlock section2 = (SectionBlock) message.getBlocks().get(1);
370+
ConversationsSelectElement elem2 = (ConversationsSelectElement) (section2).getAccessory();
371+
assertTrue(elem2.getDefaultToCurrentConversation());
372+
}
373+
374+
325375
}

0 commit comments

Comments
 (0)