Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Slack Bot 활용하여 슬랙 메시지 구현 #71

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pothole_solution.core.util.alarm.slack.constant;

// 추후 채널 추가 시 채널에 따라 다른 작업을 하기 위해 생성
public class SlackConstant {

// Slack에 생성된 채널명
public static final String POTHOLE_SERVER_DEPLOY = "#포트홀";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util.convertor;
package pothole_solution.core.util.convertor;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Long> register(){
Pothole sample = Pothole.builder().location(geometryFactory.createPoint(new Coordinate(126.93427307071744, 37.38609685274056))).progress(Progress.READY).build();
Expand All @@ -37,11 +41,17 @@ public ResponseEntity<PointDto> getPothole(@PathVariable("id") Long id) {

@GetMapping("/potholes")
public ResponseEntity<List<PointDto>> getAllPotholes() {
log.info("Slack Message 테스트");

slackService.sendSlackMessage("포트홀 전체 조회 시작");

List<Pothole> potholes = sampleService.getAllPotholes();

slackService.sendSlackMessage("포트홀 전체 조회 끝");

return ResponseEntity.ok().body(potholes.stream()
.map(PointDto::new)
.collect(Collectors.toList()));
.toList());
}

@PutMapping("/{id}")
Expand Down
Loading