Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn)
前言
這個東西有啥用,好玩?
確實, 好玩歸好玩,其實很有使用場景。
可以自己選則一些業(yè)務節(jié)點觸發(fā)這個機器人助手的消息推送;
簡單舉例:
1. 有人給你的系統(tǒng)留下反饋意見了,推送到運營群去;
2.項目部署成功了,推送到運維群去;
3.有人新增業(yè)務資料了,推送到客服群去;
本篇內(nèi)容:
對接企微機器人,推送消息到群聊。
消息類型有四種:
- 文本消息
- 圖片消息
- MarkDown格式文本消息
- 小卡片消息(小卡片哦~)
效果:
正文
注意點:
1.企業(yè)微信群聊,外部群聊不允許弄機器人。
2.整合機器人的前提是,到相關群聊建機器人。
可以整合多個機器人,每個機器的身份標識是 創(chuàng)建的時候 企微分發(fā)的一個key。
觸發(fā)哪個機器人去推消息,就使用哪個key。
機器人創(chuàng)建步驟:
①對著群聊右鍵,點擊進入管理聊天消息
②點擊添加機器人
3.創(chuàng)建機器人
4.創(chuàng)建成功(這個key就是每個機器人的唯一標識,推送消息可以設計成傳key推送)
開始敲代碼整合:
慣例,先看下這次實例最終目錄結構:
1. 從頭開始 建一個項目:
2.引入核心依賴:
<!-- http請求工具 --> <dependency> <groupId>com.dtflys.forest</groupId> <artifactId>forest-spring-boot-starter</artifactId> <version>1.5.14</version> </dependency>
3. 把配置文件改成yml格式 (個人習慣),然后配上forset調(diào)用工具的配置,以及 機器人key
## 輕量級HTTP客戶端框架forest forest: # 配置底層API為 okhttp3 backend: okhttp3 # 連接池最大連接數(shù),默認值為500 max-connections: 1000 # 每個路由的最大連接數(shù),默認值為500 max-route-connections: 500 # 請求超時時間,單位為毫秒, 默認值為3000 timeout: 3000 # 連接超時時間,單位為毫秒, 默認值為2000 connect-timeout: 3000 # 請求失敗后重試次數(shù),默認為0次不重試 retry-count: 1 # 單向驗證的HTTPS的默認SSL協(xié)議,默認為SSLv3 ssl-protocol: SSLv3 # 打開或關閉日志,默認為true logEnabled: true # 打開/關閉Forest請求日志(默認為 true) log-request: true # 打開/關閉Forest響應狀態(tài)日志(默認為 true) log-response-status: true # 打開/關閉Forest響應內(nèi)容日志(默認為 false) log-response-content: true wechat: notice: key: 3f66977b-****-4af5-****-59*0c4****3d server: port: 8571
4. 創(chuàng)建 WechatNoticeClient.java
用于對接企微機器人推消息接口,因為我們整合了forest ,簡單用注解就行(其實自己用http工具也行)
import com.dtflys.forest.annotation.JSONBody; import com.dtflys.forest.annotation.Post; import com.dtflys.forest.annotation.Var; import org.springframework.stereotype.Component; import java.util.Map; /** * @Author: JCccc * @Date: 2022-5-27 14:44 * @Description: 企業(yè)微信機器人通知client */ @Component public interface WechatNoticeClient { @Post( url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}", headers = { "Accept-Charset: utf-8", "Content-Type: application/json" }, dataType = "json") void sendWechatMsg(@Var("key") String key, @JSONBody Map<String, Object> body); }
5.創(chuàng)建 MyNoticeUtil.java
用于封裝不同消息類型消息的推送方法,里面調(diào)用的WechatNoticeClient.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import sun.misc.BASE64Encoder; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author: JCccc * @Date: 2022-5-27 14:48 * @Description: */ @Component public class MyNoticeUtil { @Autowired private WechatNoticeClient wechatNoticeClient; @Value("${wechat.notice.key}") private String NOTICE_KEY; /** * 發(fā)送文本消息 */ public void sendTextMsg() { Map<String, Object> sendMap = new HashMap<>(); //設置消息類型 txt文本 sendMap.put("msgtype", "text"); Map<String, String> contentMap = new HashMap<>(); contentMap.put("content", "你好,我是JCccc的機器人"); sendMap.put("text", contentMap); wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap); } /** * 發(fā)送markdown文本消息 */ public void sendMarkDownTextMsg() { Map<String, Object> sendMap = new HashMap<>(); //設置消息類型 markdown文本 sendMap.put("msgtype", "markdown"); Map<String, String> contentMap = new HashMap<>(); contentMap.put("content", "JCccc,您的賬戶余額已到賬<font color=\\\"warning\\\">15000元</font>,開心起來吧。\\\n" + " >付款方:<font color=\\\"comment\\\">白日做夢</font>"); sendMap.put("markdown", contentMap); wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap); } /** * 發(fā)送圖片消息 */ public void sendImageMsg() { String url = "D:\\Program Files\\KmProjects\\dotest\\src\\main\\resources\\static\\test.png"; Map<String, Object> sendMap = new HashMap<>(); sendMap.put("msgtype", "image"); Map<String, String> contentMap = new HashMap<>(); contentMap.put("md5", getMd5(url)); contentMap.put("base64", getBase64(url).replaceAll("\r|\n", "")); sendMap.put("image", contentMap); wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap); } /** * 發(fā)送圖文消息 */ public void sendImageAndTxtMsg() { Map<String, Object> sendMap = new HashMap<>(); sendMap.put("msgtype", "news"); Map<String, Object> contentMap = new HashMap<>(); List<Map<String, Object>> list = new ArrayList<>(); Map<String, Object> obj = new HashMap<>(); obj.put("title", "小目標青年的博客"); obj.put("description", "大家給他點點贊!"); obj.put("url", "https://blog.csdn.net/qq_35387940"); obj.put("picurl", "https://img-blog.csdnimg.cn/6bc435ac39514cb780739ea1cc34c409.png"); list.add(obj); contentMap.put("articles", list); sendMap.put("news", contentMap); wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap); } /** * 圖片轉為base64編碼 */ public String getBase64(String imgFile) { InputStream in = null; byte[] data = null; // 讀取圖片字節(jié)數(shù)組 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); // 返回Base64編碼過的字節(jié)數(shù)組字符串 return encoder.encode(data); } /** * 獲取文件的MD5值 * * @param path * @return */ public String getMd5(String path) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(path); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { md5.update(buffer, 0, len); } fis.close(); byte[] byteArray = md5.digest(); StringBuilder sb = new StringBuilder(); for (byte b : byteArray) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
6.簡單寫一個測試接口模擬觸發(fā)一下機器人:
import com.jc.dotest.wechat.MyNoticeUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @Author: JCccc * @Date: 2022-5-27 14:52 * @Description: */ @RestController public class TestController { @Autowired MyNoticeUtil myNoticeUtil; @GetMapping("/doTest") public String doTest(@RequestParam("testType") String testType){ if (testType.equals("1")){ myNoticeUtil.sendTextMsg(); } if (testType.equals("2")){ myNoticeUtil.sendMarkDownTextMsg(); } if (testType.equals("3")){ myNoticeUtil.sendImageMsg(); } if (testType.equals("4")){ myNoticeUtil.sendImageAndTxtMsg(); } return "success"; } }
測試效果:
觸發(fā)發(fā)送文本消息
效果:
其他的效果:
到此這篇關于Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn)的文章就介紹到這了,更多相關Springboot企業(yè)微信機器人推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java創(chuàng)建數(shù)組的3種方式代碼舉例
數(shù)組是相同類型數(shù)據(jù)的有序集合,數(shù)組描述的是若干個相同類型的數(shù)據(jù)按照一定的先后次序排列組合而成,其中每一個數(shù)據(jù)稱為數(shù)組的元素,可以通過下標進行訪問,這篇文章主要給大家介紹了關于Java創(chuàng)建數(shù)組的3種方式,需要的朋友可以參考下2024-01-01Spring整合Quartz定時任務并在集群、分布式系統(tǒng)中的應用
這篇文章主要介紹了Spring整合Quartz定時任務并在集群、分布式系統(tǒng)中的應用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04解決springboot文件配置端口不起作用(默認8080)
這篇文章主要介紹了解決springboot文件配置端口不起作用(默認8080),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08詳解SpringBoot整合RabbitMQ如何實現(xiàn)消息確認
這篇文章主要介紹了SpringBoot整合RabbitMQ是如何實現(xiàn)消息確認的,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05Java如何獲取@TableField,@TableName注解的值
這篇文章主要介紹了Java如何獲取@TableField,@TableName注解的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Springcloud基于OpenFeign實現(xiàn)服務調(diào)用代碼實例
這篇文章主要介紹了Springcloud基于OpenFeign實現(xiàn)服務調(diào)用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08Java8的Lambda遍歷兩個List匹配數(shù)據(jù)方式
這篇文章主要介紹了Java8的Lambda遍歷兩個List匹配數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03