|
| 1 | +package samples; |
| 2 | + |
| 3 | +import com.slack.api.bolt.App; |
| 4 | +import com.slack.api.bolt.AppConfig; |
| 5 | +import com.slack.api.bolt.middleware.builtin.Assistant; |
| 6 | +import com.slack.api.bolt.socket_mode.SocketModeApp; |
| 7 | +import com.slack.api.model.Message; |
| 8 | +import com.slack.api.model.event.AppMentionEvent; |
| 9 | +import com.slack.api.model.event.MessageEvent; |
| 10 | + |
| 11 | +import java.security.SecureRandom; |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +import static com.slack.api.model.block.Blocks.actions; |
| 15 | +import static com.slack.api.model.block.Blocks.section; |
| 16 | +import static com.slack.api.model.block.composition.BlockCompositions.plainText; |
| 17 | +import static com.slack.api.model.block.element.BlockElements.button; |
| 18 | + |
| 19 | +public class AssistantInteractionApp { |
| 20 | + |
| 21 | + public static void main(String[] args) throws Exception { |
| 22 | + String botToken = System.getenv("SLACK_BOT_TOKEN"); |
| 23 | + String appToken = System.getenv("SLACK_APP_TOKEN"); |
| 24 | + |
| 25 | + App app = new App(AppConfig.builder() |
| 26 | + .singleTeamBotToken(botToken) |
| 27 | + .ignoringSelfAssistantMessageEventsEnabled(false) |
| 28 | + .build() |
| 29 | + ); |
| 30 | + |
| 31 | + Assistant assistant = new Assistant(app.executorService()); |
| 32 | + |
| 33 | + assistant.threadStarted((req, ctx) -> { |
| 34 | + try { |
| 35 | + ctx.say(r -> r |
| 36 | + .text("Hi, how can I help you today?") |
| 37 | + .blocks(Arrays.asList( |
| 38 | + section(s -> s.text(plainText("Hi, how I can I help you today?"))), |
| 39 | + actions(a -> a.elements(Collections.singletonList( |
| 40 | + button(b -> b.actionId("assistant-generate-numbers").text(plainText("Generate numbers"))) |
| 41 | + ))) |
| 42 | + )) |
| 43 | + ); |
| 44 | + } catch (Exception e) { |
| 45 | + ctx.logger.error("Failed to handle assistant thread started event: {e}", e); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + app.blockAction("assistant-generate-numbers", (req, ctx) -> { |
| 50 | + app.executorService().submit(() -> { |
| 51 | + Map<String, Object> eventPayload = new HashMap<>(); |
| 52 | + eventPayload.put("num", 20); |
| 53 | + try { |
| 54 | + ctx.client().chatPostMessage(r -> r |
| 55 | + .channel(req.getPayload().getChannel().getId()) |
| 56 | + .threadTs(req.getPayload().getMessage().getThreadTs()) |
| 57 | + .text("OK, I will generate numbers for you!") |
| 58 | + .metadata(new Message.Metadata("assistant-generate-numbers", eventPayload)) |
| 59 | + ); |
| 60 | + } catch (Exception e) { |
| 61 | + ctx.logger.error("Failed to post a bot message: {e}", e); |
| 62 | + } |
| 63 | + }); |
| 64 | + return ctx.ack(); |
| 65 | + }); |
| 66 | + |
| 67 | + assistant.botMessage((req, ctx) -> { |
| 68 | + if (req.getEvent().getMetadata() != null |
| 69 | + && req.getEvent().getMetadata().getEventType().equals("assistant-generate-numbers")) { |
| 70 | + try { |
| 71 | + ctx.setStatus("is typing..."); |
| 72 | + Double num = (Double) req.getEvent().getMetadata().getEventPayload().get("num"); |
| 73 | + Set<String> numbers = new HashSet<>(); |
| 74 | + SecureRandom random = new SecureRandom(); |
| 75 | + while (numbers.size() < num) { |
| 76 | + numbers.add(String.valueOf(random.nextInt(100))); |
| 77 | + } |
| 78 | + Thread.sleep(1000L); |
| 79 | + ctx.say(r -> r.text("Her you are: " + String.join(", ", numbers))); |
| 80 | + } catch (Exception e) { |
| 81 | + ctx.logger.error("Failed to handle assistant bot message event: {e}", e); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + assistant.userMessage((req, ctx) -> { |
| 87 | + try { |
| 88 | + ctx.setStatus("is typing..."); |
| 89 | + ctx.say(r -> r.text("Sorry, I couldn't understand your comment.")); |
| 90 | + } catch (Exception e) { |
| 91 | + ctx.logger.error("Failed to handle assistant user message event: {e}", e); |
| 92 | + try { |
| 93 | + ctx.say(r -> r.text(":warning: Sorry, something went wrong during processing your request!")); |
| 94 | + } catch (Exception ee) { |
| 95 | + ctx.logger.error("Failed to inform the error to the end-user: {ee}", ee); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + |
| 101 | + app.assistant(assistant); |
| 102 | + |
| 103 | + app.event(MessageEvent.class, (req, ctx) -> { |
| 104 | + return ctx.ack(); |
| 105 | + }); |
| 106 | + |
| 107 | + app.event(AppMentionEvent.class, (req, ctx) -> { |
| 108 | + ctx.say("You can help you at our 1:1 DM!"); |
| 109 | + return ctx.ack(); |
| 110 | + }); |
| 111 | + |
| 112 | + new SocketModeApp(appToken, app).start(); |
| 113 | + } |
| 114 | +} |
0 commit comments