-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[task#736]Add enterprise WeChat application notifications for alerts (#…
…844) [manage]Add enterprise WeChat application notifications for alerts Co-authored-by: hudongdong <hudongdong9406@zto.com>
- Loading branch information
1 parent
dde9a91
commit 4291624
Showing
10 changed files
with
242 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.dromara.hertzbeat.manager.component.alerter.impl; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.dromara.hertzbeat.common.entity.alerter.Alert; | ||
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; | ||
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler; | ||
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppDTO; | ||
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppReq; | ||
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @description: | ||
* @author: hdd | ||
* @create: 2023/04/04 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class WeChatAppAlertNotifyHandlerImpl implements AlertNotifyHandler { | ||
|
||
/** | ||
* send weChat app message url | ||
*/ | ||
private static final String APP_MESSAGE_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"; | ||
|
||
/** | ||
* get access_token url | ||
*/ | ||
private static final String SECRET_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"; | ||
|
||
/** | ||
* 应用消息发送对象 | ||
*/ | ||
private static final String DEFAULT_ALL = "@all"; | ||
|
||
/** | ||
* send message type | ||
*/ | ||
private static final String DEFAULT_TYPE = "text"; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
@Override | ||
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException { | ||
String corpId = receiver.getCorpId(); | ||
Integer agentId = receiver.getAgentId(); | ||
String appSecret = receiver.getAppSecret(); | ||
|
||
try { | ||
ResponseEntity<WeChatAppReq> entityResponse = restTemplate.getForEntity(String.format(SECRET_URL, corpId, appSecret), WeChatAppReq.class); | ||
if (Objects.nonNull(entityResponse.getBody())) { | ||
String accessToken = entityResponse.getBody().getAccessToken(); | ||
WeChatAppDTO.TextDTO textDTO = new WeChatAppDTO.TextDTO(); | ||
textDTO.setContent(JSON.toJSONString(alert)); | ||
WeChatAppDTO weChatAppDTO = WeChatAppDTO.builder() | ||
.toUser(DEFAULT_ALL) | ||
.msgType(DEFAULT_TYPE) | ||
.agentId(agentId) | ||
.text(textDTO) | ||
.build(); | ||
ResponseEntity<WeChatAppReq> response = restTemplate.postForEntity(String.format(APP_MESSAGE_URL, accessToken), weChatAppDTO, WeChatAppReq.class); | ||
if (Objects.nonNull(response.getBody()) && !Objects.equals(response.getBody().getErrCode(), 0)) { | ||
log.warn("Send Enterprise WeChat App Error: {} Failed: {}", receiver.getHookUrl(), response.getBody().getErrMsg()); | ||
throw new AlertNoticeException("Http StatusCode " + response.getStatusCode()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new AlertNoticeException("[WebHook Notify Error] " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public byte type() { | ||
return 10; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.dromara.hertzbeat.manager.pojo.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @description: | ||
* @author: hdd | ||
* @create: 2023/04/05 | ||
*/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class WeChatAppDTO { | ||
|
||
@JsonProperty(value = "touser") | ||
private String toUser; | ||
|
||
@JsonProperty(value = "toparty") | ||
private String toParty; | ||
|
||
@JsonProperty(value = "totag") | ||
private String toTag; | ||
|
||
@JsonProperty(value = "msgtype") | ||
private String msgType; | ||
|
||
@JsonProperty(value = "agentid") | ||
private Integer agentId; | ||
|
||
|
||
private TextDTO text; | ||
|
||
|
||
@Data | ||
public static class TextDTO { | ||
/** | ||
* 消息内容 | ||
*/ | ||
private String content; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.dromara.hertzbeat.manager.pojo.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @description: | ||
* @author: hdd | ||
* @create: 2023/04/05 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class WeChatAppReq { | ||
|
||
@JsonProperty(value = "errcode") | ||
private Integer errCode; | ||
|
||
@JsonProperty(value = "errmsg") | ||
private String errMsg; | ||
|
||
@JsonProperty(value = "access_token") | ||
private String accessToken; | ||
} |
22 changes: 22 additions & 0 deletions
22
...dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.dromara.hertzbeat.manager.component.alerter.impl; | ||
|
||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @description: | ||
* @author: hdd | ||
* @create: 2023/04/05 | ||
*/ | ||
public class WeChatAppAlertNotifyHandlerImplTest { | ||
|
||
|
||
@InjectMocks | ||
private WeChatAppAlertNotifyHandlerImpl weChatAppAlertNotifyHandler; | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters