|
5 | 5 | import com.slack.api.bolt.socket_mode.SocketModeApp; |
6 | 6 | import com.slack.api.model.Message; |
7 | 7 | import com.slack.api.model.block.element.RichTextSectionElement; |
8 | | -import com.slack.api.model.event.AppMentionEvent; |
9 | | -import com.slack.api.model.event.MessageChangedEvent; |
10 | | -import com.slack.api.model.event.MessageDeletedEvent; |
11 | | -import com.slack.api.model.event.MessageEvent; |
| 8 | +import com.slack.api.model.event.*; |
12 | 9 | import com.slack.api.model.view.ViewState; |
13 | 10 | import config.Constants; |
14 | 11 |
|
@@ -153,6 +150,111 @@ public static void main(String[] args) throws Exception { |
153 | 150 | return ctx.ack(); |
154 | 151 | }); |
155 | 152 |
|
| 153 | + // Note that this is still in beta as of Nov 2023 |
| 154 | + app.event(FunctionExecutedEvent.class, (req, ctx) -> { |
| 155 | + // TODO: future updates enable passing callback_id as below |
| 156 | + // app.function("hello", (req, ctx) -> { |
| 157 | + // app.function(Pattern.compile("^he.+$"), (req, ctx) -> { |
| 158 | + ctx.logger.info("req: {}", req); |
| 159 | + ctx.client().chatPostMessage(r -> r |
| 160 | + // TODO: remove this token passing by enhancing bolt internals |
| 161 | + .token(req.getEvent().getBotAccessToken()) |
| 162 | + .channel(req.getEvent().getInputs().get("user_id").asString()) |
| 163 | + .text("hey!") |
| 164 | + .blocks(asBlocks(actions(a -> a.blockId("b").elements(asElements( |
| 165 | + button(b -> b.actionId("remote-function-button-success").value("clicked").text(plainText("block_actions success"))), |
| 166 | + button(b -> b.actionId("remote-function-button-error").value("clicked").text(plainText("block_actions error"))), |
| 167 | + button(b -> b.actionId("remote-function-modal").value("clicked").text(plainText("modal view"))) |
| 168 | + ))))) |
| 169 | + ); |
| 170 | + return ctx.ack(); |
| 171 | + }); |
| 172 | + |
| 173 | + app.blockAction("remote-function-button-success", (req, ctx) -> { |
| 174 | + Map<String, Object> outputs = new HashMap<>(); |
| 175 | + outputs.put("user_id", req.getPayload().getFunctionData().getInputs().get("user_id").asString()); |
| 176 | + ctx.client().functionsCompleteSuccess(r -> r |
| 177 | + // TODO: remove this token passing by enhancing bolt internals |
| 178 | + .token(req.getPayload().getBotAccessToken()) |
| 179 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 180 | + .outputs(outputs) |
| 181 | + ); |
| 182 | + ctx.client().chatUpdate(r -> r |
| 183 | + // TODO: remove this token passing by enhancing bolt internals |
| 184 | + .token(req.getPayload().getBotAccessToken()) |
| 185 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 186 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 187 | + .text("Thank you!") |
| 188 | + ); |
| 189 | + return ctx.ack(); |
| 190 | + }); |
| 191 | + app.blockAction("remote-function-button-error", (req, ctx) -> { |
| 192 | + ctx.client().functionsCompleteError(r -> r |
| 193 | + // TODO: remove this token passing by enhancing bolt internals |
| 194 | + .token(req.getPayload().getBotAccessToken()) |
| 195 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 196 | + .error("test error!") |
| 197 | + ); |
| 198 | + ctx.client().chatUpdate(r -> r |
| 199 | + // TODO: remove this token passing by enhancing bolt internals |
| 200 | + .token(req.getPayload().getBotAccessToken()) |
| 201 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 202 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 203 | + .text("Thank you!") |
| 204 | + ); |
| 205 | + return ctx.ack(); |
| 206 | + }); |
| 207 | + app.blockAction("remote-function-modal", (req, ctx) -> { |
| 208 | + ctx.client().viewsOpen(r -> r |
| 209 | + // TODO: remove this token passing by enhancing bolt internals |
| 210 | + .token(req.getPayload().getBotAccessToken()) |
| 211 | + .triggerId(req.getPayload().getInteractivity().getInteractivityPointer()) |
| 212 | + .view(view(v -> v |
| 213 | + .type("modal") |
| 214 | + .callbackId("remote-function-view") |
| 215 | + .title(viewTitle(vt -> vt.type("plain_text").text("Remote Function test"))) |
| 216 | + .close(viewClose(vc -> vc.type("plain_text").text("Close"))) |
| 217 | + .submit(viewSubmit(vs -> vs.type("plain_text").text("Submit"))) |
| 218 | + .notifyOnClose(true) |
| 219 | + .blocks(asBlocks(input(input -> input |
| 220 | + .blockId("text-block") |
| 221 | + .element(plainTextInput(pti -> pti.actionId("text-action").multiline(true))) |
| 222 | + .label(plainText(pt -> pt.text("Text").emoji(true))) |
| 223 | + ))) |
| 224 | + ))); |
| 225 | + ctx.client().chatUpdate(r -> r |
| 226 | + // TODO: remove this token passing by enhancing bolt internals |
| 227 | + .token(req.getPayload().getBotAccessToken()) |
| 228 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 229 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 230 | + .text("Thank you!") |
| 231 | + ); |
| 232 | + return ctx.ack(); |
| 233 | + }); |
| 234 | + |
| 235 | + app.viewSubmission("remote-function-view", (req, ctx) -> { |
| 236 | + Map<String, Object> outputs = new HashMap<>(); |
| 237 | + outputs.put("user_id", ctx.getRequestUserId()); |
| 238 | + ctx.client().functionsCompleteSuccess(r -> r |
| 239 | + // TODO: remove this token passing by enhancing bolt internals |
| 240 | + .token(req.getPayload().getBotAccessToken()) |
| 241 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 242 | + .outputs(outputs) |
| 243 | + ); |
| 244 | + return ctx.ack(); |
| 245 | + }); |
| 246 | + app.viewClosed("remote-function-view", (req, ctx) -> { |
| 247 | + Map<String, Object> outputs = new HashMap<>(); |
| 248 | + outputs.put("user_id", ctx.getRequestUserId()); |
| 249 | + ctx.client().functionsCompleteSuccess(r -> r |
| 250 | + // TODO: remove this token passing by enhancing bolt internals |
| 251 | + .token(req.getPayload().getBotAccessToken()) |
| 252 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 253 | + .outputs(outputs) |
| 254 | + ); |
| 255 | + return ctx.ack(); |
| 256 | + }); |
| 257 | + |
156 | 258 | String appToken = System.getenv(Constants.SLACK_SDK_TEST_SOCKET_MODE_APP_TOKEN); |
157 | 259 | SocketModeApp socketModeApp = new SocketModeApp(appToken, app); |
158 | 260 | socketModeApp.start(); |
|
0 commit comments