-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from YAPP-Github/feature/MAFOO-42
feat: ์ ๊ท ์ ์ ๊ฐ์ ์ ์ฌ๋ ์๋ฆผ ๋ฉ์์ง ๋ณด๋ด๊ธฐ ๊ธฐ๋ฅ ์ถ๊ฐ
- Loading branch information
Showing
9 changed files
with
200 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
user-service/src/main/java/kr/mafoo/user/config/SlackApiConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package kr.mafoo.user.config; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.methods.MethodsClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SlackApiConfig { | ||
|
||
@Value("${slack.webhook.token}") | ||
private String token; | ||
|
||
@Bean | ||
public MethodsClient getClient() { | ||
Slack slackClient = Slack.getInstance(); | ||
return slackClient.methods(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
user-service/src/main/java/kr/mafoo/user/service/SlackService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package kr.mafoo.user.service; | ||
|
||
import com.slack.api.methods.MethodsClient; | ||
import com.slack.api.methods.SlackApiException; | ||
import com.slack.api.methods.request.chat.ChatPostMessageRequest; | ||
import com.slack.api.model.block.Blocks; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.MarkdownTextObject; | ||
import com.slack.api.model.block.composition.TextObject; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.slack.api.model.block.Blocks.*; | ||
import static com.slack.api.model.block.composition.BlockCompositions.markdownText; | ||
import static com.slack.api.model.block.composition.BlockCompositions.plainText; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SlackService { | ||
|
||
@Value(value = "${slack.webhook.channel.error}") | ||
private String errorChannel; | ||
|
||
@Value(value = "${slack.webhook.channel.member}") | ||
private String memberChannel; | ||
|
||
private final MethodsClient methodsClient; | ||
|
||
public void sendErrorNotification(Throwable throwable, String method, String uri, String statusCode, long executionTime, String userAgent) { | ||
try { | ||
List<TextObject> textObjects = new ArrayList<>(); | ||
|
||
textObjects.add(markdownText(">*์์ํ์ง ๋ชปํ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค!*\n")); | ||
textObjects.add(markdownText("\n")); | ||
|
||
textObjects.add(markdownText("*๋ฉ์๋:* \n`" + method + "`\n")); | ||
textObjects.add(markdownText("*URI:* \n`" + uri + "`\n")); | ||
textObjects.add(markdownText("*์ํ์ฝ๋:* \n`" + statusCode + "`\n")); | ||
textObjects.add(markdownText("*๋ฉ์ธ์ง:* \n`" + throwable.getMessage() + "`\n")); | ||
textObjects.add(markdownText("*์์์๊ฐ:* \n`" + executionTime + " ms`\n")); | ||
textObjects.add(markdownText("*์ฌ์ฉ์:* \n`" + userAgent + "`\n")); | ||
|
||
ChatPostMessageRequest request = ChatPostMessageRequest | ||
.builder() | ||
.channel(errorChannel) | ||
.blocks( | ||
asBlocks( | ||
divider(), | ||
section( | ||
section -> section.fields(textObjects) | ||
) | ||
)) | ||
.build(); | ||
|
||
methodsClient.chatPostMessage(request); | ||
} catch (SlackApiException | IOException e) { | ||
throw new RuntimeException("Can't send Slack Message.", e); | ||
} | ||
} | ||
|
||
public Mono<Void> sendNewMemberNotification(String memberId, String memberName, String memberProfileImageUrl, String memberCreatedAt, String userAgent) { | ||
return Mono.fromCallable(() -> { | ||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
|
||
layoutBlocks.add( | ||
Blocks.header( | ||
headerBlockBuilder -> | ||
headerBlockBuilder.text(plainText("๐ ์ ๊ท ์ฌ์ฉ์ ๊ฐ์ ")))); | ||
layoutBlocks.add(divider()); | ||
|
||
MarkdownTextObject userIdMarkdown = | ||
MarkdownTextObject.builder().text("`์ฌ์ฉ์ ID`\n" + memberId).build(); | ||
|
||
MarkdownTextObject userNameMarkdown = | ||
MarkdownTextObject.builder().text("`์ฌ์ฉ์ ๋๋ค์`\n" + memberName).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(userIdMarkdown, userNameMarkdown)))); | ||
|
||
MarkdownTextObject userProfileImageMarkdown = | ||
MarkdownTextObject.builder().text("`ํ๋กํ ์ด๋ฏธ์ง`\n" + memberProfileImageUrl).build(); | ||
|
||
MarkdownTextObject userCreatedAtMarkdown = | ||
MarkdownTextObject.builder().text("`๊ฐ์ ์ผ์`\n" + memberCreatedAt).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(userProfileImageMarkdown, userCreatedAtMarkdown)))); | ||
|
||
MarkdownTextObject userUserAgentMarkdown = | ||
MarkdownTextObject.builder().text("`๊ฐ์ ํ๊ฒฝ`\n" + userAgent).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(userUserAgentMarkdown)))); | ||
|
||
ChatPostMessageRequest chatPostMessageRequest = | ||
ChatPostMessageRequest | ||
.builder() | ||
.text("์ ๊ท ์ฌ์ฉ์ ๊ฐ์ ์๋ฆผ") | ||
.channel(memberChannel) | ||
.blocks(layoutBlocks) | ||
.build(); | ||
|
||
return methodsClient.chatPostMessage(chatPostMessageRequest); | ||
}) | ||
.then(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters