大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
最近,接手了告警的一个需求。详细需求:监控一个应用的某些指标超标了,要提醒用户,通过企业微信给指定用户发送告警信息;今日自己实现了一下,总结出来分享给大家。
注意:代码亲自编写,已自测通过
文章目录
前言
通过企业微信给指定用户发送告警信息
一、编码?
1.依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2.SendWX.java
/** * Created by Domi on 2020/10/21. */
public class SendWX {
/** * 发送消息的执行方法 * @Param [alertTitle, alertMsg] * @return void **/
public static void send(String alertTitle, String alertMsg){
WeChatMsgSend swx = new WeChatMsgSend();
try {
//token--企业微信获取
String token = swx.getToken("ww78696d5d79e37874", "TczeIo8tQQ8AqtKxAnw380ZNNDS_jaSgNtX2AMs-K7E");
//发送的数据
String postdata = swx.createpostdata("SongPengJu", "text", 1000002, "content", "告警信息:" + alertTitle + "\n内容:" + alertMsg);
//响应结果
String resp = swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE, (new WeChatUrlData()).getSendMessage_Url(), postdata, token);
System.out.println("获取到的token======>" + token);
System.out.println("请求数据======>" + postdata);
System.out.println("发送微信的响应数据======>" + resp);
}catch (Exception e){
e.getStackTrace();
}
}
/** * 测试 * @Param [args] * @return void **/
public static void main(String[] args) {
send("您的应用XXX","告警啦告警啦告警啦告警啦~");
}
}
3.WeChatMsgSend.java
/** * Created by Domi on 2020/10/21. */
public class WeChatMsgSend {
private CloseableHttpClient httpClient;
// 用于提交登录数据
private HttpPost httpPost;
// 用于获得登陆后页面
private HttpGet httpGet;
public static final String CONTENT_TYPE = "Content-Type";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Gson gson = new Gson();
/** * 微信授权请求,GET类型,获取授权响应,用于其他方法截取token * @Param Get_Token_Url * @return String 授权响应内容 **/
protected String toAuth(String Get_Token_Url) throws IOException {
httpClient = HttpClients.createDefault();
httpGet = new HttpGet(Get_Token_Url);
CloseableHttpResponse response = httpClient.execute(httpGet);
String resp = "";
try {
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.getStackTrace();
} finally {
response.close();
}
LoggerFactory.getLogger(getClass()).info(" resp:{}", resp);
return resp;
}
/** * corpid应用组织编号 corpsecret应用秘钥 * 获取toAuth(String Get_Token_Url)返回结果中键值对中access_token键的值 * @Param [corpid, corpsecret] * @return java.lang.String **/
public String getToken(String corpid, String corpsecret) throws IOException {
WeChatMsgSend sw = new WeChatMsgSend();
WeChatUrlData uData = new WeChatUrlData();
uData.setGet_Token_Url(corpid, corpsecret);//拿到token连接
String resp = sw.toAuth(uData.getGet_Token_Url());//授权信息
System.out.println("resp=====:" + resp);//输出日志
try {
Map<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() {
}.getType());
return map.get("access_token").toString();
} catch (Exception e) {
e.getStackTrace();
return resp;
}
}
/** * 创建微信发送请求post数据 touser发送消息接收者 ,msgtype消息类型(文本/图片等), application_id应用编号。 * 本方法适用于text型微信消息,contentKey和contentValue只能组一对 * @Param [touser, msgtype, application_id, contentKey, contentValue] * @return java.lang.String **/
public String createpostdata(String touser, String msgtype, int application_id, String contentKey,
String contentValue) {
WeChatData wcd = new WeChatData();
wcd.setTouser(touser);
wcd.setAgentid(application_id + "");
wcd.setMsgtype(msgtype);
Map<Object, Object> content = new HashMap<Object, Object>();
content.put(contentKey, contentValue);
wcd.setText(content);
return gson.toJson(wcd);
}
/** * @Title 创建微信发送请求post实体,charset消息编码 ,contentType消息体内容类型, * url微信消息发送请求地址,data为post数据,token鉴权token * @Param [charset, contentType, url, data, token] * @return java.lang.String **/
public String post(String charset, String contentType, String url, String data, String token) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
httpPost = new HttpPost(url + token);
httpPost.setHeader(CONTENT_TYPE, contentType);
httpPost.setEntity(new StringEntity(data, charset));
CloseableHttpResponse response = httpclient.execute(httpPost);
String resp;
try {
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, charset);
EntityUtils.consume(entity);
} finally {
response.close();
}
LoggerFactory.getLogger(getClass()).info("call [{}], param:{}, resp:{}", url, data, resp);
return resp;
}
}
4.WeChatData.java
/** * Created by Domi on 2020/10/21. */
@Data
public class WeChatData {
private String touser;
private String msgtype;
private String agentid;
private Object text;
}
5.WeChatUrlData.java
/** * Created by Domi on 2020/10/21. */
@Data
public class WeChatUrlData {
private String corpid;
private String corpsecret;
private String Get_Token_Url;
private String SendMessage_Url;
public void setGet_Token_Url(String corpid,String corpsecret) {
Get_Token_Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;
}
public String getSendMessage_Url() {
SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
return SendMessage_Url;
}
public void setSendMessage_Url(String sendMessage_Url) {
SendMessage_Url = sendMessage_Url;
}
}
二、参数
1.构建自己的企业微信
开始创建企业微信官网https://work.weixin.qq.com/注册,并登陆。
点击‘应用管理’,自建里面创建应用:
2.参数详细获取
然后进入自己创建的应用,找到这两个信息:
对应代码的:
然后打开我的企业最下面有个企业ID:
对应代码的:
最后,打开通讯录:
对应代码:
总结
快去试一试吧~~
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188440.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...