|
| 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 | +} |
0 commit comments