|
| 1 | +package wooteco.prolog.article.application; |
| 2 | + |
| 3 | +import com.slack.api.Slack; |
| 4 | +import com.slack.api.methods.SlackApiException; |
| 5 | +import com.slack.api.methods.request.chat.ChatPostMessageRequest; |
| 6 | +import com.slack.api.methods.response.chat.ChatPostMessageResponse; |
| 7 | +import com.slack.api.model.block.ActionsBlock; |
| 8 | +import com.slack.api.model.block.ContextBlock; |
| 9 | +import com.slack.api.model.block.DividerBlock; |
| 10 | +import com.slack.api.model.block.ImageBlock; |
| 11 | +import com.slack.api.model.block.LayoutBlock; |
| 12 | +import com.slack.api.model.block.SectionBlock; |
| 13 | +import com.slack.api.model.block.composition.MarkdownTextObject; |
| 14 | +import com.slack.api.model.block.composition.PlainTextObject; |
| 15 | +import com.slack.api.model.block.element.ButtonElement; |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | +import org.springframework.beans.factory.annotation.Value; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | +import wooteco.prolog.article.domain.Article; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +@RequiredArgsConstructor |
| 28 | +@Service |
| 29 | +@Transactional(readOnly = true) |
| 30 | +public class SlackService { |
| 31 | + |
| 32 | + @Value("${slack.article.token}") |
| 33 | + private String slackToken; |
| 34 | + |
| 35 | + @Value("${slack.article.channel}") |
| 36 | + private String slackChannel; |
| 37 | + |
| 38 | + public void sendSlackMessage(Article article) { |
| 39 | + Slack slack = Slack.getInstance(); |
| 40 | + |
| 41 | + // Image block |
| 42 | + ImageBlock imageBlock = ImageBlock.builder() |
| 43 | + .imageUrl(article.getImageUrl().getUrl()) |
| 44 | + .altText("article image") |
| 45 | + .build(); |
| 46 | + |
| 47 | + // Title section |
| 48 | + SectionBlock titleSection = SectionBlock.builder() |
| 49 | + .text(MarkdownTextObject.builder() |
| 50 | + .text("*" + article.getTitle().getTitle() + "*") |
| 51 | + .build()) |
| 52 | + .build(); |
| 53 | + |
| 54 | + // Context block |
| 55 | + String name = article.getMember().getNickname() + "(" + article.getMember().getUsername() + ")"; |
| 56 | + String date = article.getCreatedAt().toLocalDate().toString(); |
| 57 | + ContextBlock contextBlock = ContextBlock.builder() |
| 58 | + .elements(Collections.singletonList( |
| 59 | + MarkdownTextObject.builder() |
| 60 | + .text(name + " | " + date) |
| 61 | + .build())) |
| 62 | + .build(); |
| 63 | + |
| 64 | + // Summary section |
| 65 | + SectionBlock summarySection = SectionBlock.builder() |
| 66 | + .text(MarkdownTextObject.builder() |
| 67 | + .text(article.getDescription().getDescription()) |
| 68 | + .build()) |
| 69 | + .build(); |
| 70 | + |
| 71 | + // Button block |
| 72 | + ButtonElement buttonElement = ButtonElement.builder() |
| 73 | + .text(PlainTextObject.builder() |
| 74 | + .text("자세히 보기") |
| 75 | + .build()) |
| 76 | + .url(article.getUrl().getUrl()) |
| 77 | + .actionId("button_to_article") |
| 78 | + .build(); |
| 79 | + |
| 80 | + ActionsBlock actionsBlock = ActionsBlock.builder() |
| 81 | + .elements(Arrays.asList(buttonElement)) |
| 82 | + .build(); |
| 83 | + |
| 84 | + // Divider block |
| 85 | + DividerBlock dividerBlock = DividerBlock.builder().build(); |
| 86 | + |
| 87 | + // Prologue section |
| 88 | + SectionBlock prologueSection = SectionBlock.builder() |
| 89 | + .text(MarkdownTextObject.builder() |
| 90 | + .text("📝 프롤로그 프로필 페이지에서 `RSS Link` 를 설정해보세요.") |
| 91 | + .build()) |
| 92 | + .accessory(ButtonElement.builder() |
| 93 | + .text(PlainTextObject.builder() |
| 94 | + .text("프롤로그 둘러보기") |
| 95 | + .emoji(true) |
| 96 | + .build()) |
| 97 | + .value("click_to_prolog") |
| 98 | + .url("https://prolog.techcourse.co.kr/article") |
| 99 | + .actionId("button-action") |
| 100 | + .build()) |
| 101 | + .build(); |
| 102 | + |
| 103 | + // List of blocks |
| 104 | + List<LayoutBlock> blocks = Arrays.asList( |
| 105 | + imageBlock, |
| 106 | + titleSection, |
| 107 | + contextBlock, |
| 108 | + summarySection, |
| 109 | + actionsBlock, |
| 110 | + dividerBlock, |
| 111 | + prologueSection, |
| 112 | + dividerBlock |
| 113 | + ); |
| 114 | + |
| 115 | + // Create a message request |
| 116 | + ChatPostMessageRequest request = ChatPostMessageRequest.builder() |
| 117 | + .channel(slackChannel) |
| 118 | + .text("새로운 글이 등록되었습니다.") |
| 119 | + .blocks(blocks) |
| 120 | + .build(); |
| 121 | + |
| 122 | + try { |
| 123 | + ChatPostMessageResponse chatPostMessageResponse = slack.methods(slackToken).chatPostMessage(request); |
| 124 | + chatPostMessageResponse.getMessage(); |
| 125 | + chatPostMessageResponse.getChannel(); |
| 126 | + } catch (IOException | SlackApiException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + // TODO: slack 전송 실패 시 예외 처리 |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments