SpringBoot와 Slack webhook을 사용하여 메세지 보내기
2021. 8. 10. 00:03ㆍSpring
이 글에서는 slack webhook을 사용하여 springboot api를 호출해 slack으로 메세지를 전송하는 방법을 보여줍니다.
1. slack 앱에서 Incoming WebHooks 추가 및 설정
1. slack앱에서 Incoming WebHooks를 추가합니다.
2. slack에 추가를 클릭합니다.
3. 메세지를 보낼 기존에 있는 채널을 선택하거나 새로운 채널을 생성합니다.
4. Webhook URL을 복사합니다.
2. 메세지 전송을 위한 코드 작성
@PostMapping("/publish/slack")
public void send(@RequestBody String message) {
RestTemplate restTemplate = new RestTemplate();
Map<String, Object> request = new HashMap<>();
request.put("username", "slack메세지 명"); //slack bot name
request.put("text", message); //전송할 메세지
request.put("icon_emoji", ":slack_emoji:"); //slack bot image
HttpEntity<Map<String, Object>> entity = new HttpEntity<Map<String, Object>>(request);
String url = "https://hooks.slack.com/services/-------"; //복사한 Webhook URL 입력
restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
}
코드 작성 후 api를 호출합니다.
3. 채널에서 메세지 확인
※ 간단한 POST요청을 통한 메세지 전송방법
반응형
'Spring' 카테고리의 다른 글
spring cloud config client설정(parameter store) (0) | 2021.08.23 |
---|---|
spring cloud config server설정(AWS parameter store) (0) | 2021.08.20 |
spring cloud config server설정(git) (0) | 2021.08.15 |
Spring Security / JWT (0) | 2021.08.14 |