diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java index 3fab4e42089..29457c51b65 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java @@ -17,12 +17,18 @@ package org.apache.hertzbeat.manager.component.alerter.impl; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Objects; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.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; @@ -58,6 +64,12 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a assert entity.getBody() != null; if (entity.getBody().getErrCode() == 0) { log.debug("Send WeWork webHook: {} Success", webHookUrl); + WeWorkWebHookDto weWorkWebHookTextDto = checkNeedAtNominator(receiver, alert); + if (!Objects.isNull(weWorkWebHookTextDto)) { + HttpEntity httpEntityText = new HttpEntity<>(weWorkWebHookTextDto, headers); + restTemplate.postForEntity(webHookUrl, httpEntityText, CommonRobotNotifyResp.class); + } + } else { log.warn("Send WeWork webHook: {} Failed: {}", webHookUrl, entity.getBody().getErrMsg()); throw new AlertNoticeException(entity.getBody().getErrMsg()); @@ -71,6 +83,33 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a } } + private WeWorkWebHookDto checkNeedAtNominator(NoticeReceiver receiver, Alert alert) { + if (StringUtils.isBlank(receiver.getPhone()) && StringUtils.isBlank(receiver.getTgUserId())) { + return null; + } + WeWorkWebHookDto weWorkWebHookTextDto = new WeWorkWebHookDto(); + weWorkWebHookTextDto.setMsgtype(WeWorkWebHookDto.TEXT); + WeWorkWebHookDto.TextDTO textDto = new WeWorkWebHookDto.TextDTO(); + if (StringUtils.isNotBlank(receiver.getPhone())) { + textDto.setMentionedMobileList(analysisArgToList(receiver.getPhone())); + weWorkWebHookTextDto.setText(textDto); + } + if (StringUtils.isNotBlank(receiver.getTgUserId())) { + textDto.setMentionedList(analysisArgToList(receiver.getTgUserId())); + weWorkWebHookTextDto.setText(textDto); + } + return weWorkWebHookTextDto; + + } + + private List analysisArgToList(String arg) { + if (StringUtils.isBlank(arg)) { + return Collections.emptyList(); + } + //english symbol + return Arrays.asList(arg.split("\\s*,\\s*")); + } + @Override public byte type() { return 4; @@ -105,6 +144,11 @@ private static class WeWorkWebHookDto { */ private MarkdownDTO markdown; + /** + * text message + */ + private TextDTO text; + @Data private static class MarkdownDTO { @@ -114,5 +158,24 @@ private static class MarkdownDTO { private String content; } + @Data + private static class TextDTO { + + /** + * message content + */ + private String content; + /** + * @ userId + */ + @JsonProperty(value = "mentioned_list") + private List mentionedList; + /** + * @ phone + */ + @JsonProperty(value = "mentioned_mobile_list") + private List mentionedMobileList; + } + } } diff --git a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html index 023b329ac31..af6b5b62b60 100644 --- a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html +++ b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html @@ -584,6 +584,18 @@ /> + + {{ 'alert.notice.type.phone' | i18n }} + + + + + + {{ 'alert.notice.type.wework-userId' | i18n }} + + + + {{ 'alert.notice.type.access-token' | i18n diff --git a/web-app/src/assets/i18n/en-US.json b/web-app/src/assets/i18n/en-US.json index e744c1ef5d8..5f163c97768 100644 --- a/web-app/src/assets/i18n/en-US.json +++ b/web-app/src/assets/i18n/en-US.json @@ -271,6 +271,7 @@ "alert.notice.type.wechat-id": "WeChat OPENID", "alert.notice.type.wework": "WeWork Robot", "alert.notice.type.wework-key": "WeWork Robot KEY", + "alert.notice.type.wework-userId": "UserId", "alert.notice.type.access-token": "Robot ACCESS_TOKEN", "alert.notice.type.ding": "DingDing Robot", "alert.notice.type.fei-shu": "FeiShu Robot", diff --git a/web-app/src/assets/i18n/zh-CN.json b/web-app/src/assets/i18n/zh-CN.json index 4ae1609ce8c..ca316a7094a 100644 --- a/web-app/src/assets/i18n/zh-CN.json +++ b/web-app/src/assets/i18n/zh-CN.json @@ -271,6 +271,7 @@ "alert.notice.type.wechat-id": "微信OPENID", "alert.notice.type.wework": "企业微信机器人", "alert.notice.type.wework-key": "企业微信机器人KEY", + "alert.notice.type.wework-userId": "用户id", "alert.notice.type.access-token": "机器人ACCESS_TOKEN", "alert.notice.type.ding": "钉钉机器人", "alert.notice.type.fei-shu": "飞书机器人", diff --git a/web-app/src/assets/i18n/zh-TW.json b/web-app/src/assets/i18n/zh-TW.json index 59b8a9726ce..80661e48a5d 100644 --- a/web-app/src/assets/i18n/zh-TW.json +++ b/web-app/src/assets/i18n/zh-TW.json @@ -270,6 +270,7 @@ "alert.notice.type.wechat-id": "微信OPENID", "alert.notice.type.wework": "企業微信機器人", "alert.notice.type.wework-key": "企業微信機器人KEY", + "alert.notice.type.wework-userId": "用户id", "alert.notice.type.access-token": "機器人ACCESS_TOKEN", "alert.notice.type.ding": "釘釘機器人", "alert.notice.type.fei-shu": "飛書機器人",