企业微信发送消息api_微信公众号发消息给用户

2022-11-10 18:27:15 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

最近,接手了告警的一个需求。详细需求:监控一个应用的某些指标超标了,要提醒用户,通过企业微信给指定用户发送告警信息;今日自己实现了一下,总结出来分享给大家。

注意:代码亲自编写,已自测通过

文章目录

  • 前言
  • 一、编码?
    • 1.依赖
    • 2.SendWX.java
    • 3.WeChatMsgSend.java
    • 4.WeChatData.java
    • 5.WeChatUrlData.java
  • 二、参数
    • 1.构建自己的企业微信
    • 2.参数详细获取
  • 总结

前言

通过企业微信给指定用户发送告警信息

一、编码?

1.依赖

代码语言:javascript复制
<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

代码语言:javascript复制
/** * 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

代码语言:javascript复制
/** * 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

代码语言:javascript复制
/** * 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

代码语言:javascript复制
/** * 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:

对应代码的:

最后,打开通讯录:

对应代码:

总结

快去试一试吧~~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/188440.html原文链接:https://javaforall.cn

0 人点赞