|
1 | 1 | package samples; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
3 | 4 | import com.slack.api.bolt.App; |
4 | 5 | import com.slack.api.bolt.AppConfig; |
5 | 6 | import com.slack.api.bolt.socket_mode.SocketModeApp; |
6 | 7 | import com.slack.api.model.Message; |
7 | 8 | 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; |
| 9 | +import com.slack.api.model.event.*; |
12 | 10 | import com.slack.api.model.view.ViewState; |
| 11 | +import com.slack.api.util.json.GsonFactory; |
13 | 12 | import config.Constants; |
14 | 13 |
|
15 | 14 | import java.util.Arrays; |
@@ -153,6 +152,91 @@ public static void main(String[] args) throws Exception { |
153 | 152 | return ctx.ack(); |
154 | 153 | }); |
155 | 154 |
|
| 155 | + app.event(FunctionExecutedEvent.class, (req, ctx) -> { |
| 156 | + ctx.logger.info("req: {}", req); |
| 157 | + ctx.client().chatPostMessage(r -> r |
| 158 | + .channel(req.getEvent().getInputs().get("user_id").asString()) |
| 159 | + .text("hey!") |
| 160 | + .blocks(asBlocks(actions(a -> a.blockId("b").elements(asElements( |
| 161 | + button(b -> b.actionId("remote-function-button-success").value("clicked").text(plainText("block_actions success"))), |
| 162 | + button(b -> b.actionId("remote-function-button-error").value("clicked").text(plainText("block_actions error"))), |
| 163 | + button(b -> b.actionId("remote-function-modal").value("clicked").text(plainText("modal view"))) |
| 164 | + ))))) |
| 165 | + ); |
| 166 | + return ctx.ack(); |
| 167 | + }); |
| 168 | + |
| 169 | + app.blockAction("remote-function-button-success", (req, ctx) -> { |
| 170 | + Map<String, Object> outputs = new HashMap<>(); |
| 171 | + outputs.put("user_id", req.getPayload().getFunctionData().getInputs().get("user_id").asString()); |
| 172 | + ctx.client().functionsCompleteSuccess(r -> r |
| 173 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 174 | + .outputs(outputs) |
| 175 | + ); |
| 176 | + ctx.client().chatUpdate(r -> r |
| 177 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 178 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 179 | + .text("Thank you!") |
| 180 | + ); |
| 181 | + return ctx.ack(); |
| 182 | + }); |
| 183 | + app.blockAction("remote-function-button-error", (req, ctx) -> { |
| 184 | + ctx.client().functionsCompleteError(r -> r |
| 185 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 186 | + .error("test error!") |
| 187 | + ); |
| 188 | + ctx.client().chatUpdate(r -> r |
| 189 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 190 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 191 | + .text("Thank you!") |
| 192 | + ); |
| 193 | + return ctx.ack(); |
| 194 | + }); |
| 195 | + app.blockAction("remote-function-modal", (req, ctx) -> { |
| 196 | + ctx.client().viewsOpen(r -> r |
| 197 | + .triggerId(req.getPayload().getInteractivity().getInteractivityPointer()) |
| 198 | + .view(view(v -> v |
| 199 | + .type("modal") |
| 200 | + .callbackId("remote-function-view") |
| 201 | + .title(viewTitle(vt -> vt.type("plain_text").text("Remote Function test"))) |
| 202 | + .close(viewClose(vc -> vc.type("plain_text").text("Close"))) |
| 203 | + .submit(viewSubmit(vs -> vs.type("plain_text").text("Submit"))) |
| 204 | + .notifyOnClose(true) |
| 205 | + .blocks(asBlocks(input(input -> input |
| 206 | + .blockId("text-block") |
| 207 | + .element(plainTextInput(pti -> pti.actionId("text-action").multiline(true))) |
| 208 | + .label(plainText(pt -> pt.text("Text").emoji(true))) |
| 209 | + ))) |
| 210 | + ))); |
| 211 | + ctx.client().chatUpdate(r -> r |
| 212 | + .channel(req.getPayload().getContainer().getChannelId()) |
| 213 | + .ts(req.getPayload().getContainer().getMessageTs()) |
| 214 | + .text("Thank you!") |
| 215 | + ); |
| 216 | + return ctx.ack(); |
| 217 | + }); |
| 218 | + |
| 219 | + app.viewSubmission("remote-function-view", (req, ctx) -> { |
| 220 | + Map<String, Object> outputs = new HashMap<>(); |
| 221 | + outputs.put("user_id", ctx.getRequestUserId()); |
| 222 | + ctx.client().functionsCompleteSuccess(r -> r |
| 223 | + .token(req.getPayload().getBotAccessToken()) |
| 224 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 225 | + .outputs(outputs) |
| 226 | + ); |
| 227 | + return ctx.ack(); |
| 228 | + }); |
| 229 | + app.viewClosed("remote-function-view", (req, ctx) -> { |
| 230 | + Map<String, Object> outputs = new HashMap<>(); |
| 231 | + outputs.put("user_id", ctx.getRequestUserId()); |
| 232 | + ctx.client().functionsCompleteSuccess(r -> r |
| 233 | + .token(req.getPayload().getBotAccessToken()) |
| 234 | + .functionExecutionId(req.getPayload().getFunctionData().getExecutionId()) |
| 235 | + .outputs(outputs) |
| 236 | + ); |
| 237 | + return ctx.ack(); |
| 238 | + }); |
| 239 | + |
156 | 240 | String appToken = System.getenv(Constants.SLACK_SDK_TEST_SOCKET_MODE_APP_TOKEN); |
157 | 241 | SocketModeApp socketModeApp = new SocketModeApp(appToken, app); |
158 | 242 | socketModeApp.start(); |
|
0 commit comments