forked from slackapi/java-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleApp.java
More file actions
262 lines (246 loc) · 14.7 KB
/
SimpleApp.java
File metadata and controls
262 lines (246 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package samples;
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.bolt.socket_mode.SocketModeApp;
import com.slack.api.model.Message;
import com.slack.api.model.block.element.RichTextSectionElement;
import com.slack.api.model.event.*;
import com.slack.api.model.view.ViewState;
import config.Constants;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static com.slack.api.model.block.Blocks.*;
import static com.slack.api.model.block.composition.BlockCompositions.dispatchActionConfig;
import static com.slack.api.model.block.composition.BlockCompositions.plainText;
import static com.slack.api.model.block.element.BlockElements.*;
import static com.slack.api.model.view.Views.*;
public class SimpleApp {
public static void main(String[] args) throws Exception {
App app = new App(AppConfig.builder()
.singleTeamBotToken(System.getenv(Constants.SLACK_SDK_TEST_SOCKET_MODE_BOT_TOKEN))
.build());
app.use((req, resp, chain) -> {
req.getContext().logger.info(req.getRequestBodyAsString());
return chain.next(req);
});
app.event(AppMentionEvent.class, (req, ctx) -> {
ctx.say("Hi there!");
return ctx.ack();
});
app.event(MessageEvent.class, (req, ctx) -> {
ctx.asyncClient().reactionsAdd(r -> r
.channel(req.getEvent().getChannel())
.name("eyes")
.timestamp(req.getEvent().getTs())
);
return ctx.ack();
});
app.event(MessageChangedEvent.class, (req, ctx) -> ctx.ack());
app.event(MessageDeletedEvent.class, (req, ctx) -> ctx.ack());
app.command("/hello-socket-mode", (req, ctx) -> {
Map<String, Object> eventPayload = new HashMap<>();
eventPayload.put("something", "great");
Message.Metadata metadata = Message.Metadata.builder()
.eventType("foo")
.eventPayload(eventPayload)
.build();
ctx.respond(r -> r.responseType("in_channel").text("hi!").metadata(metadata));
ctx.asyncClient().viewsOpen(r -> r
.triggerId(req.getContext().getTriggerId())
.view(view(v -> v
.type("modal")
.callbackId("test-view")
.title(viewTitle(vt -> vt.type("plain_text").text("Modal by Global Shortcut")))
.close(viewClose(vc -> vc.type("plain_text").text("Close")))
.submit(viewSubmit(vs -> vs.type("plain_text").text("Submit")))
.blocks(asBlocks(
input(input -> input
.blockId("agenda-block")
.element(plainTextInput(pti -> pti.actionId("agenda-action").multiline(true)))
.label(plainText(pt -> pt.text("Detailed Agenda").emoji(true)))
),
// Note that this block element requires files:read scope
input(input -> input
.blockId("files-block")
.element(fileInput(fi -> fi.actionId("files-action")))
.label(plainText(pt -> pt.text("Attached files").emoji(true)))
),
input(input -> input
.blockId("email-block")
.element(emailTextInput(pti -> pti.actionId("email-action")))
.label(plainText(pt -> pt.text("Email Address").emoji(true)))
),
input(input -> input
.blockId("url-block")
.element(urlTextInput(pti -> pti.actionId("url-action")))
.label(plainText(pt -> pt.text("URL").emoji(true)))
),
input(input -> input
.blockId("number-block")
.element(numberInput(pti -> pti.actionId("number-action")))
.label(plainText(pt -> pt.text("Budget").emoji(true)))
),
input(input -> input
.blockId("date-block")
.element(datePicker(pti -> pti.actionId("date-action")))
.label(plainText(pt -> pt.text("Date").emoji(true)))
),
input(input -> input
.blockId("time-block")
.element(timePicker(pti -> pti.actionId("time-action").timezone("America/Los_Angeles")))
.label(plainText(pt -> pt.text("Time").emoji(true)))
),
input(input -> input
.blockId("date-time-block")
.element(datetimePicker(pti -> pti.actionId("date-time-action")))
.label(plainText(pt -> pt.text("Date Time").emoji(true)))
),
input(input -> input
.blockId("rich-text-block")
.element(richTextInput(pti -> pti.actionId("rich-text-action")
.initialValue(richText(rt -> rt.blockId("b").elements(Arrays.asList(
richTextList(rtl -> rtl.style("bullet").elements(Arrays.asList(
richTextSection(rtl1 -> rtl1.elements(Arrays.asList(
RichTextSectionElement.Text.builder()
.text("Hey!")
.style(RichTextSectionElement.TextStyle.builder().bold(true).build())
.build()
))),
richTextSection(rtl2 -> rtl2.elements(Arrays.asList(
RichTextSectionElement.Text.builder().text("What's up?").build()
)))
)))
))))
.dispatchActionConfig(dispatchActionConfig(dc -> dc.triggerActionsOn(Arrays.asList("on_character_entered"))))
))
.dispatchAction(true)
.label(plainText(pt -> pt.text("Rich Text").emoji(true)))
)
))
)));
return ctx.ack();
});
app.blockAction("rich-text-action", (req, ctx) -> {
ctx.logger.info("state values: {}", req.getPayload().getView().getState().getValues());
ctx.logger.info("action[0]: {}", req.getPayload().getActions().get(0));
return ctx.ack();
});
app.viewSubmission("test-view", (req, ctx) -> {
ViewState.Value time = req.getPayload().getView().getState().getValues().get("time-block").get("time-action");
assert time.getTimezone().equals("America/Los_Angeles");
ViewState.Value richText = req.getPayload().getView().getState().getValues().get("rich-text-block").get("rich-text-action");
assert richText.getRichTextValue().getElements().size() > 0;
return ctx.ack();
});
app.messageShortcut("socket-mode-message-shortcut", (req, ctx) -> {
ctx.respond("It works!");
return ctx.ack();
});
// Note that this is still in beta as of Nov 2023
app.event(FunctionExecutedEvent.class, (req, ctx) -> {
// TODO: future updates enable passing callback_id as below
// app.function("hello", (req, ctx) -> {
// app.function(Pattern.compile("^he.+$"), (req, ctx) -> {
ctx.logger.info("req: {}", req);
ctx.client().chatPostMessage(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getEvent().getBotAccessToken())
.channel(req.getEvent().getInputs().get("user_id").asString())
.text("hey!")
.blocks(asBlocks(actions(a -> a.blockId("b").elements(asElements(
button(b -> b.actionId("remote-function-button-success").value("clicked").text(plainText("block_actions success"))),
button(b -> b.actionId("remote-function-button-error").value("clicked").text(plainText("block_actions error"))),
button(b -> b.actionId("remote-function-modal").value("clicked").text(plainText("modal view")))
)))))
);
return ctx.ack();
});
app.blockAction("remote-function-button-success", (req, ctx) -> {
Map<String, Object> outputs = new HashMap<>();
outputs.put("user_id", req.getPayload().getFunctionData().getInputs().get("user_id").asString());
ctx.client().functionsCompleteSuccess(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.functionExecutionId(req.getPayload().getFunctionData().getExecutionId())
.outputs(outputs)
);
ctx.client().chatUpdate(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.channel(req.getPayload().getContainer().getChannelId())
.ts(req.getPayload().getContainer().getMessageTs())
.text("Thank you!")
);
return ctx.ack();
});
app.blockAction("remote-function-button-error", (req, ctx) -> {
ctx.client().functionsCompleteError(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.functionExecutionId(req.getPayload().getFunctionData().getExecutionId())
.error("test error!")
);
ctx.client().chatUpdate(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.channel(req.getPayload().getContainer().getChannelId())
.ts(req.getPayload().getContainer().getMessageTs())
.text("Thank you!")
);
return ctx.ack();
});
app.blockAction("remote-function-modal", (req, ctx) -> {
ctx.client().viewsOpen(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.triggerId(req.getPayload().getInteractivity().getInteractivityPointer())
.view(view(v -> v
.type("modal")
.callbackId("remote-function-view")
.title(viewTitle(vt -> vt.type("plain_text").text("Remote Function test")))
.close(viewClose(vc -> vc.type("plain_text").text("Close")))
.submit(viewSubmit(vs -> vs.type("plain_text").text("Submit")))
.notifyOnClose(true)
.blocks(asBlocks(input(input -> input
.blockId("text-block")
.element(plainTextInput(pti -> pti.actionId("text-action").multiline(true)))
.label(plainText(pt -> pt.text("Text").emoji(true)))
)))
)));
ctx.client().chatUpdate(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.channel(req.getPayload().getContainer().getChannelId())
.ts(req.getPayload().getContainer().getMessageTs())
.text("Thank you!")
);
return ctx.ack();
});
app.viewSubmission("remote-function-view", (req, ctx) -> {
Map<String, Object> outputs = new HashMap<>();
outputs.put("user_id", ctx.getRequestUserId());
ctx.client().functionsCompleteSuccess(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.functionExecutionId(req.getPayload().getFunctionData().getExecutionId())
.outputs(outputs)
);
return ctx.ack();
});
app.viewClosed("remote-function-view", (req, ctx) -> {
Map<String, Object> outputs = new HashMap<>();
outputs.put("user_id", ctx.getRequestUserId());
ctx.client().functionsCompleteSuccess(r -> r
// TODO: remove this token passing by enhancing bolt internals
.token(req.getPayload().getBotAccessToken())
.functionExecutionId(req.getPayload().getFunctionData().getExecutionId())
.outputs(outputs)
);
return ctx.ack();
});
String appToken = System.getenv(Constants.SLACK_SDK_TEST_SOCKET_MODE_APP_TOKEN);
SocketModeApp socketModeApp = new SocketModeApp(appToken, app);
socketModeApp.start();
}
}