赞
踩
注意注意!
微信群必须是内部群 外部群添加不了群机器人
第一步:添加群机器人
第二步: 获取到机器人的key值
第三步:创建springboot项目 并导入对应的依赖
- <dependency>
- <groupId>com.dtflys.forest</groupId>
- <artifactId>forest-spring-boot-starter</artifactId>
- <version>1.5.14</version>
- </dependency>
第四步:配置yml文件
## 轻量级HTTP客户端框架forest forest: # 配置底层API为 okhttp3 backend: okhttp3 # 连接池最大连接数,默认值为500 max-connections: 1000 # 每个路由的最大连接数,默认值为500 max-route-connections: 500 # 请求超时时间,单位为毫秒, 默认值为3000 timeout: 3000 # 连接超时时间,单位为毫秒, 默认值为2000 connect-timeout: 3000 # 请求失败后重试次数,默认为0次不重试 retry-count: 1 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3 ssl-protocol: SSLv3 # 打开或关闭日志,默认为true logEnabled: true # 打开/关闭Forest请求日志(默认为 true) log-request: true # 打开/关闭Forest响应状态日志(默认为 true) log-response-status: true # 打开/关闭Forest响应内容日志(默认为 false) log-response-content: true wechat: notice: key: 这里复制机器人的key值 server: port: 8571 #后端的ip端口
第五步 配置url的地址 并发送消息
- @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);
-
-
- }
第六步:编写controller(发送请求)
controller
- @RestController
- public class RebotController {
-
- @Autowired
- MyNoticeUtil myNoticeUtil;
-
- @GetMapping("/doTest/{testType}")
- public String doTest(@PathVariable("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";
- }
-
- }
第七步:发送具体的消息
- @Component
- public class MyNoticeUtil {
- @Autowired
- private WechatNoticeClient wechatNoticeClient;
- @Value("${wechat.notice.key}")
- private String NOTICE_KEY;
-
- /**
- * 发送文本消息
- */
- public void sendTextMsg() {
- Map<String, Object> sendMap = new HashMap<>();
- //设置消息类型 txt文本
- sendMap.put("msgtype", "text");
- Map<String, String> contentMap = new HashMap<>();
- contentMap.put("content", "你好,lupeng的机器人");
- sendMap.put("text", contentMap);
- wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap);
- }
-
- /**
- * 发送markdown文本消息
- */
- public void sendMarkDownTextMsg() {
- Map<String, Object> sendMap = new HashMap<>();
- //设置消息类型 markdown文本
- sendMap.put("msgtype", "markdown");
- Map<String, String> contentMap = new HashMap<>();
- contentMap.put("content", "支付宝到账<font color=\\\"warning\\\">100元</font>,开心起来吧。\\\n" +
- " >付款方:<font color=\\\"comment\\\">空气</font>");
- sendMap.put("markdown", contentMap);
- wechatNoticeClient.sendWechatMsg(NOTICE_KEY, sendMap);
- }
-
- /**
- * 发送图片消息
- */
- public void sendImageMsg() {
- String url = "D:\\javaWeb\\image\\image-20231006212440733.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);
- }
-
- /**
- * 发送图文消息
- */
- 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", "lupeng的博客");
- obj.put("description", "大家给他点点赞!点击图片立即跳转");
- obj.put("url", "https://blog.csdn.net/qq_44910673?type=blog");
- obj.put("picurl", "https://pic.iqshw.com/d/file/wzgdq/tx/2018/05/06/efa1b8c542784d05804c21bb9ec5f77e.jpg");
- 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;
- // 读取图片字节数组
- try {
- in = new FileInputStream(imgFile);
- data = new byte[in.available()];
- in.read(data);
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 对字节数组Base64编码
- BASE64Encoder encoder = new BASE64Encoder();
- // 返回Base64编码过的字节数组字符串
- 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;
- }
-
-
- }
最后运行!
消息效果:
发送md效果
发送卡片效果
......
可以根据自己的需求进行更改
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。