-
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.
Browse files
Browse the repository at this point in the history
feat - #160 μ¬λ μλ μ°λ
- Loading branch information
Showing
11 changed files
with
188 additions
and
8 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
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,30 @@ | ||
package sopt.org.hmh.domain.slack; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.model.Attachment; | ||
import com.slack.api.webhook.WebhookPayloads; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SlackSender { | ||
|
||
private final Slack slackClient = Slack.getInstance(); | ||
|
||
public void sendSlackNotification(String webhookUrl, String title, Attachment attachment) { | ||
try { | ||
slackClient.send(webhookUrl, WebhookPayloads.payload(p -> p | ||
.text(title) | ||
.attachments(List.of(attachment)) | ||
)); | ||
} catch (IOException slackError) { | ||
log.debug("Slack ν΅μ μμΈ λ°μ: {}", slackError.getMessage()); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/sopt/org/hmh/domain/slack/builder/ErrorSlackMessageBuilder.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,45 @@ | ||
package sopt.org.hmh.domain.slack.builder; | ||
|
||
import com.slack.api.model.Attachment; | ||
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 ErrorSlackMessageBuilder implements SlackMessageBuilder { | ||
|
||
@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(), | ||
generateSlackAttachment(status, e, request)); | ||
} | ||
|
||
@Override | ||
public Attachment generateSlackAttachment(SlackStatus status, Object... params) { | ||
Exception e = (Exception) params[0]; | ||
HttpServletRequest request = (HttpServletRequest) params[1]; | ||
|
||
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now()); | ||
|
||
return Attachment.builder() | ||
.color(changeColorToHex(status.getColor())) | ||
.title(requestTime + " λ°μ μ€λ₯ λ‘κ·Έ") | ||
.fields(List.of( | ||
generateSlackField("User Info", "- ID : " + request.getRemoteUser()), | ||
generateSlackField("Request URL", "[" + request.getMethod() + "] " + request.getRequestURI()), | ||
generateSlackField("Error Info", e.getMessage()))) | ||
.build(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/sopt/org/hmh/domain/slack/builder/NewUserSlackMessageBuilder.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,43 @@ | ||
package sopt.org.hmh.domain.slack.builder; | ||
|
||
import com.slack.api.model.Attachment; | ||
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 NewUserSlackMessageBuilder implements SlackMessageBuilder{ | ||
|
||
@Value("${slack.webhook.newUserWebUrl}") | ||
private String newUserWebUrl; | ||
|
||
private final SlackSender slackSender; | ||
private final UserRepository userRepository; | ||
|
||
public void sendNotification(SlackStatus status, String userName, String os) { | ||
slackSender.sendSlackNotification( | ||
newUserWebUrl, | ||
status.getTitle(), | ||
generateSlackAttachment(status, userName, os)); | ||
} | ||
|
||
@Override | ||
public Attachment generateSlackAttachment(SlackStatus status, Object... params) { | ||
String userName = (String) params[0]; | ||
String os = (String) params[1]; | ||
|
||
return Attachment.builder() | ||
.color(changeColorToHex(status.getColor())) | ||
.title("μλ‘μ΄ μ μ '" + userName + "'λμ΄ κ°μ νμ΅λλ€!") | ||
.fields(List.of( | ||
generateSlackField("μ΄ μ μ μ", " π " + userRepository.count() + "λͺ "), | ||
generateSlackField("κ°μ ν OS", "π " + os))) | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/sopt/org/hmh/domain/slack/builder/SlackMessageBuilder.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,22 @@ | ||
package sopt.org.hmh.domain.slack.builder; | ||
|
||
import com.slack.api.model.Attachment; | ||
import com.slack.api.model.Field; | ||
import sopt.org.hmh.domain.slack.constant.SlackStatus; | ||
|
||
public interface SlackMessageBuilder { | ||
|
||
Attachment generateSlackAttachment(SlackStatus status, Object... params); | ||
|
||
default String changeColorToHex(java.awt.Color color) { | ||
return String.format("%06X", (0xFFFFFF & color.getRGB())); | ||
} | ||
|
||
default Field generateSlackField(String title, String value) { | ||
return Field.builder() | ||
.title(title) | ||
.value(value) | ||
.valueShortEnough(false) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/sopt/org/hmh/domain/slack/constant/SlackStatus.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,15 @@ | ||
package sopt.org.hmh.domain.slack.constant; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum SlackStatus { | ||
NEW_USER_SIGNUP(java.awt.Color.GREEN, "π μ κ· μ μ νμ κ°μ λ°μ"), | ||
INTERNAL_ERROR(java.awt.Color.ORANGE, "π¨ μλ² λ΄λΆ μλ¬ λ°μ"), | ||
CLIENT_ERROR(java.awt.Color.RED, "π μλ² μ€λ₯ λ°μ"); | ||
|
||
private final java.awt.Color color; | ||
private final String title; | ||
} |
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