Skip to content

Commit 10f7123

Browse files
authored
🆕 #3725【企业微信】 增加markdown_v2的消息类型支持
1 parent 9aa2781 commit 10f7123

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ public interface WxCpGroupRobotService {
7070
*/
7171
void sendMarkdown(String webhookUrl, String content) throws WxErrorException;
7272

73+
/**
74+
* 发送markdown_v2类型的消息
75+
*
76+
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码
77+
* @throws WxErrorException 异常
78+
*/
79+
void sendMarkdownV2(String content) throws WxErrorException;
80+
81+
/**
82+
* 发送markdown_v2类型的消息
83+
*
84+
* @param webhookUrl webhook地址
85+
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码
86+
* @throws WxErrorException 异常
87+
*/
88+
void sendMarkdownV2(String webhookUrl, String content) throws WxErrorException;
89+
7390
/**
7491
* 发送image类型的消息
7592
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void sendMarkdown(String content) throws WxErrorException {
4242
this.sendMarkdown(this.getWebhookUrl(), content);
4343
}
4444

45+
@Override
46+
public void sendMarkdownV2(String content) throws WxErrorException {
47+
this.sendMarkdownV2(this.getWebhookUrl(), content);
48+
}
49+
4550
@Override
4651
public void sendImage(String base64, String md5) throws WxErrorException {
4752
this.sendImage(this.getWebhookUrl(), base64, md5);
@@ -70,6 +75,14 @@ public void sendMarkdown(String webhookUrl, String content) throws WxErrorExcept
7075
.toJson());
7176
}
7277

78+
@Override
79+
public void sendMarkdownV2(String webhookUrl, String content) throws WxErrorException {
80+
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
81+
.setMsgType(GroupRobotMsgType.MARKDOWN_V2)
82+
.setContent(content)
83+
.toJson());
84+
}
85+
7386
@Override
7487
public void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException {
7588
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ public String toJson() {
252252
messageJson.add("markdown", text);
253253
break;
254254
}
255+
case MARKDOWN_V2: {
256+
JsonObject text = new JsonObject();
257+
text.addProperty("content", this.getContent());
258+
messageJson.add("markdown_v2", text);
259+
break;
260+
}
255261
case IMAGE: {
256262
JsonObject text = new JsonObject();
257263
text.addProperty("base64", this.getBase64());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ public static class GroupRobotMsgType {
630630
*/
631631
public static final String MARKDOWN = "markdown";
632632

633+
/**
634+
* markdown_v2消息.
635+
*/
636+
public static final String MARKDOWN_V2 = "markdown_v2";
637+
633638
/**
634639
* 图文消息(点击跳转到外链).
635640
*/

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,51 @@ public void testSendMarkDown() throws WxErrorException {
6464
robotService.sendMarkdown(content);
6565
}
6666

67+
/**
68+
* Test send mark down v2.
69+
*
70+
* @throws WxErrorException the wx error exception
71+
*/
72+
@Test
73+
public void testSendMarkDownV2() throws WxErrorException {
74+
String content = "# 一、标题\n" +
75+
"## 二级标题\n" +
76+
"### 三级标题\n" +
77+
"# 二、字体\n" +
78+
"*斜体*\n" +
79+
"\n" +
80+
"**加粗**\n" +
81+
"# 三、列表 \n" +
82+
"- 无序列表 1 \n" +
83+
"- 无序列表 2\n" +
84+
" - 无序列表 2.1\n" +
85+
" - 无序列表 2.2\n" +
86+
"1. 有序列表 1\n" +
87+
"2. 有序列表 2\n" +
88+
"# 四、引用\n" +
89+
"> 一级引用\n" +
90+
">>二级引用\n" +
91+
">>>三级引用\n" +
92+
"# 五、链接\n" +
93+
"[这是一个链接](https://work.weixin.qq.com/api/doc)\n" +
94+
"![](https://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png)\n" +
95+
"# 六、分割线\n" +
96+
"\n" +
97+
"---\n" +
98+
"# 七、代码\n" +
99+
"`这是行内代码`\n" +
100+
"```\n" +
101+
"这是独立代码块\n" +
102+
"```\n" +
103+
"\n" +
104+
"# 八、表格\n" +
105+
"| 姓名 | 文化衫尺寸 | 收货地址 |\n" +
106+
"| :----- | :----: | -------: |\n" +
107+
"| 张三 | S | 广州 |\n" +
108+
"| 李四 | L | 深圳 |";
109+
robotService.sendMarkdownV2(content);
110+
}
111+
67112
/**
68113
* Test send image.
69114
*

0 commit comments

Comments
 (0)