david's daily developer note

[BE] Slack API - Log notice 본문

[Develop] Web/Back-end

[BE] Slack API - Log notice

mouse-david 2025. 4. 14. 22:55
728x90

현재 스프링부트로 개발하고 있는 서버의 로그를 Slack API를 사용하여 출력해보자.

1. 워크스페이스 생성하기

https://slack.com/create

 

Slack

nav.top { position: relative; } #page_contents > h1 { width: 920px; margin-right: auto; margin-left: auto; } h2, .align_margin { padding-left: 50px; } .card { width: 920px; margin: 0 auto; .card { width: 880px; } } .linux_col { display: none; } .platform_i

slack.com

 

2. 슬랙 앱 생성하기

https://api.slack.com/apps/new

 

Slack API: Applications | Slack

Your Apps Don't see an app you're looking for? Sign in to another workspace.

api.slack.com

상기 링크에서 Create an App 버튼 클릭하고 팝업에서 "From a manifest" 선택

1단계에서 생성한 워크스페이스 선택

매니페스트 파일 생성

구성 정보를 확인하고 생성 버튼 클릭

3. 권한 설정

https://api.slack.com/methods

 

Web API methods | Slack

 

api.slack.com

 

4. 게시 채널 (채팅방) 만들기

5. 앱 인스톨하기 (채널에 앱 추가)

Features ▶ OAuth & Permissions ▶ OAuth Tokens ▶Install to {work space name}

채널에 앱이 추가된 것을 확인
앱이 정상적으로 설치되면, 이후 앱 동작을 위한 토큰이 생성됨

6. 메시지 코드 수행

final String token = "Bot User OAuth Token";
final String channel = "Channal id";

try {
    LinkedHashMap<String, Object> slackMsgParam = new LinkedHashMap<>();
    slackMsgParam.put("channel", channel);
    slackMsgParam.put("text", message);

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost("Webhook URLs for Your Workspace");
    request.setHeader("Content-Type", "application/json");
    request.addHeader("Authorization",  "Bearer "+ token);
    request.setEntity(new StringEntity(new Gson().toJson(slackMsgParam)));

    HttpResponse response = client.execute(request);

} catch (IOException e) {
    logger.error("slack message is failed : {}", e.getMessage());
}

1번줄의 token값은 상단 그림의 토큰 값
2번줄의 채널ID값은 아래 경로로 확인 가능

채널 세부정보 최하단 채널 ID

요청 URL은 다음에서 확인

728x90