1717
1818package org .apache .hertzbeat .alert .notice .impl ;
1919
20- import java .util .Arrays ;
21- import java .util .stream .Collectors ;
2220import lombok .RequiredArgsConstructor ;
2321import lombok .extern .slf4j .Slf4j ;
2422import org .apache .commons .lang3 .StringUtils ;
2927import org .apache .hertzbeat .common .util .JsonUtil ;
3028import org .springframework .http .HttpEntity ;
3129import org .springframework .http .HttpHeaders ;
32- import org .springframework .http .HttpStatus ;
3330import org .springframework .http .MediaType ;
31+ import org .springframework .http .HttpStatus ;
3432import org .springframework .http .ResponseEntity ;
3533import org .springframework .stereotype .Component ;
3634
35+ import java .util .Arrays ;
36+ import java .util .stream .Collectors ;
37+
3738/**
3839 * Send alert information through FeiShu
3940 */
@@ -47,19 +48,18 @@ final class FlyBookAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl
4748 */
4849 private static final String [] TITLE_COLOR = {"red" , "yellow" , "orange" };
4950
50-
5151 @ Override
5252 public void send (NoticeReceiver receiver , NoticeTemplate noticeTemplate , GroupAlert alert ) {
5353 try {
5454 String notificationContent = JsonUtil .toJson (renderContent (noticeTemplate , alert ));
55- // todo priority custom the color
55+ // todo priority custom the color
5656 String cardMessage = createLarkMessage (receiver .getUserId (), notificationContent , (byte ) 1 );
5757 String webHookUrl = alerterProperties .getFlyBookWebhookUrl () + receiver .getAccessToken ();
5858 HttpHeaders headers = new HttpHeaders ();
5959 headers .setContentType (MediaType .APPLICATION_JSON );
6060 HttpEntity <String > flyEntity = new HttpEntity <>(cardMessage , headers );
6161 ResponseEntity <CommonRobotNotifyResp > entity = restTemplate .postForEntity (webHookUrl ,
62- flyEntity , CommonRobotNotifyResp .class );
62+ flyEntity , CommonRobotNotifyResp .class );
6363 if (entity .getStatusCode () == HttpStatus .OK ) {
6464 assert entity .getBody () != null ;
6565 if (entity .getBody ().getCode () == null || entity .getBody ().getCode () == 0 ) {
@@ -87,115 +87,120 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAl
8787 */
8888 private String createLarkMessage (String userId , String notificationContent , byte priority ) {
8989 String larkCardMessage = """
90- {
91- "msg_type": "interactive",
92- "card": {
93- "config": {
94- "update_multi": true
95- },
96- "i18n_elements": {
97- "zh_cn": [
98- {
99- "tag": "column_set",
100- "flex_mode": "none",
101- "horizontal_spacing": "default",
102- "background_style": "default",
103- "columns": [
104- {
105- "tag": "column",
106- "elements": [
107- {
108- "tag": "div",
109- "text": {
110- "tag": "plain_text",
111- "content": "",
112- "text_size": "normal",
113- "text_align": "left",
114- "text_color": "default"
115- }
116- }
117- ],
118- "width": "weighted",
119- "weight": 1
120- }
121- ]
122- },
123- {
124- "tag": "column_set",
125- "flex_mode": "none",
126- "horizontal_spacing": "default",
127- "background_style": "default",
128- "columns": [
129- {
130- "tag": "column",
131- "elements": [
132- {
133- "tag": "div",
134- "text": {
135- "tag": "plain_text",
136- "content": %s,
137- "text_size": "normal",
138- "text_align": "left",
139- "text_color": "default"
140- }
141- }
142- ],
143- "width": "weighted",
144- "weight": 1
145- }
146- ]
147- },
148- %s
149- {
150- "tag": "action",
151- "actions": [
152- {
153- "tag": "button",
154- "text": {
155- "tag": "plain_text",
156- "content": "登入控制台"
157- },
158- "type": "default",
159- "complex_interaction": true,
160- "width": "default",
161- "size": "medium",
162- "multi_url": {
163- "url": "%s"
164- }
165- }
166- ]
167- }
168- ]
169- },
170- "i18n_header": {
171- "zh_cn": {
172- "title": {
173- "tag": "plain_text",
174- "content": "HertzBeat 告警"
175- },
176- "template": "%s"
177- }
178- }
90+ {
91+ "msg_type": "interactive",
92+ "card": {
93+ "schema": "2.0",
94+ "config": {
95+ "update_multi": true,
96+ "locales": [
97+ "en_us",
98+ "zh_cn"
99+ ],
100+ "style": {
101+ "text_size": {
102+ "normal_v2": {
103+ "default": "normal",
104+ "pc": "normal",
105+ "mobile": "heading"
106+ }
107+ }
108+ }
109+ },
110+ "body": {
111+ "direction": "vertical",
112+ "padding": "12px 12px 12px 12px",
113+ "elements": [
114+ {
115+ "tag": "markdown",
116+ "content": "%s",
117+ "i18n_content": {
118+ "en_us": ""
119+ },
120+ "text_align": "left",
121+ "text_size": "normal_v2",
122+ "margin": "0px 0px 0px 0px"
123+ },
124+ {
125+ "tag": "hr",
126+ "margin": "0px 0px 0px 0px"
127+ },
128+ {
129+ "tag": "column_set",
130+ "horizontal_align": "left",
131+ "columns": [
132+ {
133+ "tag": "column",
134+ "width": "weighted",
135+ "elements": [
136+ {
137+ "tag": "button",
138+ "text": {
139+ "tag": "plain_text",
140+ "content": "登入控制台",
141+ "i18n_content": {
142+ "en_us": "Login In"
143+ }
144+ },
145+ "type": "default",
146+ "width": "default",
147+ "size": "medium",
148+ "behaviors": [
149+ {
150+ "type": "open_url",
151+ "default_url": "%s",
152+ "pc_url": "",
153+ "ios_url": "",
154+ "android_url": ""
155+ }
156+ ]
157+ }
158+ ],
159+ "direction": "horizontal",
160+ "vertical_spacing": "8px",
161+ "horizontal_align": "left",
162+ "vertical_align": "top",
163+ "weight": 1
164+ }
165+ ],
166+ "margin": "0px 0px 0px 0px"
167+ }
168+ ]
169+ },
170+ "header": {
171+ "title": {
172+ "tag": "plain_text",
173+ "content": "HertzBeat 告警",
174+ "i18n_content": {
175+ "en_us": "HertzBeat Alarm"
176+ }
177+ },
178+ "subtitle": {
179+ "tag": "plain_text",
180+ "content": ""
181+ },
182+ "template": "%s",
183+ "padding": "12px 12px 12px 12px"
184+ }
185+ }
179186 }
180- }
181- """ ;
187+ """ ;
182188
183189 String atUserElement = "" ;
184190 if (StringUtils .isNotBlank (userId )) {
185- String atUserId = Arrays .stream (userId .split ("," ))
186- .map (id -> "<at id=" + id + "></at>" )
187- .collect (Collectors .joining (" " ));
188- atUserElement = String .format ("""
189- {
190- "tag": "div",
191- "text": {
192- "content": "%s",
193- "tag": "lark_md"
194- }
195- },
196- """ , atUserId );
191+ atUserElement = "\\ n" + Arrays .stream (userId .split ("," ))
192+ .map (id -> "<at id=" + id + "></at>" )
193+ .collect (Collectors .joining (" " ));
197194 }
198- return String .format (larkCardMessage , notificationContent , atUserElement , alerterProperties .getConsoleUrl (), TITLE_COLOR [priority ]);
195+
196+ if (notificationContent .startsWith ("\" " ) && notificationContent .endsWith ("\" " )) {
197+ notificationContent = StringUtils .removeStart (notificationContent , "\" " );
198+ notificationContent = StringUtils .removeEnd (notificationContent , "\" " );
199+ }
200+
201+ return String .format (larkCardMessage ,
202+ notificationContent .replace ("\" " , "\\ \" " ) + atUserElement ,
203+ alerterProperties .getConsoleUrl (), TITLE_COLOR [priority ]);
199204 }
200205
201206 @ Override
0 commit comments