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調用工具的配置,以及 機器人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: 85714. 創(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
用于封裝不同消息類型消息的推送方法,里面調用的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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Spring?Boot+Vue實現(xiàn)Socket通知推送的完整步驟
- spring中websocket定時任務實現(xiàn)實時推送
- Springboot集成SSE實現(xiàn)單工通信消息推送流程詳解
- SpringBoot整合WebSocket實現(xiàn)后端向前端主動推送消息方式
- Spring?Boot?使用?SSE?方式向前端推送數(shù)據(jù)詳解
- SpringBoot+WebSocket實現(xiàn)消息推送功能
- SpringBoot整合WxJava開啟消息推送的實現(xiàn)
- SpringBoot2.0集成WebSocket實現(xiàn)后臺向前端推送信息
- SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼
- Spring SseEmitter推送消息及常用方法
相關文章
springboot的實體類字段校驗的分組校驗具體實現(xiàn)步驟
分組校驗允許在不同場景下對同一實體類應用不同的校驗規(guī)則,通過定義分組接口、在實體類和Controller中指定分組,以及全局異常處理,可以靈活控制校驗規(guī)則,本文介紹springboot的實體類字段校驗的分組校驗,感興趣的朋友一起看看吧2025-03-03
Java模擬HTTP Get Post請求實現(xiàn)論壇自動回帖功能
這篇文章主要介紹了Java模擬HTTP Get Post請求實現(xiàn)論壇自動回帖功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

