Skip to content

Commit 5da9fb3

Browse files
authored
🎨 #2541 【企业微信】发送群聊机器人消息接口增加对文件类型的支持
1 parent 19c3113 commit 5da9fb3

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ public interface WxCpGroupRobotService {
8888
* @throws WxErrorException 异常
8989
*/
9090
void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException;
91+
92+
/**
93+
* 发送文件类型的消息
94+
*
95+
* @param webhookUrl webhook地址
96+
* @param mediaId 文件id
97+
* @throws WxErrorException 异常
98+
*/
99+
void sendFile(String webhookUrl, String mediaId) throws WxErrorException;
91100
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.chanjar.weixin.cp.api.impl;
22

33
import lombok.RequiredArgsConstructor;
4-
import me.chanjar.weixin.common.error.WxError;
54
import me.chanjar.weixin.common.error.WxErrorException;
65
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
76
import me.chanjar.weixin.cp.api.WxCpService;
@@ -14,8 +13,6 @@
1413
import java.util.List;
1514

1615
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType;
17-
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.MARKDOWN;
18-
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.TEXT;
1916

2017
/**
2118
* 企业微信群机器人消息发送api 实现
@@ -59,7 +56,7 @@ public void sendNews(List<NewArticle> articleList) throws WxErrorException {
5956
@Override
6057
public void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
6158
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
62-
.setMsgType(TEXT)
59+
.setMsgType(GroupRobotMsgType.TEXT)
6360
.setContent(content)
6461
.setMentionedList(mentionedList)
6562
.setMentionedMobileList(mobileList)
@@ -69,7 +66,7 @@ public void sendText(String webhookUrl, String content, List<String> mentionedLi
6966
@Override
7067
public void sendMarkdown(String webhookUrl, String content) throws WxErrorException {
7168
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
72-
.setMsgType(MARKDOWN)
69+
.setMsgType(GroupRobotMsgType.MARKDOWN)
7370
.setContent(content)
7471
.toJson());
7572
}
@@ -89,4 +86,11 @@ public void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxE
8986
.setArticles(articleList).toJson());
9087
}
9188

89+
@Override
90+
public void sendFile(String webhookUrl, String mediaId) throws WxErrorException {
91+
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
92+
.setMsgType(GroupRobotMsgType.FILE)
93+
.setMediaId(mediaId).toJson());
94+
}
95+
9296
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpGroupRobotMessage.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public class WxCpGroupRobotMessage implements Serializable {
5757
*/
5858
private List<NewArticle> articles;
5959

60+
/**
61+
* 文件id
62+
*/
63+
private String mediaId;
64+
6065
public String toJson() {
6166
JsonObject messageJson = new JsonObject();
6267
messageJson.addProperty("msgtype", this.getMsgType());
@@ -112,6 +117,12 @@ public String toJson() {
112117
messageJson.add("news", text);
113118
break;
114119
}
120+
case FILE: {
121+
JsonObject file = new JsonObject();
122+
file.addProperty("media_id", this.getMediaId());
123+
messageJson.add("file", file);
124+
break;
125+
}
115126
default:
116127

117128
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ public static class GroupRobotMsgType {
374374
* 图文消息(点击跳转到外链).
375375
*/
376376
public static final String NEWS = "news";
377+
378+
/**
379+
* 文件类型消息.
380+
*/
381+
public static final String FILE = "file";
382+
377383
}
378384

379385
/**

0 commit comments

Comments
 (0)