1
1
package com .app .toaster .external .client .discord ;
2
2
3
+ import java .io .IOException ;
3
4
import java .io .PrintWriter ;
4
5
import java .io .StringWriter ;
5
6
import java .net .URI ;
9
10
10
11
import org .springframework .beans .factory .annotation .Value ;
11
12
import org .springframework .core .env .Environment ;
13
+ import org .springframework .core .io .ByteArrayResource ;
12
14
import org .springframework .stereotype .Component ;
13
15
import org .springframework .transaction .event .TransactionPhase ;
14
16
import org .springframework .transaction .event .TransactionalEventListener ;
20
22
import jakarta .servlet .http .HttpServletRequest ;
21
23
import lombok .RequiredArgsConstructor ;
22
24
import lombok .extern .slf4j .Slf4j ;
25
+ import org .springframework .web .multipart .MultipartFile ;
23
26
24
27
@ RequiredArgsConstructor
25
28
@ Component
@@ -43,6 +46,19 @@ public void sendNotification(NotificationDto notification) {
43
46
switch (notification .type ()){
44
47
case ERROR -> discordClient .sendMessage (URI .create (webhookUrlError ), createErrorMessage (notification .e (), notification .request ()));
45
48
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 ()));
46
62
}
47
63
} catch (Exception error ) {
48
64
log .warn ("discord notification fail : " + error );
@@ -95,6 +111,33 @@ private DiscordMessage createErrorMessage(Exception e, String requestUrl) {
95
111
.build ();
96
112
}
97
113
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
+
98
141
private String createRequestFullPath (WebRequest webRequest ) {
99
142
HttpServletRequest request = ((ServletWebRequest ) webRequest ).getRequest ();
100
143
String fullPath = request .getMethod () + " " + request .getRequestURL ();
0 commit comments