引言
原来系统中报警采用短信和邮件方式,短信采用阿里的短信,近期由于 一些原因,项目中想将报警信息发送到 企业微信中,由于之前小编玩zabbix的时候实现过这个需求,所以认为通过java应该比较简单, 下面分享接入代码,直接上干货。
一、创建应用
1)、登录企业微信后台管理页面,选择应用管理
2)、在最下面 创建应用
这里面可以控制那些人收到报警信息,其中质量的Secret 就是我们后面代码中的秘钥;
3)获得企业id
在 管理后台-》我的企业 一栏的最下面可以找到企业ID
二、代码实现
添看到依赖
org.apache.httpcomponents
httpcore
4.4.6
org.apache.httpcomponents
httpclient
4.5.5
代码实现
1、实体类
package com.jack.common.utils;
/**
* @Description:
* @author: zhenghao
* @date: 2020/3/5 17:24
*/
public class IacsUrlDataVo {
String corpid;
String corpsecret;
String Get_Token_Url;
String SendMessage_Url;
public String getCorpid() {
return corpid;
}
public void setCorpid(String corpid) {
this.corpid = corpid;
}
public String getCorpsecret() {
return corpsecret;
}
public void setCorpsecret(String corpsecret) {
this.corpsecret = corpsecret;
}
public void setGet_Token_Url(String corpid,String corpsecret) {
this.Get_Token_Url ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;
}
public String getGet_Token_Url() {
return Get_Token_Url;
}
public String getSendMessage_Url(){
SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
return SendMessage_Url;
}
}
package com.jack.common.utils;
/**
* @Description:
* @author: zhenghao
* @date: 2020/3/5 17:24
*/
public class IacsWeChatDataVo {
String touser;
String msgtype;
int agentid;
Object text;//实际接收Map类型数据
public Object getText() {
return text;
}
public void setText(Object text) {
this.text = text;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public int getAgentid() {
return agentid;
}
public void setAgentid(int agentid) {
this.agentid = agentid;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
}
2、消息发送代码
package com.jack.common.utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Description: 企业微信发送代码
* @author: zhenghao
* @date: 2020/3/5 17:25
*/
public class SendWeChatUtils {
private CloseableHttpClient httpClient;
private static HttpPost httpPost;//用于提交登陆数据
private static HttpGet httpGet;//用于获得登录后的页面
public static final String CONTENT_TYPE = "Content-Type";
static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Gson gson = new Gson();
/**
* 微信授权请求,GET类型,获取授权响应,用于其他方法截取token
*
* @param Get_Token_Url
* @return String 授权响应内容
* @throws IOException
*/
public 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);
} finally {
response.close();
}
LoggerFactory.getLogger(getClass()).info(" resp:{}", resp);
return resp;
}
/**
* 获取toAuth(String Get_Token_Url)返回结果中键值对中access_token键的值
*
* @param corpid 应用组织编号 corpsecret 应用秘钥
*/
public static String getToken(String corpid, String corpsecret) throws IOException {
SendWeChatUtils sw = new SendWeChatUtils();
IacsUrlDataVo uData = new IacsUrlDataVo();
uData.setGet_Token_Url(corpid, corpsecret);
String resp = sw.toAuth(uData.getGet_Token_Url());
Map map = gson.fromJson(resp,
new TypeToken
上面代码中我们需要填写我们自己的企业id和秘钥;
3、业务调用
腾讯企业微信官方文档:https://work.weixin.qq.com/api/doc#10167