-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat - #160 slack api 사용과 메세지 구성하는 메소드 분리
- Loading branch information
Showing
3 changed files
with
96 additions
and
72 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
49 changes: 49 additions & 0 deletions
49
src/main/java/sopt/org/hmh/domain/slack/provider/SlackErrorNotificationProvider.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,49 @@ | ||
package sopt.org.hmh.domain.slack.provider; | ||
|
||
import com.slack.api.model.Attachment; | ||
import com.slack.api.model.Field; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import sopt.org.hmh.domain.slack.SlackSender; | ||
import sopt.org.hmh.domain.slack.constant.SlackStatus; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SlackErrorNotificationProvider { | ||
|
||
@Value("${slack.webhook.serverErrorWebUrl}") | ||
private String serverErrorWebUrl; | ||
|
||
private final SlackSender slackSender; | ||
|
||
public void sendNotification(SlackStatus status, Exception e, HttpServletRequest request) { | ||
slackSender.sendSlackNotification(serverErrorWebUrl, status.getTitle(), | ||
generateErrorSlackAttachment(e, request, status.getColor())); | ||
} | ||
|
||
private Attachment generateErrorSlackAttachment(Exception e, HttpServletRequest request, String colorCode) { | ||
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now()); | ||
return Attachment.builder() | ||
.color(colorCode) | ||
.title(requestTime + " 발생 오류 로그") | ||
.fields(List.of( | ||
generateSlackField("User Info", "- ID : " + request.getRemoteUser()), | ||
generateSlackField("Request URL", "[" + request.getMethod()+"] " + request.getRequestURI()), | ||
generateSlackField("Error Info", e.getMessage()))) | ||
.build(); | ||
} | ||
|
||
private Field generateSlackField(String title, String value) { | ||
return Field.builder() | ||
.title(title) | ||
.value(value) | ||
.valueShortEnough(false) | ||
.build(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/sopt/org/hmh/domain/slack/provider/SlackNewUserNotificationProvider.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,46 @@ | ||
package sopt.org.hmh.domain.slack.provider; | ||
|
||
import com.slack.api.model.Attachment; | ||
import com.slack.api.model.Field; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import sopt.org.hmh.domain.slack.SlackSender; | ||
import sopt.org.hmh.domain.slack.constant.SlackStatus; | ||
import sopt.org.hmh.domain.user.repository.UserRepository; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SlackNewUserNotificationProvider { | ||
|
||
@Value("${slack.webhook.newUserWebUrl}") | ||
private String newUserWebUrl; | ||
|
||
private final SlackSender slackSender; | ||
private final UserRepository userRepository; | ||
|
||
public void sendNotification(String userName, String os, SlackStatus status) { | ||
slackSender.sendSlackNotification(newUserWebUrl, status.getTitle(), | ||
generateNewUserSlackAttachment(userName, os, status.getColor())); | ||
} | ||
|
||
public Attachment generateNewUserSlackAttachment(String userName, String os, String colorCode) { | ||
return Attachment.builder() | ||
.color(colorCode) | ||
.title("새로운 유저 '" + userName + "'님이 가입했습니다!") | ||
.fields(List.of( | ||
generateSlackField("총 유저 수", " 👉 " + userRepository.count() + "명"), | ||
generateSlackField("가입한 OS", "👉 " + os))) | ||
.build(); | ||
} | ||
|
||
private Field generateSlackField(String title, String value) { | ||
return Field.builder() | ||
.title(title) | ||
.value(value) | ||
.valueShortEnough(false) | ||
.build(); | ||
} | ||
} |