Skip to content

Commit 86b5ecb

Browse files
committed
Merge branch 'issue-477'
2 parents a99f916 + 0b1e8e1 commit 86b5ecb

File tree

14 files changed

+241
-2
lines changed

14 files changed

+241
-2
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+
}

docs/guides/ja/modals.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,34 @@ ctx.ackWithPush(newViewInStack)
379379

380380
#### モーダル送信後にメッセージを投稿
381381

382-
`view_submission` のペイロードはデフォルトでは `response_url` を含んでいません。しかし、モーダルがユーザーにメッセージを投稿するためのチャンネルを入力するよう求める `input` タイプのブロックを含む場合、ペイロード内の `response_urls` として URL を受け取ることができます。
382+
`view_submission` のペイロードはデフォルトでは `response_url` を含んでいません。しかし、モーダルがユーザーにメッセージを投稿するためのチャンネルを入力するよう求める `input` タイプのブロックを含む場合、ペイロード内の `response_urls` (Java では `List<ResponseUrl> responseUrls` となります) として URL を受け取ることができます。
383383

384384
これを有効にするためには [`channels_select`](https://api.slack.com/reference/block-kit/block-elements#channel_select) もしくは [`conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_select) のタイプのブロックエレメントを配置し、さらにその属性として `"response_url_enabled": true` を追加してください。より詳細な情報は [API ドキュメント(英語)](https://api.slack.com/surfaces/modals/using#modal_response_url)を参照してください。
385385

386+
また、ユーザーがモーダルを開いたときに見ていたチャンネルや DM を `initial_conversation(s)` として自動的に反映したい場合は [`conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_select) / [`multi_conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_multi_select) エレメントの `default_to_current_conversation` を有効にしてください。
387+
388+
```java
389+
import static com.slack.api.model.block.Blocks.*;
390+
import static com.slack.api.model.block.composition.BlockCompositions.*;
391+
import static com.slack.api.model.block.element.BlockElements.*;
392+
import static com.slack.api.model.view.Views.*;
393+
394+
View modalView = view(v -> v
395+
.type("modal")
396+
.callbackId("request-modal")
397+
.submit(viewSubmit(vs -> vs.type("plain_text").text("Start")))
398+
.blocks(asBlocks(
399+
section(s -> s
400+
.text(plainText("The channel we'll post the result"))
401+
.accessory(conversationsSelect(conv -> conv
402+
.actionId("notification_conv_id")
403+
.responseUrlEnabled(true)
404+
.defaultToCurrentConversation(true)
405+
))
406+
)
407+
)));
408+
```
409+
386410
### `"view_closed"` リクエスト (`notify_on_close` が `true` のときのみ)
387411

388412
BoltSlack アプリに必要な共通処理の多くを巻き取ります。それを除いて、あなたのアプリがやらなければならない手順は以下の通りです。

docs/guides/modals.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,34 @@ ctx.ack { it.responseAction("push").view(newViewInStack) }
371371

372372
#### Publishing Messages After Modal Submissions
373373

374-
`view_submission` payloads don't have `response_url` by default. However, if you have an `input` block asking users a channel to post a message, payloads may provide `response_urls`.
374+
`view_submission` payloads don't have `response_url` by default. However, if you have an `input` block asking users a channel to post a message, payloads may provide `response_urls` (`List<ResponseUrl> responseUrls` in Java).
375375
376376
To enable this, set the block element type as either [`channels_select`](https://api.slack.com/reference/block-kit/block-elements#channel_select) or [`conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_select) and add `"response_url_enabled": true`. Refer to [the API document](https://api.slack.com/surfaces/modals/using#modal_response_url) for details.
377377
378+
Also, if you want to automatically set the channel a user is viewing when opening a modal to`initial_conversation(s)`, turn `default_to_current_conversation` on in [`conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_select) / [`multi_conversations_select`](https://api.slack.com/reference/block-kit/block-elements#conversation_multi_select) elements.
379+
380+
```java
381+
import static com.slack.api.model.block.Blocks.*;
382+
import static com.slack.api.model.block.composition.BlockCompositions.*;
383+
import static com.slack.api.model.block.element.BlockElements.*;
384+
import static com.slack.api.model.view.Views.*;
385+
386+
View modalView = view(v -> v
387+
.type("modal")
388+
.callbackId("request-modal")
389+
.submit(viewSubmit(vs -> vs.type("plain_text").text("Start")))
390+
.blocks(asBlocks(
391+
section(s -> s
392+
.text(plainText("The channel we'll post the result"))
393+
.accessory(conversationsSelect(conv -> conv
394+
.actionId("notification_conv_id")
395+
.responseUrlEnabled(true)
396+
.defaultToCurrentConversation(true)
397+
))
398+
)
399+
)));
400+
```
401+
378402
### `"view_closed"` requests (only when `notify_on_close` is `true`)
379403
380404
Bolt does many of the commonly required tasks for you. The steps you need to handle would be:

json-logs/samples/app-backend/interactive-components/BlockActionPayload.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
},
152152
"action_id": "",
153153
"initial_conversation": "",
154+
"default_to_current_conversation": false,
154155
"confirm": {
155156
"title": {
156157
"type": "plain_text",
@@ -191,6 +192,7 @@
191192
"emoji": false
192193
},
193194
"action_id": "",
195+
"default_to_current_conversation": false,
194196
"confirm": {
195197
"title": {
196198
"type": "plain_text",

json-logs/samples/app-backend/interactive-components/MessageShortcutPayload.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
},
128128
"action_id": "",
129129
"initial_conversation": "",
130+
"default_to_current_conversation": false,
130131
"confirm": {
131132
"title": {
132133
"type": "plain_text",
@@ -167,6 +168,7 @@
167168
"emoji": false
168169
},
169170
"action_id": "",
171+
"default_to_current_conversation": false,
170172
"confirm": {
171173
"title": {
172174
"type": "plain_text",

json-logs/samples/rtm/MessageEvent.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
},
102102
"action_id": "",
103103
"initial_conversation": "",
104+
"default_to_current_conversation": false,
104105
"confirm": {
105106
"title": {
106107
"type": "plain_text",

json-logs/samples/rtm/PinAddedEvent.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
},
110110
"action_id": "",
111111
"initial_conversation": "",
112+
"default_to_current_conversation": false,
112113
"confirm": {
113114
"title": {
114115
"type": "plain_text",

json-logs/samples/rtm/PinRemovedEvent.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
},
110110
"action_id": "",
111111
"initial_conversation": "",
112+
"default_to_current_conversation": false,
112113
"confirm": {
113114
"title": {
114115
"type": "plain_text",

json-logs/samples/rtm/StarAddedEvent.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
},
110110
"action_id": "",
111111
"initial_conversation": "",
112+
"default_to_current_conversation": false,
112113
"confirm": {
113114
"title": {
114115
"type": "plain_text",

0 commit comments

Comments
 (0)