11package com .app .toaster .external .client .discord ;
22
3+ import java .io .IOException ;
34import java .io .PrintWriter ;
45import java .io .StringWriter ;
56import java .net .URI ;
910
1011import org .springframework .beans .factory .annotation .Value ;
1112import org .springframework .core .env .Environment ;
13+ import org .springframework .core .io .ByteArrayResource ;
1214import org .springframework .stereotype .Component ;
1315import org .springframework .transaction .event .TransactionPhase ;
1416import org .springframework .transaction .event .TransactionalEventListener ;
2022import jakarta .servlet .http .HttpServletRequest ;
2123import lombok .RequiredArgsConstructor ;
2224import lombok .extern .slf4j .Slf4j ;
25+ import org .springframework .web .multipart .MultipartFile ;
2326
2427@ RequiredArgsConstructor
2528@ Component
@@ -43,6 +46,19 @@ public void sendNotification(NotificationDto notification) {
4346 switch (notification .type ()){
4447 case ERROR -> discordClient .sendMessage (URI .create (webhookUrlError ), createErrorMessage (notification .e (), notification .request ()));
4548 case SIGNUP -> discordClient .sendMessage (URI .create (webhookUrlSign ), createSignUpMessage ());
49+ case ADMIN -> discordClient .sendMessage (URI .create (webhookUrlError ), createAdminMessage (notification .request ()));
50+ }
51+ } catch (Exception error ) {
52+ log .warn ("discord notification fail : " + error );
53+ }
54+ }
55+ }
56+
57+ public void sendAdmin (NotificationDto notification ) {
58+ if (!Arrays .asList (environment .getActiveProfiles ()).contains ("local" )) { // 일단 로컬 막아두겠습니다. TODO: 웹훅 주소 바꾸기
59+ try {
60+ switch (notification .type ()){
61+ case ADMIN -> discordClient .sendMessage (URI .create (webhookUrlError ), createAdminMessage (notification .request ()));
4662 }
4763 } catch (Exception error ) {
4864 log .warn ("discord notification fail : " + error );
@@ -95,6 +111,33 @@ private DiscordMessage createErrorMessage(Exception e, String requestUrl) {
95111 .build ();
96112 }
97113
114+ private DiscordMessage createAdminMessage (String file ) throws IOException {
115+ return DiscordMessage .builder ()
116+ .content ("# 😍스웨거 MFA 만들어보기" )
117+ .embeds (
118+ List .of (
119+ DiscordMessage .Embed .builder ()
120+ .title ("ℹ️ 에러 정보" )
121+ .description (
122+ "### 🕖 발생 시간\n "
123+ + LocalDateTime .now ()
124+ + "\n "
125+ + "### 🔗 요청 URL\n "
126+ + "스웨거 테스트"
127+ + "\n "
128+ + "### 📄 Stack Trace\n "
129+ + "\n ```" )
130+ .image (DiscordMessage .EmbedImage .builder ()
131+ .url (file )
132+ .height (300 )
133+ .width (300 )
134+ .build ()
135+ ).build ()
136+ )
137+ )
138+ .build ();
139+ }
140+
98141 private String createRequestFullPath (WebRequest webRequest ) {
99142 HttpServletRequest request = ((ServletWebRequest ) webRequest ).getRequest ();
100143 String fullPath = request .getMethod () + " " + request .getRequestURL ();
0 commit comments