Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add at function for wechat #1994

Merged
merged 13 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 All @@ -33,6 +34,10 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Send alarm information through enterprise WeChat
Expand All @@ -58,6 +63,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 +82,34 @@ 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();
String alertMessage = String.format("警告对象:%s\n详情:%s", alert.getTarget(), alert.getContent());
Yanshuming1 marked this conversation as resolved.
Show resolved Hide resolved
textDto.setContent(alertMessage);
if (StringUtils.isNotBlank(receiver.getPhone())) {
textDto.setMentioned_mobile_list(analysisArgToList(receiver.getPhone()));
weWorkWebHookTextDto.setText(textDto);
Yanshuming1 marked this conversation as resolved.
Show resolved Hide resolved
}
if (StringUtils.isNotBlank(receiver.getTgUserId())) {
textDto.setMentioned_list(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 @@ -104,6 +143,10 @@ private static class WeWorkWebHookDto {
* markdown message
*/
private MarkdownDTO markdown;
/**
* text message
*/
private TextDTO text;

@Data
private static class MarkdownDTO {
Expand All @@ -113,6 +156,22 @@ private static class MarkdownDTO {
*/
private String content;
}
@Data
private static class TextDTO {

/**
* message content
*/
private String content;
/**
* @ userId
*/
private List<String> mentioned_list;
Yanshuming1 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @ phone
*/
private List<String> mentioned_mobile_list;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,20 @@
/>
</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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"alert.notice.type.wework-userId": "userId",
"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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, the i18n zh-TW.json also need this.

"alert.notice.type.access-token": "机器人ACCESS_TOKEN",
"alert.notice.type.ding": "钉钉机器人",
"alert.notice.type.fei-shu": "飞书机器人",
Expand Down
Loading