Skip to content

Commit

Permalink
[feature] add at function for wechat (#1994)
Browse files Browse the repository at this point in the history
Signed-off-by: linDong <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
Yanshuming1 and tomsun28 authored May 17, 2024
1 parent 479b454 commit b473de4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WeWorkWebHookDto> 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());
Expand All @@ -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<String> 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;
Expand Down Expand Up @@ -105,6 +144,11 @@ private static class WeWorkWebHookDto {
*/
private MarkdownDTO markdown;

/**
* text message
*/
private TextDTO text;

@Data
private static class MarkdownDTO {

Expand All @@ -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<String> mentionedList;
/**
* @ phone
*/
@JsonProperty(value = "mentioned_mobile_list")
private List<String> mentionedMobileList;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,18 @@
/>
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 4">
<nz-form-label [nzSpan]="7" nzFor="phone">{{ 'alert.notice.type.phone' | i18n }}</nz-form-label>
<nz-form-control [nzErrorTip]="'validation.phone.invalid' | i18n" [nzSpan]="12">
<input [(ngModel)]="receiver.phone" [required]="receiver.type === 4" id="WePhone" name="phone" nz-input type="tel" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 4">
<nz-form-label [nzSpan]="7" nzFor="tgUserId">{{ 'alert.notice.type.wework-userId' | i18n }}</nz-form-label>
<nz-form-control [nzErrorTip]="'validation.required' | i18n" [nzSpan]="12">
<input [(ngModel)]="receiver.tgUserId" [required]="receiver.type === 4" name="tgUserId" nz-input type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 5">
<nz-form-label [nzSpan]="7" nzFor="accessToken" [nzRequired]="receiver.type === 5">{{
'alert.notice.type.access-token' | i18n
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "飞书机器人",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "飛書機器人",
Expand Down

0 comments on commit b473de4

Please sign in to comment.