|
| 1 | +package samples; |
| 2 | + |
| 3 | +import com.slack.api.app_backend.events.payload.EventsApiPayload; |
| 4 | +import com.slack.api.bolt.App; |
| 5 | +import com.slack.api.bolt.AppConfig; |
| 6 | +import com.slack.api.bolt.context.builtin.EventContext; |
| 7 | +import com.slack.api.methods.response.chat.ChatUnfurlResponse; |
| 8 | +import com.slack.api.model.event.LinkSharedEvent; |
| 9 | +import com.slack.api.model.event.MessageEvent; |
| 10 | +import util.ResourceLoader; |
| 11 | +import util.TestSlackAppServer; |
| 12 | + |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | + |
| 15 | +import static com.slack.api.model.block.Blocks.*; |
| 16 | +import static com.slack.api.model.block.composition.BlockCompositions.plainText; |
| 17 | +import static com.slack.api.model.block.element.BlockElements.asElements; |
| 18 | +import static com.slack.api.model.block.element.BlockElements.button; |
| 19 | + |
| 20 | +public class AuthenticatedUnfurlsSample { |
| 21 | + |
| 22 | + public static CompletableFuture<ChatUnfurlResponse> displayUserAuthRequired( |
| 23 | + EventsApiPayload<LinkSharedEvent> req, |
| 24 | + EventContext ctx |
| 25 | + ) { |
| 26 | + return ctx.asyncClient().chatUnfurl(r -> r |
| 27 | + .channel(ctx.getChannelId()) |
| 28 | + .ts(req.getEvent().getMessageTs()) |
| 29 | + .userAuthRequired(true) |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + public static CompletableFuture<ChatUnfurlResponse> displayUserAuthMessage( |
| 34 | + EventsApiPayload<LinkSharedEvent> req, |
| 35 | + EventContext ctx |
| 36 | + ) { |
| 37 | + return ctx.asyncClient().chatUnfurl(r -> r |
| 38 | + .channel(ctx.getChannelId()) |
| 39 | + .ts(req.getEvent().getMessageTs()) |
| 40 | + .userAuthMessage("Loading the preview... <https://www.example.com/auth|Connect your account>") |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public static CompletableFuture<ChatUnfurlResponse> displayUserAuthUrl( |
| 45 | + EventsApiPayload<LinkSharedEvent> req, |
| 46 | + EventContext ctx |
| 47 | + ) { |
| 48 | + return ctx.asyncClient().chatUnfurl(r -> r |
| 49 | + .channel(ctx.getChannelId()) |
| 50 | + .ts(req.getEvent().getMessageTs()) |
| 51 | + .userAuthUrl("https://www.example.com/auth") |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public static CompletableFuture<ChatUnfurlResponse> displayUserAuthBlocks( |
| 56 | + EventsApiPayload<LinkSharedEvent> req, |
| 57 | + EventContext ctx |
| 58 | + ) { |
| 59 | + return ctx.asyncClient().chatUnfurl(r -> r |
| 60 | + .channel(ctx.getChannelId()) |
| 61 | + .ts(req.getEvent().getMessageTs()) |
| 62 | + .userAuthBlocks(asBlocks( |
| 63 | + section(s -> s |
| 64 | + .blockId("intro-section") |
| 65 | + .text(plainText("Loading the preview... :eyes:")) |
| 66 | + ), |
| 67 | + actions(a -> a.elements(asElements( |
| 68 | + button(b -> b |
| 69 | + .actionId("button-click") |
| 70 | + .url("https://www.example.com/auth") |
| 71 | + .text(plainText("Connect your account")) |
| 72 | + ) |
| 73 | + ))) |
| 74 | + )) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String[] args) throws Exception { |
| 79 | + AppConfig config = ResourceLoader.loadAppConfig(); |
| 80 | + App app = new App(config); |
| 81 | + app.event(MessageEvent.class, (req, ctx) -> ctx.ack()); |
| 82 | + app.event(LinkSharedEvent.class, (req, ctx) -> { |
| 83 | + displayUserAuthRequired(req, ctx); |
| 84 | + displayUserAuthMessage(req, ctx); |
| 85 | + displayUserAuthBlocks(req, ctx); |
| 86 | + displayUserAuthUrl(req, ctx); |
| 87 | + return ctx.ack(); |
| 88 | + }); |
| 89 | + app.blockAction("button-click", (req, ctx) -> ctx.ack()); |
| 90 | + |
| 91 | + TestSlackAppServer server = new TestSlackAppServer(app); |
| 92 | + server.start(); |
| 93 | + } |
| 94 | +} |
0 commit comments