调用企业微信API给用户发消息
最近,接手了告警的一个需求。详细需求:监控一个应用的某些指标超标了,要提醒用户,通过企业微信给指定用户发送告警信息;今日自己实现了一下,总结出来分享给大家。
注意:代码亲自编写,已自测通过
文章目录
- 前言
- 一、编码?
- 1.依赖
- 2.SendWX.java
- 3.WeChatMsgSend.java
- 4.WeChatData.java
- 5.WeChatUrlData.java
- 二、参数
- 1.构建自己的企业微信
- 2.参数详细获取
- 总结
前言
通过企业微信给指定用户发送告警信息
一、编码?
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:
对应代码的:
最后,打开通讯录:
对应代码:
总结
快去试一试吧~~
调用企业微信API给用户发消息相关推荐
- 【Linux学习】如何编写Shell脚本调用企业微信api来发消息给企业微信成员?
一.前言 最近通过python实现了发送消息给企业微信的功能,参考链接: [Jenkins学习 ]如何编写Python脚本来调用企业微信的api通知企业微信成员关于Jenkins的编译结果? http ...
- python企业微信特定用户_python3调用企业微信api!开发一款属于自己的企业微信...
python3调用企业微信api 最后更新时间:2020/5/11 前段时间,我将企业微信官方提供的python接口代码的部分功能修改成了python3的,并且自己也使用并测试过部分功能: 因为并没有 ...
- java调用个人微信api接口实现收发消息发朋友圈
个人微信api接口,java调用个人微信api接口实现收发消息发朋友圈 1.微信好友收发消息 /** * 给微信好友发消息 * @author wechatno:t ...
- 调用个人微信API协议接口收发消息,发朋友圈
调用个人微信API协议接口收发消息,发朋友圈 java调用个人微信的API接口收发消息 /** * 接受微信好友发来聊天消息 * @author wechatno:tangjinjinwx * @pa ...
- insert时调用本身字段_python3调用企业微信api!开发一款属于自己的企业微信
python3调用企业微信api 最后更新时间:2020/5/11 前段时间,我将企业微信官方提供的python接口代码的部分功能修改成了python3的,并且自己也使用并测试过部分功能: 因为并没有 ...
- python3调用企业微信api!开发一款属于自己的企业微信
python3调用企业微信api 最后更新时间:2020/5/11 前段时间,我将企业微信官方提供的python接口代码的部分功能修改成了python3的,并且自己也使用并测试过部分功能: 因为并没有 ...
- delphi 企业微信消息机器人_nodeJS实现企业微信机器人每天定时发消息实例 定时任务...
nodeJS实现企业微信机器人每天定时发消息实例 背景 由于企业微信办公需要,"每天定时推送某消息用来提醒群里面所有人或者部分人",于是决定用企业微信自带的机器人来实现此功能,来代 ...
- python实现微信自动发信息_Python实现企业微信机器人每天定时发消息实例
1.背景 由于办公需要"每天定时推送某消息用来提醒群里面所有人",于是决定用企业微信自带的机器人来实现此功能.具体方法我来一一讲述. 2.企业微信API 3.想法 想到几种方式: ...
- Node.JS调用企业微信API:生成渠道二维码
目标: 通过API设置成员的"联系我"方式生成渠道标识二维码,实现标记客户扫码渠道,方便个渠道客户信息统计等. 实现环境: 白码低代码平台(该平台提供企业微信API,可直接调用,减 ...
- 企业微信机器人脚本python_Python开发 之 企业微信机器人天天定时发消息实例
文章目录 一.背景 二.企业微信API 三.想法 四.效果 五.源代码 六.Github源码分享 七.具体步骤 7.一.建立一个群 7.二.建立好后,添加一个群机器人 7.三.给机器人起名字.添加头像 ...
最新文章
- C# 单元测试简单入门
- 【学术相关】新一轮“双一流”名单公布!这些学校上榜
- 安卓创建快捷方式相关问题 Intent Intent-filter
- 腾讯地图拾取坐标html,腾讯地图Api 实现拾取坐标功能示例
- 视频会议十大开源编解码项目排行
- Java验收项目清单_软件验收管理工作内容
- multisim的汉化
- VS2013安装SVN插件
- 联想Win10安装Ubuntu双系统教程
- EasyExcel 背景颜色枚举
- 风口的猪-中国牛市(动态规划)
- iOS开发-Tom猫
- 丰巢互动媒体的新玩法,智能柜焕新“皮肤”了解一下
- 常见软件设计原则总结
- Python 环境搭建
- vue使用高德地图api,点击地图标记,弹出弹窗,使用animate让弹窗有动画的加载
- 后代选择器与子元素选择器
- 记录一次华为CE6800和华三S6800交换机BFD对接配置
- 嵌入式软件笔试(转载)
- 海尔共享空调入驻200余所高校共建智慧校园
热门文章
- creo减速器建模实例,减速箱proE整体及零件图
- linux中vi后如何退出命令,linux用VI编辑后保存退出命令是什么啊?
- Android hardware简易流程
- excel使用教程_Office办公软件(word、ppt、excel)视频教程(更新)
- mx350显卡天梯图_2020年显卡天梯图(2020.04月更新)
- 创新案例分享 | 一体化政务服务平台运维项目,全力提升平台服务效能
- C# 把文件和文件夹 放到回收站 (出现Unknown err (0x402) 无法删除 文件:无法读取源文件或磁盘 解决)
- 无法删除文件 无法读取源文件或磁盘_U盘损坏“无法读取文件”不要慌,教你一招马上回血复活...
- java如何去掉文件后缀名_JAVA 递归批量更改文件后缀名 删除后缀
- 我的世界如何安装java环境变量_JDK安装与环境变量配置方法