Skip to content

Commit

Permalink
feat: 소모임 생성 시 slack Alarm 보내는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
minsu20 committed Nov 20, 2023
1 parent 4f74feb commit ae99b7b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.moing.backend.domain.teamMember.domain.service.TeamMemberSaveService;
import com.moing.backend.domain.teamScore.application.mapper.TeamScoreMapper;
import com.moing.backend.domain.teamScore.domain.service.TeamScoreSaveService;
import com.moing.backend.global.util.SlackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -26,6 +27,7 @@ public class CreateTeamUserCase {
private final TeamMapper teamMapper;
private final TeamScoreSaveService teamScoreSaveService;
private final TeamScoreMapper teamScoreMapper;
private final SlackService slackService;

public CreateTeamResponse createTeam(CreateTeamRequest createTeamRequest, String socialId){
Member member = memberGetService.getMemberBySocialId(socialId);
Expand All @@ -36,6 +38,7 @@ public CreateTeamResponse createTeam(CreateTeamRequest createTeamRequest, String
team.approveTeam();
//====지워야 함 (테스트 용)=====
teamScoreSaveService.save(teamScoreMapper.mapToTeamScore(team)); // 팀스코어 엔티티 생성
slackService.sendSlackTeamCreatedMessage(team.getName(), team.getLeaderId());
return new CreateTeamResponse(team.getTeamId());
}
}
44 changes: 36 additions & 8 deletions src/main/java/com/moing/backend/global/util/SlackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.slack.api.model.Field;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -22,24 +23,51 @@ public class SlackService {
@Value("${webhook.slack.error_url}")
private String errorWebhookUrl;

@Value("${webhook.slack.team_alarm_url}")
private String teamAlarmWebhookUrl;

private final Slack slackClient = Slack.getInstance();

// 슬랙 에러 알림 보내는 메서드
public void sendSlackAlertErrorLog(Exception e, HttpServletRequest request) {
// 공통 슬랙 메시지 전송 메서드
@Async
public void sendSlackMessage(String webhookUrl, String message, List<Attachment> attachments) {
try {
slackClient.send(errorWebhookUrl, payload(p -> p
.text("[500 에러가 발생했습니다.]")
.attachments(
List.of(generateSlackAttachment(e, request))
)
slackClient.send(webhookUrl, payload(p -> p
.text(message)
.attachments(attachments)
));
} catch (IOException slackError) {
log.debug("Slack 통신과의 예외 발생");
}
}

// 슬랙 에러 알림 메서드
public void sendSlackAlertErrorLog(Exception e, HttpServletRequest request) {
String message = "[500 에러가 발생했습니다.]";
List<Attachment> attachments = List.of(generateSlackErrorAttachment(e, request));
sendSlackMessage(errorWebhookUrl, message, attachments);
}

// 슬랙 소모임 생성 알림 메서드
public void sendSlackTeamCreatedMessage(String teamName, Long leaderId) {
String message = String.format("새로운 소모임 '%s'이(가) 생성되었습니다!", teamName);
List<Attachment> attachments = List.of(generateSlackTeamAttachment(teamName, leaderId));
sendSlackMessage(teamAlarmWebhookUrl, message, attachments);
}

private Attachment generateSlackTeamAttachment(String teamName, Long leaderId) {
return Attachment.builder()
.color("36a64f")
.title("소모임 생성 알림")
.fields(List.of(
generateSlackField("소모임 이름", teamName),
generateSlackField("생성자 아이디", String.valueOf(leaderId))
))
.build();
}

// attachment 생성 메서드
private Attachment generateSlackAttachment(Exception e, HttpServletRequest request) {
private Attachment generateSlackErrorAttachment(Exception e, HttpServletRequest request) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now());
String xffHeader = request.getHeader("X-FORWARDED-FOR");
return Attachment.builder()
Expand Down

0 comments on commit ae99b7b

Please sign in to comment.