|
| 1 | +package com.example.solidconnection.common.discord; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.http.HttpEntity; |
| 8 | +import org.springframework.http.HttpHeaders; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.scheduling.annotation.Async; |
| 11 | +import org.springframework.scheduling.annotation.EnableAsync; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.web.client.RestTemplate; |
| 14 | + |
| 15 | +@Component |
| 16 | +@RequiredArgsConstructor |
| 17 | +@EnableAsync |
| 18 | +@Slf4j |
| 19 | +public class DiscordNotifier { |
| 20 | + |
| 21 | + private static final String ADMIN_PAGE_URL = "https://admins.solid-connection.com"; |
| 22 | + |
| 23 | + private final RestTemplate restTemplate; |
| 24 | + |
| 25 | + @Value("${discord.webhook-url:}") |
| 26 | + private String webhookUrl; |
| 27 | + |
| 28 | + @Value("${spring.profiles.active:}") |
| 29 | + private String environment; |
| 30 | + |
| 31 | + @Async |
| 32 | + public void notify(DiscordNotificationType type, String applicantInfo) { |
| 33 | + if (webhookUrl.isBlank()) { |
| 34 | + return; |
| 35 | + } |
| 36 | + try { |
| 37 | + HttpHeaders headers = new HttpHeaders(); |
| 38 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 39 | + HttpEntity<Map<String, String>> request = new HttpEntity<>(Map.of("content", buildMessage(type, applicantInfo)), headers); |
| 40 | + restTemplate.postForEntity(webhookUrl, request, Void.class); |
| 41 | + } catch (Exception e) { |
| 42 | + log.error("Discord 검수 알림 전송 실패. type={}, applicantInfo={}", type, applicantInfo, e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private String buildMessage(DiscordNotificationType type, String applicantInfo) { |
| 47 | + return "[%s] %s 검수 요청이 등록되었습니다.\n신청자: %s\n관리자 페이지: %s" |
| 48 | + .formatted(environment.toUpperCase(), type.getDisplayName(), applicantInfo, ADMIN_PAGE_URL); |
| 49 | + } |
| 50 | +} |
0 commit comments