引言
原来系统中报警采用短信和邮件方式,短信采用阿里的短信,近期由于 一些原因,项目中想将报警信息发送到 企业微信中,由于之前小编玩zabbix的时候实现过这个需求,所以认为通过java应该比较简单, 下面分享接入代码,直接上干货。
一、创建应用
1)、登录企业微信后台管理页面,选择应用管理
2)、在最下面 创建应用
这里面可以控制那些人收到报警信息,其中质量的Secret 就是我们后面代码中的秘钥;
3)获得企业id
在 管理后台-》我的企业 一栏的最下面可以找到企业ID
二、代码实现
添看到依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency>
代码实现
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<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() { }.getType()); return map.get("access_token").toString(); } /** * @param touser 发送消息接收者 ,msgtype消息类型(文本/图片等), * @param application_id 应用编号。 * @return String * @Title:创建微信发送请求post数据 */ public static String createpostdata(String touser, String msgtype, int application_id, String contentKey, String contentValue) { IacsWeChatDataVo wcd = new IacsWeChatDataVo(); wcd.setTouser(touser); wcd.setAgentid(application_id); wcd.setMsgtype(msgtype); Map<Object, Object> content = new HashMap<Object, Object>(); content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date())); wcd.setText(content); return gson.toJson(wcd); } /** * @param charset 消息编码 ,contentType 消息体内容类型, * @param url 微信消息发送请求地址,data为post数据,token鉴权token * @return String * @Title 创建微信发送请求post实体 */ public static 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(SendWeChatUtils.class).info( "call [{}], param:{}, resp:{}", url, data, resp); return resp; } public static void sendMsg(String strContent){ try { String token = getToken("企业id", "秘钥"); String postdata = createpostdata("@all", "text", 1000011, "content",strContent); String resp = post("utf-8", "text", (new IacsUrlDataVo()).getSendMessage_Url(), postdata, token); // System.out.println("获取到的token======>" + token); // System.out.println("请求数据======>" + postdata); // System.out.println("发送微信的响应数据======>" + resp); } catch (Exception e) { e.printStackTrace(); } } }
上面代码中我们需要填写我们自己的企业id和秘钥;
3、业务调用