当前位置:   article > 正文

java发送企业微信群_java给企业微信发送群消息

java给企业微信发送群消息

一、修改pom.xml

<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version>3.14.9</version>
</dependency>
<dependency>
   <groupId>commons-codec</groupId>
   <artifactId>commons-codec</artifactId>
   <version>1.10</version>
</dependency>

 二、java代码

  1. package com.sdmktech.mep.energy.monitor.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.common.collect.Lists;
  4. import lombok.extern.slf4j.Slf4j;
  5. import okhttp3.*;
  6. import java.io.IOException;
  7. import java.net.InetSocketAddress;
  8. import java.net.Proxy;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.concurrent.TimeUnit;
  12. @Slf4j
  13. public class WeComPushUtil {
  14. private static final String SECRET = "96b1f94b-eadc-441c-b8cb-87a93d7a4904hlw";
  15. /**
  16. * 配置企业微信的webhook
  17. * https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=96b1f94b-eadc-441c-b8cb-87a93d7a4904hlw
  18. */
  19. private static final String WEBHOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
  20. private static final String dingTemplate = "**告警名称** : %s\n\n**告警级别** : %s\n\n**设备信息** : %s\n\n" +
  21. "**告警内容** : %s\n\n**告警时间** : %s\n\n";
  22. private static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23. public static void main(String[] args) {
  24. sendGroup(SECRET,"火灾","S2","铁塔","大火熊熊燃气啊");
  25. }
  26. /**
  27. *
  28. * @param token 企业微信机器人token
  29. * @param warningName 告警名称
  30. * @param level 告警级别
  31. * @param deviceInfo 设备信息
  32. * @param content 告警内容
  33. */
  34. public static void sendGroup(String token,String warningName,String level,String deviceInfo,String content){
  35. try {
  36. JSONObject reqBody = getReqBody(String.format(dingTemplate, warningName, level, deviceInfo, content, format1.format(new Date())));
  37. callWeChatBot(reqBody.toString(),token);
  38. }catch (Exception e){
  39. }
  40. }
  41. /**
  42. * 发送MarKDown消息
  43. *
  44. * @param msg 需要发送的消息
  45. * @return
  46. * @throws Exception
  47. */
  48. public static JSONObject getReqBody(String msg) throws Exception {
  49. JSONObject markdown = new JSONObject();
  50. markdown.put("content", msg);
  51. JSONObject reqBody = new JSONObject();
  52. reqBody.put("msgtype", "markdown");
  53. reqBody.put("markdown", markdown);
  54. reqBody.put("safe", 0);
  55. return reqBody;
  56. }
  57. /**
  58. * 调用群机器人
  59. *
  60. * @param reqBody 接口请求参数
  61. * @throws Exception 可能有IO异常
  62. */
  63. public static String callWeChatBot(String reqBody,String botUrl) throws Exception {
  64. log.info("请求参数:" + reqBody);
  65. // 构造RequestBody对象,用来携带要提交的数据;需要指定MediaType,用于描述请求/响应 body 的内容类型
  66. MediaType contentType = MediaType.parse("application/json; charset=utf-8");
  67. RequestBody body = RequestBody.create(contentType, reqBody);
  68. // 调用群机器人
  69. String respMsg = okHttp(body, WEBHOOK+botUrl);
  70. if ("0".equals(respMsg.substring(11, 12))) {
  71. log.info("向群发送消息成功!");
  72. } else {
  73. log.info("请求失败!");
  74. // 发送错误信息到群
  75. sendTextMsg("群机器人推送消息失败,错误信息:\n" + respMsg);
  76. }
  77. return respMsg;
  78. }
  79. /**
  80. * @param body 携带需要提交的数据
  81. * @param url 请求地址
  82. * @return
  83. * @throws Exception
  84. */
  85. public static String okHttp(RequestBody body, String url) throws Exception {
  86. // 构造和配置OkHttpClient
  87. OkHttpClient client;
  88. client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS) // 设置连接超时时间
  89. .readTimeout(20, TimeUnit.SECONDS) // 设置读取超时时间
  90. .build();
  91. // 构造Request对象
  92. Request request = new Request.Builder().url(url).post(body).addHeader("cache-control", "no-cache") // 响应消息不缓存
  93. .build();
  94. // 构建Call对象,通过Call对象的execute()方法提交异步请求
  95. Response response = null;
  96. try {
  97. response = client.newCall(request).execute();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. // 请求结果处理
  102. byte[] datas = response.body().bytes();
  103. String respMsg = new String(datas);
  104. log.info("返回结果:" + respMsg);
  105. return respMsg;
  106. }
  107. /**
  108. * 发送文字消息
  109. *
  110. * @param msg 需要发送的消息
  111. * @return
  112. * @throws Exception
  113. */
  114. public static String sendTextMsg(String msg) throws Exception {
  115. JSONObject text = new JSONObject();
  116. text.put("content", msg);
  117. JSONObject reqBody = new JSONObject();
  118. reqBody.put("msgtype", "text");
  119. reqBody.put("text", text);
  120. reqBody.put("safe", 0);
  121. return callWeChatBot(reqBody.toString(),SECRET);
  122. }
  123. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/178495
推荐阅读
相关标签
  

闽ICP备14008679号