Skip to content

Commit

Permalink
Merge pull request #206 from Katchup-dev/feat#205/slack
Browse files Browse the repository at this point in the history
[FEAT] slack 알림 구현
  • Loading branch information
unanchoi authored Feb 13, 2024
2 parents 9ef4e6a + 3a6a9d7 commit 5137678
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .gitmessage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

################
# [FEAT] : 기능 추가 및 구현
# [FIX] : 에러 발생 시, 버그
# [REFACTOR] : 기존 코드에서 기능 추가 및 개선
# [HOTFIX] : 급한 수정 , 배포 후 수정
# [TEST] : Test code
# [RENAME] : 변수명, 메소드명, 클래스명 변경
# [CHORE] : 사소한 코드 수정, 디렉토리 구조 변경, 변수명 변경, 불필요한 코드 삭제(import)
# [DOCS] : 주석 및 Readme
# [CONFIG] : 의존성 추가, 설정 파일 추가
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

// slack
implementation("com.slack.api:slack-api-client:1.31.0")

// AWS sdk
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package site.katchup.katchupserver.common.advice;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -8,19 +9,41 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.katchup.katchupserver.common.dto.ApiResponseDto;
import site.katchup.katchupserver.common.exception.BaseException;
import site.katchup.katchupserver.common.exception.InternalServerException;
import site.katchup.katchupserver.common.response.ErrorCode;
import site.katchup.katchupserver.external.slack.SlackService;

import static site.katchup.katchupserver.common.response.ErrorCode.VALIDATION_REQUEST_MISSING_EXCEPTION;


@RestControllerAdvice
public class ControllerExceptionAdvice {

private final String channel;

public ControllerExceptionAdvice(@Value("${slack.channel}") final String channel, SlackService slackService) {
this.channel = channel;
this.slackService = slackService;
}

private final SlackService slackService;

@ExceptionHandler(BaseException.class)
public ResponseEntity<ApiResponseDto> handleGlobalException(BaseException ex) {
if (ex.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) {
slackService.sendMessage(channel, ex.getMessage());
}
return ResponseEntity.status(ex.getStatusCode())
.body(ApiResponseDto.error(ex.getCode()));
}

@ExceptionHandler(InternalServerException.class)
public ResponseEntity<ApiResponseDto> handleInternalServerException(InternalServerException ex) {
slackService.sendMessage(channel, ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ApiResponseDto.error(ErrorCode.INTERNAL_SERVER_ERROR.getCode()));
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ApiResponseDto> handleMissingParameter(MissingServletRequestParameterException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package site.katchup.katchupserver.external.slack;


import com.slack.api.Slack;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class SlackService {

@Value("${slack.bot-token}")
private String SLACK_TOKEN;

private final Environment env;

public void sendMessage(String channel, String text) {
try {
Slack slack = Slack.getInstance();
ChatPostMessageResponse response = slack.methods(SLACK_TOKEN).chatPostMessage(req -> req
.channel(channel)
.text("[" + getProfiles() + "]" + text));
System.out.println(response);
} catch (IOException | SlackApiException e) {
throw new RuntimeException(e);
}
}

private String getProfiles() {
return Arrays.stream(env.getActiveProfiles()).findFirst().orElse("");
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ springdoc:
path: /api-docs/json
groups:
enabled: true
slack:
bot-token: ${SLACK_BOT_TOKEN}
channel: ${SLACK_ALERT_CHANNEL}
6 changes: 5 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ aws-property:
aws-region: ${AWS_REGION}
s3-bucket-name: ${AWS_BUCKET_NAME}
access-key: ${AWS_ACCESS_KEY}
secret-key: ${AWS_SECRET_KEY}
secret-key: ${AWS_SECRET_KEY}

slack:
bot-token: ${SLACK_BOT_TOKEN}
channel: ${SLACK_ALERT_CHANNEL}

0 comments on commit 5137678

Please sign in to comment.