๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[springboot] slack web hook_ ์Šฌ๋ž™์›นํ›… ์ง€์ • ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€/springboot

[springboot] slack web hook_ ์Šฌ๋ž™์›นํ›… ์ง€์ •

Minhhk 2024. 12. 8. 13:19
๐Ÿ’ก ์šฐ์„  ์•Œ๋ฆผ ๋ฐ›์„ ์ฑ„๋„์„ ์Šฌ๋ž™์— ๋งŒ๋“ค์ž 
๐Ÿ’ก #hooktest ์ฑ„๋„ ์šฐํด๋ฆญ → ์ฑ„๋„ ์„ธ๋ถ€์ •๋ณด ๋ณด๊ธฐ

 

 

 

 

 

 

 

 

 

 

 

 

 

์•„๋ž˜๋กœ ๋‚ด๋ ค ์ด๋ฆ„, ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ!

 

 

๐Ÿ’ก SpringBoot ์ฝ”๋“œ

 

import com.slack.api.Slack;
import com.slack.api.webhook.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SlackHttpClient {
    public void send(String message) {
        try {
            Slack instance = Slack.getInstance();
            Payload payload = Payload.builder()
                    .text(message)
                    .build();
            instance.send("SLACK WEB HOOK URL ์ž…๋ ฅ", payload); // webhook URL
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }}

SlackHttpClient ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ์ปจํŠธ๋กค๋Ÿฌ๋‚˜ ์„œ๋น„์Šค๋‹จ์—์„œ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉ ํ•˜๋ฉด๋œ๋‹ค.

์•Œ๋ฆผ ๋‚ด์šฉ์€ String.format ์ด๋‚˜ StringBuilder ๋กœ ์‚ฌ์šฉํ•ด์„œ ์ปค์Šคํ…€ ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์ž.

@RestController
@RequiredArgsConstructor
public class SlackController {

    private final SlackHttpClient slackHttpClient;

    @GetMapping("/api/slack/alarm")
    public String getAlarmTest() {
        return slackHttpClient.send("๋ฉ”์„ธ์ง€ ๋‚ด์šฉ");
    }
}

 

 

→ ์•Œ๋ฆผ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ