From e5ac17ea837fed21ce79dd7019cf736963575530 Mon Sep 17 00:00:00 2001 From: jjunsik Date: Mon, 29 Apr 2024 22:26:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[Feat]=20Slack=20Bot=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EC=8A=AC=EB=9E=99=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=95=B4=EB=8B=B9?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit core 내부에 util 패키지 존재하도록 패키지 경로 수정 --- build.gradle | 5 +++ .../java/pothole_solution/core/Pothole.java | 2 +- .../core/util/alarm/slack/SlackService.java | 37 +++++++++++++++++++ .../alarm/slack/constant/SlackConstant.java | 8 ++++ .../util/convertor/ProgressEnumConvertor.java | 2 +- .../controller/SampleManagerController.java | 14 ++++++- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/SlackService.java create mode 100644 pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/constant/SlackConstant.java rename pothole-core/src/main/java/{ => pothole_solution/core}/util/convertor/ProgressEnumConvertor.java (93%) diff --git a/build.gradle b/build.gradle index c1401a5..aee645e 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,11 @@ subprojects { runtimeOnly 'org.postgresql:postgresql' implementation 'org.hibernate.orm:hibernate-spatial' + + + implementation("com.slack.api:bolt:1.39.0") + implementation("com.slack.api:bolt-servlet:1.39.0") + implementation("com.slack.api:bolt-jetty:1.39.0") } tasks.named('test') { diff --git a/pothole-core/src/main/java/pothole_solution/core/Pothole.java b/pothole-core/src/main/java/pothole_solution/core/Pothole.java index b5aac45..cdf6dbb 100644 --- a/pothole-core/src/main/java/pothole_solution/core/Pothole.java +++ b/pothole-core/src/main/java/pothole_solution/core/Pothole.java @@ -5,7 +5,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.locationtech.jts.geom.Point; -import util.convertor.ProgressEnumConvertor; +import pothole_solution.core.util.convertor.ProgressEnumConvertor; @Entity @Getter diff --git a/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/SlackService.java b/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/SlackService.java new file mode 100644 index 0000000..cf6b711 --- /dev/null +++ b/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/SlackService.java @@ -0,0 +1,37 @@ +package pothole_solution.core.util.alarm.slack; + +import com.slack.api.Slack; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.SlackApiException; +import com.slack.api.methods.request.chat.ChatPostMessageRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import pothole_solution.core.util.alarm.slack.constant.SlackConstant; + +import java.io.IOException; + +@Slf4j +@Service +public class SlackService { + @Value(value = "${slack.token}") + String slackToken; + + public void sendSlackMessage(String message) { + try { + MethodsClient methodsClient = Slack.getInstance().methods(slackToken); + + ChatPostMessageRequest messageRequest = ChatPostMessageRequest.builder() + .channel(SlackConstant.POTHOLE_SERVER_DEPLOY) + .text(message) + .build(); + + methodsClient.chatPostMessage(messageRequest); + + log.info("Slack " + SlackConstant.POTHOLE_SERVER_DEPLOY + "에 메시지 보냄."); + + } catch (SlackApiException | IOException e) { + log.error("slack error: {}", e.getMessage(), e); + } + } +} diff --git a/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/constant/SlackConstant.java b/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/constant/SlackConstant.java new file mode 100644 index 0000000..dcb0ad4 --- /dev/null +++ b/pothole-core/src/main/java/pothole_solution/core/util/alarm/slack/constant/SlackConstant.java @@ -0,0 +1,8 @@ +package pothole_solution.core.util.alarm.slack.constant; + +// 추후 채널 추가 시 채널에 따라 다른 작업을 하기 위해 생성 +public class SlackConstant { + + // Slack에 생성된 채널명 + public static final String POTHOLE_SERVER_DEPLOY = "#포트홀"; +} diff --git a/pothole-core/src/main/java/util/convertor/ProgressEnumConvertor.java b/pothole-core/src/main/java/pothole_solution/core/util/convertor/ProgressEnumConvertor.java similarity index 93% rename from pothole-core/src/main/java/util/convertor/ProgressEnumConvertor.java rename to pothole-core/src/main/java/pothole_solution/core/util/convertor/ProgressEnumConvertor.java index f492b37..4e1ef34 100644 --- a/pothole-core/src/main/java/util/convertor/ProgressEnumConvertor.java +++ b/pothole-core/src/main/java/pothole_solution/core/util/convertor/ProgressEnumConvertor.java @@ -1,4 +1,4 @@ -package util.convertor; +package pothole_solution.core.util.convertor; import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; diff --git a/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java b/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java index 09baf31..2fc0d51 100644 --- a/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java +++ b/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java @@ -1,6 +1,7 @@ package pothole_solution.manager.sample.controller; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import org.springframework.http.ResponseEntity; @@ -9,17 +10,20 @@ import pothole_solution.core.Pothole; import pothole_solution.core.Progress; import pothole_solution.manager.sample.service.SampleService; +import pothole_solution.core.util.alarm.slack.SlackService; import java.util.List; -import java.util.stream.Collectors; @RequiredArgsConstructor @RestController @RequestMapping("/pothole/v1/manager") +@Slf4j public class SampleManagerController { private final SampleService sampleService; private final GeometryFactory geometryFactory = new GeometryFactory(); + private final SlackService slackService; + @PostMapping public ResponseEntity register(){ Pothole sample = Pothole.builder().location(geometryFactory.createPoint(new Coordinate(126.93427307071744, 37.38609685274056))).progress(Progress.READY).build(); @@ -37,11 +41,17 @@ public ResponseEntity getPothole(@PathVariable("id") Long id) { @GetMapping("/potholes") public ResponseEntity> getAllPotholes() { + log.info("Slack Message 테스트"); + + slackService.sendMessage("포트홀 전체 조회 시작"); + List potholes = sampleService.getAllPotholes(); + slackService.sendMessage("포트홀 전체 조회 끝"); + return ResponseEntity.ok().body(potholes.stream() .map(PointDto::new) - .collect(Collectors.toList())); + .toList()); } @PutMapping("/{id}") From 15560da57b8808a1e8fd152a59098f5a956ed561 Mon Sep 17 00:00:00 2001 From: jjunsik Date: Mon, 29 Apr 2024 22:30:52 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[Fix]=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/sample/controller/SampleManagerController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java b/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java index 2fc0d51..19a07d9 100644 --- a/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java +++ b/pothole-manager-api/src/main/java/pothole_solution/manager/sample/controller/SampleManagerController.java @@ -43,11 +43,11 @@ public ResponseEntity getPothole(@PathVariable("id") Long id) { public ResponseEntity> getAllPotholes() { log.info("Slack Message 테스트"); - slackService.sendMessage("포트홀 전체 조회 시작"); + slackService.sendSlackMessage("포트홀 전체 조회 시작"); List potholes = sampleService.getAllPotholes(); - slackService.sendMessage("포트홀 전체 조회 끝"); + slackService.sendSlackMessage("포트홀 전체 조회 끝"); return ResponseEntity.ok().body(potholes.stream() .map(PointDto::new)