|
| 1 | +package samples; |
| 2 | + |
| 3 | +import com.slack.api.bolt.App; |
| 4 | +import com.slack.api.bolt.AppConfig; |
| 5 | +import com.slack.api.bolt.socket_mode.SocketModeApp; |
| 6 | +import com.slack.api.model.assistant.SuggestedPrompt; |
| 7 | +import com.slack.api.model.event.*; |
| 8 | + |
| 9 | +import java.util.Collections; |
| 10 | + |
| 11 | +public class AssistantEventListenerApp { |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + String botToken = System.getenv("SLACK_BOT_TOKEN"); |
| 15 | + String appToken = System.getenv("SLACK_APP_TOKEN"); |
| 16 | + |
| 17 | + App app = new App(AppConfig.builder().singleTeamBotToken(botToken).build()); |
| 18 | + |
| 19 | + app.event(AssistantThreadStartedEvent.class, (req, ctx) -> { |
| 20 | + String channelId = req.getEvent().getAssistantThread().getChannelId(); |
| 21 | + String threadTs = req.getEvent().getAssistantThread().getThreadTs(); |
| 22 | + app.executorService().submit(() -> { |
| 23 | + try { |
| 24 | + ctx.client().assistantThreadsSetTitle(r -> r |
| 25 | + .channelId(channelId) |
| 26 | + .threadTs(threadTs) |
| 27 | + .title("New chat") |
| 28 | + ); |
| 29 | + ctx.client().chatPostMessage(r -> r |
| 30 | + .channel(channelId) |
| 31 | + .threadTs(threadTs) |
| 32 | + .text("Hi, how can I help you today?") |
| 33 | + ); |
| 34 | + ctx.client().assistantThreadsSetSuggestedPrompts(r -> r |
| 35 | + .channelId(channelId) |
| 36 | + .threadTs(threadTs) |
| 37 | + .title("How are you?") |
| 38 | + .prompts(Collections.singletonList(new SuggestedPrompt("What does SLACK stand for?"))) |
| 39 | + ); |
| 40 | + } catch (Exception e) { |
| 41 | + ctx.logger.error("Failed to handle assistant thread started event: {e}", e); |
| 42 | + } |
| 43 | + }); |
| 44 | + return ctx.ack(); |
| 45 | + }); |
| 46 | + |
| 47 | + app.event(AssistantThreadContextChangedEvent.class, (req, ctx) -> { |
| 48 | + app.executorService().submit(() -> { |
| 49 | + String channelId = req.getEvent().getAssistantThread().getChannelId(); |
| 50 | + String threadTs = req.getEvent().getAssistantThread().getThreadTs(); |
| 51 | + // TODO: Store req.getEvent().getAssistantThread() for the following conversation |
| 52 | + }); |
| 53 | + return ctx.ack(); |
| 54 | + }); |
| 55 | + |
| 56 | + app.event(MessageEvent.class, (req, ctx) -> { |
| 57 | + if (req.getEvent().getChannelType().equals("im") && req.getEvent().getThreadTs() != null) { |
| 58 | + String channelId = req.getEvent().getChannel(); |
| 59 | + String threadTs = req.getEvent().getThreadTs(); |
| 60 | + app.executorService().submit(() -> { |
| 61 | + try { |
| 62 | + ctx.client().assistantThreadsSetStatus(r -> r |
| 63 | + .channelId(channelId) |
| 64 | + .threadTs(threadTs) |
| 65 | + .status("is typing...") |
| 66 | + ); |
| 67 | + Thread.sleep(500L); |
| 68 | + ctx.client().chatPostMessage(r -> r |
| 69 | + .channel(channelId) |
| 70 | + .threadTs(threadTs) |
| 71 | + .text("Here you are!") |
| 72 | + ); |
| 73 | + } catch (Exception e) { |
| 74 | + ctx.logger.error("Failed to handle assistant thread started event: {e}", e); |
| 75 | + try { |
| 76 | + ctx.client().chatPostMessage(r -> r |
| 77 | + .channel(channelId) |
| 78 | + .threadTs(threadTs) |
| 79 | + .text(":warning: Sorry, something went wrong during processing your request!") |
| 80 | + ); |
| 81 | + } catch (Exception ee) { |
| 82 | + ctx.logger.error("Failed to inform the error to the end-user: {ee}", ee); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + }); |
| 87 | + } |
| 88 | + return ctx.ack(); |
| 89 | + }); |
| 90 | + |
| 91 | + app.event(MessageFileShareEvent.class, (req, ctx) -> { |
| 92 | + if (req.getEvent().getChannelType().equals("im") && req.getEvent().getThreadTs() != null) { |
| 93 | + String channelId = req.getEvent().getChannel(); |
| 94 | + String threadTs = req.getEvent().getThreadTs(); |
| 95 | + app.executorService().submit(() -> { |
| 96 | + try { |
| 97 | + ctx.client().assistantThreadsSetStatus(r -> r |
| 98 | + .channelId(channelId) |
| 99 | + .threadTs(threadTs) |
| 100 | + .status("is analyzing the files...") |
| 101 | + ); |
| 102 | + Thread.sleep(500L); |
| 103 | + ctx.client().assistantThreadsSetStatus(r -> r |
| 104 | + .channelId(channelId) |
| 105 | + .threadTs(threadTs) |
| 106 | + .status("is still checking the files...") |
| 107 | + ); |
| 108 | + Thread.sleep(500L); |
| 109 | + ctx.client().chatPostMessage(r -> r |
| 110 | + .channel(channelId) |
| 111 | + .threadTs(threadTs) |
| 112 | + .text("Your files do not have any issues!") |
| 113 | + ); |
| 114 | + } catch (Exception e) { |
| 115 | + ctx.logger.error("Failed to handle assistant thread started event: {e}", e); |
| 116 | + try { |
| 117 | + ctx.client().chatPostMessage(r -> r |
| 118 | + .channel(channelId) |
| 119 | + .threadTs(threadTs) |
| 120 | + .text(":warning: Sorry, something went wrong during processing your request!") |
| 121 | + ); |
| 122 | + } catch (Exception ee) { |
| 123 | + ctx.logger.error("Failed to inform the error to the end-user: {ee}", ee); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + }); |
| 128 | + } |
| 129 | + return ctx.ack(); |
| 130 | + }); |
| 131 | + |
| 132 | + app.event(MessageChangedEvent.class, (req, ctx) -> { |
| 133 | + return ctx.ack(); |
| 134 | + }); |
| 135 | + app.event(MessageDeletedEvent.class, (payload, ctx) -> { |
| 136 | + return ctx.ack(); |
| 137 | + }); |
| 138 | + |
| 139 | + new SocketModeApp(appToken, app).start(); |
| 140 | + } |
| 141 | +} |
0 commit comments