|
| 1 | +package com.objectcomputing.checkins.notifications.social_media; |
| 2 | + |
| 3 | +import com.objectcomputing.checkins.configuration.CheckInsConfiguration; |
| 4 | + |
| 5 | +import com.slack.api.Slack; |
| 6 | +import com.slack.api.methods.MethodsClient; |
| 7 | +import com.slack.api.methods.request.chat.ChatPostMessageRequest; |
| 8 | +import com.slack.api.methods.response.chat.ChatPostMessageResponse; |
| 9 | +import com.slack.api.methods.request.chat.ChatDeleteRequest; |
| 10 | +import com.slack.api.methods.response.chat.ChatDeleteResponse; |
| 11 | +import com.slack.api.methods.request.conversations.ConversationsOpenRequest; |
| 12 | +import com.slack.api.methods.response.conversations.ConversationsOpenResponse; |
| 13 | + |
| 14 | +import jakarta.inject.Singleton; |
| 15 | +import jakarta.inject.Inject; |
| 16 | + |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +@Singleton |
| 23 | +public class SlackSender { |
| 24 | + private static final Logger LOG = LoggerFactory.getLogger(SlackSender.class); |
| 25 | + |
| 26 | + @Inject |
| 27 | + private CheckInsConfiguration configuration; |
| 28 | + |
| 29 | + public boolean send(List<String> userIds, String slackBlocks) { |
| 30 | + // See if we have a token. |
| 31 | + String token = configuration.getApplication() |
| 32 | + .getSlack().getBotToken(); |
| 33 | + if (token != null && !slackBlocks.isEmpty()) { |
| 34 | + MethodsClient client = Slack.getInstance().methods(token); |
| 35 | + |
| 36 | + try { |
| 37 | + ConversationsOpenResponse openResponse = |
| 38 | + client.conversationsOpen(ConversationsOpenRequest.builder() |
| 39 | + .users(userIds) |
| 40 | + .returnIm(true) |
| 41 | + .build()); |
| 42 | + if (!openResponse.isOk()) { |
| 43 | + LOG.error("Unable to open the conversation"); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return send(openResponse.getChannel().getId(), slackBlocks); |
| 48 | + } catch(Exception ex) { |
| 49 | + LOG.error("SlackSender.send: " + ex.toString()); |
| 50 | + return false; |
| 51 | + } |
| 52 | + } else { |
| 53 | + LOG.error("Missing token or missing slack blocks"); |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public boolean send(String channelId, String slackBlocks) { |
| 59 | + // See if we have a token. |
| 60 | + String token = configuration.getApplication() |
| 61 | + .getSlack().getBotToken(); |
| 62 | + if (token != null && !slackBlocks.isEmpty()) { |
| 63 | + MethodsClient client = Slack.getInstance().methods(token); |
| 64 | + |
| 65 | + try { |
| 66 | + ChatPostMessageRequest request = ChatPostMessageRequest |
| 67 | + .builder() |
| 68 | + .channel(channelId) |
| 69 | + .blocksAsString(slackBlocks) |
| 70 | + .build(); |
| 71 | + |
| 72 | + // Send it to Slack |
| 73 | + ChatPostMessageResponse response = client.chatPostMessage(request); |
| 74 | + |
| 75 | + if (!response.isOk()) { |
| 76 | + LOG.error("Unable to send the chat message: " + |
| 77 | + response.getError()); |
| 78 | + } |
| 79 | + |
| 80 | + return response.isOk(); |
| 81 | + } catch(Exception ex) { |
| 82 | + LOG.error("SlackSender.send: " + ex.toString()); |
| 83 | + return false; |
| 84 | + } |
| 85 | + } else { |
| 86 | + LOG.error("Missing token or missing slack blocks"); |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public boolean delete(String channel, String ts) { |
| 92 | + // See if we have a token. |
| 93 | + String token = configuration.getApplication() |
| 94 | + .getSlack().getBotToken(); |
| 95 | + if (token != null) { |
| 96 | + MethodsClient client = Slack.getInstance().methods(token); |
| 97 | + |
| 98 | + try { |
| 99 | + ChatDeleteRequest request = ChatDeleteRequest |
| 100 | + .builder() |
| 101 | + .channel(channel) |
| 102 | + .ts(ts) |
| 103 | + .build(); |
| 104 | + |
| 105 | + // Send it to Slack |
| 106 | + ChatDeleteResponse response = client.chatDelete(request); |
| 107 | + |
| 108 | + if (!response.isOk()) { |
| 109 | + LOG.error("Unable to delete the chat message: " + |
| 110 | + response.getError()); |
| 111 | + } |
| 112 | + |
| 113 | + return response.isOk(); |
| 114 | + } catch(Exception ex) { |
| 115 | + LOG.error("SlackSender.delete: " + ex.toString()); |
| 116 | + return false; |
| 117 | + } |
| 118 | + } else { |
| 119 | + LOG.error("Missing token or missing slack blocks"); |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
0 commit comments