From b00a50a469e0218874aad5b208903ff92ed091d6 Mon Sep 17 00:00:00 2001 From: liutianyou Date: Sat, 7 Sep 2024 10:38:44 +0800 Subject: [PATCH] [improve] beautify lark notification (#2683) --- .../impl/FlyBookAlertNotifyHandlerImpl.java | 254 +++++++++--------- 1 file changed, 134 insertions(+), 120 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java index 6c5c4ceaaae..89e482c21f6 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java @@ -17,17 +17,15 @@ package org.apache.hertzbeat.manager.component.alerter.impl; -import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import lombok.Data; +import java.util.Arrays; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.manager.NoticeReceiver; import org.apache.hertzbeat.common.entity.manager.NoticeTemplate; -import org.apache.hertzbeat.common.util.StrUtil; +import org.apache.hertzbeat.common.util.JsonUtil; import org.apache.hertzbeat.manager.support.exception.AlertNoticeException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -44,59 +42,23 @@ @Slf4j final class FlyBookAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl { + /** + * Title color corresponding to the alarm priority + */ + private static final String[] TITLE_COLOR = {"red", "yellow", "orange"}; + + @Override public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert alert) { try { - FlyBookWebHookDto flyBookWebHookDto = new FlyBookWebHookDto(); - flyBookWebHookDto.setMsgType("post"); - - Content content = new Content(); - flyBookWebHookDto.setContent(content); - - Post post = new Post(); - content.setPost(post); - - ZhCn zhCn = new ZhCn(); - post.setZhCn(zhCn); - - zhCn.setTitle("[" + bundle.getString("alerter.notify.title") + "]"); - - List contentList = new ArrayList<>(); - - FlyBookContent textContent = new FlyBookContent(); - textContent.setTag("text"); - textContent.setText(renderContent(noticeTemplate, alert)); - contentList.add(textContent); - - FlyBookContent linkContent = new FlyBookContent(); - linkContent.setTag("a"); - linkContent.setText(bundle.getString("alerter.notify.console")); - linkContent.setHref(alerterProperties.getConsoleUrl()); - contentList.add(linkContent); - - String userId = receiver.getUserId(); - List userIdList = StrUtil.analysisArgToList(userId); - if (userIdList != null && !userIdList.isEmpty()) { - List atContents = userIdList.stream() - .map(userID -> { - FlyBookContent atContent = new FlyBookContent(); - atContent.setTag("at"); - atContent.setUserId(userID); - return atContent; - }) - .toList(); - contentList.addAll(atContents); - } - - List> contents = Collections.singletonList(contentList); - zhCn.setContent(contents); - + String notificationContent = JsonUtil.toJson(renderContent(noticeTemplate, alert)); + String cardMessage = createLarkMessage(receiver.getUserId(), notificationContent, alert.getPriority()); String webHookUrl = alerterProperties.getFlyBookWebhookUrl() + receiver.getAccessToken(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); - HttpEntity flyEntity = new HttpEntity<>(flyBookWebHookDto, headers); + HttpEntity flyEntity = new HttpEntity<>(cardMessage, headers); ResponseEntity entity = restTemplate.postForEntity(webHookUrl, - flyEntity, CommonRobotNotifyResp.class); + flyEntity, CommonRobotNotifyResp.class); if (entity.getStatusCode() == HttpStatus.OK) { assert entity.getBody() != null; if (entity.getBody().getCode() == null || entity.getBody().getCode() == 0) { @@ -114,77 +76,129 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a } } - - @Override - public byte type() { - return 6; - } - - @Data - private static class FlyBookWebHookDto { - - private static final String DEFAULT_MSG_TYPE = "post"; - - /** - * Message type - */ - @JsonProperty("msg_type") - private String msgType = DEFAULT_MSG_TYPE; - - - private Content content; - - } - /** - * Message content + * create a lark notification card message + * + * @param userId at user id + * @param notificationContent notification content + * @param priority priority for alert + * @return message */ - @Data - private static class Content { - public Post post; - } - - @Data - private static class FlyBookContent { - /** - * format currently supports text、hyperlink、@people function - */ - public String tag; - - /** - * text - */ - public String text; - - /** - * hyperlink address - */ - public String href; - - @JsonProperty("user_id") - public String userId; - - @JsonProperty("user_name") - public String userName; - } - - @Data - private static class Post { - @JsonProperty("zh_cn") - public ZhCn zhCn; + private String createLarkMessage(String userId, String notificationContent, byte priority) { + String larkCardMessage = """ + { + "msg_type": "interactive", + "card": { + "config": { + "update_multi": true + }, + "i18n_elements": { + "zh_cn": [ + { + "tag": "column_set", + "flex_mode": "none", + "horizontal_spacing": "default", + "background_style": "default", + "columns": [ + { + "tag": "column", + "elements": [ + { + "tag": "div", + "text": { + "tag": "plain_text", + "content": "", + "text_size": "normal", + "text_align": "left", + "text_color": "default" + } + } + ], + "width": "weighted", + "weight": 1 + } + ] + }, + { + "tag": "column_set", + "flex_mode": "none", + "horizontal_spacing": "default", + "background_style": "default", + "columns": [ + { + "tag": "column", + "elements": [ + { + "tag": "div", + "text": { + "tag": "plain_text", + "content": %s, + "text_size": "normal", + "text_align": "left", + "text_color": "default" + } + } + ], + "width": "weighted", + "weight": 1 + } + ] + }, + %s + { + "tag": "action", + "actions": [ + { + "tag": "button", + "text": { + "tag": "plain_text", + "content": "登入控制台" + }, + "type": "default", + "complex_interaction": true, + "width": "default", + "size": "medium", + "multi_url": { + "url": "%s" + } + } + ] + } + ] + }, + "i18n_header": { + "zh_cn": { + "title": { + "tag": "plain_text", + "content": "HertzBeat 告警" + }, + "template": "%s" + } + } + } + } + """; + + String atUserElement = ""; + if (StringUtils.isNotBlank(userId)) { + String atUserId = Arrays.stream(userId.split(",")) + .map(id -> "") + .collect(Collectors.joining(" ")); + atUserElement = String.format(""" + { + "tag": "div", + "text": { + "content": "%s", + "tag": "lark_md" + } + }, + """, atUserId); + } + return String.format(larkCardMessage, notificationContent, atUserElement, alerterProperties.getConsoleUrl(), TITLE_COLOR[priority]); } - @Data - private static class ZhCn { - /** - * Title - */ - public String title; - - /** - * Content - */ - public List> content; + @Override + public byte type() { + return 6; } - }