Skip to content

Commit 568706f

Browse files
committed
#921 企业微信发送应用消息接口支持Markdown消息格式
1 parent 3225311 commit 568706f

File tree

12 files changed

+108
-18
lines changed

12 files changed

+108
-18
lines changed

weixin-java-common/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212

1313
<artifactId>weixin-java-common</artifactId>
14-
<name>WxJava - Common</name>
14+
<name>WxJava - Common Java SDK</name>
1515
<description>微信开发Java SDK公共模块</description>
1616

1717
<dependencies>

weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public static class KefuMsgType {
6262
* 图文消息(点击跳转到图文消息页面).
6363
*/
6464
public static final String MPNEWS = "mpnews";
65+
/**
66+
* markdown消息.
67+
* (目前仅支持markdown语法的子集,微工作台(原企业号)不支持展示markdown消息)
68+
*/
69+
public static final String MARKDOWN = "markdown";
6570
/**
6671
* 发送文件(CP专用).
6772
*/

weixin-java-cp/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212

1313
<artifactId>weixin-java-cp</artifactId>
14-
<name>WxJava - CP</name>
14+
<name>WxJava - CP Java SDK</name>
1515
<description>微信企业号/企业微信 Java SDK</description>
1616

1717
<dependencies>

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.google.gson.JsonElement;
1111
import com.google.gson.JsonObject;
1212
import com.google.gson.JsonParser;
13-
1413
import me.chanjar.weixin.common.bean.WxJsapiSignature;
1514
import me.chanjar.weixin.common.error.WxError;
1615
import me.chanjar.weixin.common.error.WxErrorException;
@@ -141,9 +140,10 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
141140
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
142141
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
143142
Integer agentId = message.getAgentId();
144-
if(null == agentId){
143+
if (null == agentId) {
145144
message.setAgentId(this.getWxCpConfigStorage().getAgentId());
146145
}
146+
147147
return WxCpMessageSendResult.fromJson(this.post(url, message.toJson()));
148148
}
149149

@@ -171,7 +171,7 @@ public String post(String url, String postData) throws WxErrorException {
171171
}
172172

173173
/**
174-
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
174+
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
175175
*/
176176
@Override
177177
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import me.chanjar.weixin.cp.bean.article.NewArticle;
1111
import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
1212
import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder;
13+
import me.chanjar.weixin.cp.bean.messagebuilder.MarkdownMsgBuilder;
1314
import me.chanjar.weixin.cp.bean.messagebuilder.MpnewsBuilder;
1415
import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder;
1516
import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
@@ -94,6 +95,13 @@ public static MpnewsBuilder MPNEWS() {
9495
return new MpnewsBuilder();
9596
}
9697

98+
/**
99+
* 获得markdown消息builder.
100+
*/
101+
public static MarkdownMsgBuilder MARKDOWN() {
102+
return new MarkdownMsgBuilder();
103+
}
104+
97105
/**
98106
* 获得文件消息builder.
99107
*/
@@ -112,6 +120,7 @@ public static FileBuilder FILE() {
112120
* {@link WxConsts.KefuMsgType#VIDEO}
113121
* {@link WxConsts.KefuMsgType#NEWS}
114122
* {@link WxConsts.KefuMsgType#MPNEWS}
123+
* {@link WxConsts.KefuMsgType#MARKDOWN}
115124
* </pre>
116125
*
117126
* @param msgType 消息类型
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.chanjar.weixin.cp.bean.messagebuilder;
2+
3+
import me.chanjar.weixin.common.api.WxConsts;
4+
import me.chanjar.weixin.cp.bean.WxCpMessage;
5+
6+
/**
7+
* <pre>
8+
* markdown类型的消息builder
9+
* Created by Binary Wang on 2019/1/20.
10+
* </pre>
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
*/
14+
public class MarkdownMsgBuilder extends BaseBuilder<MarkdownMsgBuilder> {
15+
private String content;
16+
17+
public MarkdownMsgBuilder() {
18+
this.msgType = WxConsts.KefuMsgType.MARKDOWN;
19+
}
20+
21+
public MarkdownMsgBuilder content(String content) {
22+
this.content = content;
23+
return this;
24+
}
25+
26+
@Override
27+
public WxCpMessage build() {
28+
WxCpMessage m = super.build();
29+
m.setContent(this.content);
30+
return m;
31+
}
32+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
*/
99
package me.chanjar.weixin.cp.util.json;
1010

11-
import com.google.gson.*;
11+
import java.lang.reflect.Type;
12+
13+
import org.apache.commons.lang3.StringUtils;
14+
15+
import com.google.gson.JsonArray;
16+
import com.google.gson.JsonElement;
17+
import com.google.gson.JsonObject;
18+
import com.google.gson.JsonSerializationContext;
19+
import com.google.gson.JsonSerializer;
1220
import me.chanjar.weixin.common.api.WxConsts;
1321
import me.chanjar.weixin.cp.bean.WxCpMessage;
1422
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
1523
import me.chanjar.weixin.cp.bean.article.NewArticle;
16-
import org.apache.commons.lang3.StringUtils;
17-
18-
import java.lang.reflect.Type;
1924

2025
/**
2126
* @author Daniel Qian
@@ -37,12 +42,19 @@ public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializat
3742
if (StringUtils.isNotBlank(message.getToTag())) {
3843
messageJson.addProperty("totag", message.getToTag());
3944
}
45+
4046
if (WxConsts.KefuMsgType.TEXT.equals(message.getMsgType())) {
4147
JsonObject text = new JsonObject();
4248
text.addProperty("content", message.getContent());
4349
messageJson.add("text", text);
4450
}
4551

52+
if (WxConsts.KefuMsgType.MARKDOWN.equals(message.getMsgType())) {
53+
JsonObject text = new JsonObject();
54+
text.addProperty("content", message.getContent());
55+
messageJson.add("markdown", text);
56+
}
57+
4658
if (WxConsts.KefuMsgType.TEXTCARD.equals(message.getMsgType())) {
4759
JsonObject text = new JsonObject();
4860
text.addProperty("title", message.getTitle());

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageAPITest.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import org.testng.annotations.*;
4+
35
import com.google.inject.Inject;
46
import me.chanjar.weixin.common.api.WxConsts;
57
import me.chanjar.weixin.common.error.WxErrorException;
68
import me.chanjar.weixin.cp.bean.WxCpMessage;
79
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
8-
import org.testng.annotations.*;
910

1011
import static org.testng.Assert.*;
1112

@@ -14,7 +15,7 @@
1415
* @author Daniel Qian
1516
*
1617
*/
17-
@Test(groups = "customMessageAPI")
18+
@Test
1819
@Guice(modules = ApiTestModule.class)
1920
public class WxCpMessageAPITest {
2021

@@ -59,4 +60,32 @@ public void testSendMessage1() throws WxErrorException {
5960
System.out.println(messageSendResult.getInvalidUserList());
6061
System.out.println(messageSendResult.getInvalidTagList());
6162
}
63+
64+
@Test
65+
public void testSendMessage_markdown() throws WxErrorException {
66+
WxCpMessage message = WxCpMessage
67+
.MARKDOWN()
68+
.toUser(configStorage.getUserId())
69+
.content("您的会议室已经预定,稍后会同步到`邮箱` \n" +
70+
" >**事项详情** \n" +
71+
" >事 项:<font color=\\\"info\\\">开会</font> \n" +
72+
" >组织者:@miglioguan \n" +
73+
" >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
74+
" > \n" +
75+
" >会议室:<font color=\\\"info\\\">广州TIT 1楼 301</font> \n" +
76+
" >日 期:<font color=\\\"warning\\\">2018年5月18日</font> \n" +
77+
" >时 间:<font color=\\\"comment\\\">上午9:00-11:00</font> \n" +
78+
" > \n" +
79+
" >请准时参加会议。 \n" +
80+
" > \n" +
81+
" >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)")
82+
.build();
83+
84+
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
85+
assertNotNull(messageSendResult);
86+
System.out.println(messageSendResult);
87+
System.out.println(messageSendResult.getInvalidPartyList());
88+
System.out.println(messageSendResult.getInvalidUserList());
89+
System.out.println(messageSendResult.getInvalidTagList());
90+
}
6291
}

weixin-java-miniapp/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
<artifactId>wx-java</artifactId>
1010
<version>3.3.2.B</version>
1111
</parent>
12+
1213
<artifactId>weixin-java-miniapp</artifactId>
13-
<name>WxJava - MiniApp</name>
14-
<description>微信小程序Java SDK</description>
14+
<name>WxJava - MiniApp Java SDK</name>
15+
<description>微信小程序 Java SDK</description>
1516

1617
<dependencies>
1718
<dependency>

weixin-java-mp/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<artifactId>wx-java</artifactId>
1010
<version>3.3.2.B</version>
1111
</parent>
12+
1213
<artifactId>weixin-java-mp</artifactId>
13-
<name>WxJava - MP</name>
14+
<name>WxJava - MP Java SDK</name>
1415
<description>微信公众号Java SDK</description>
1516

1617
<dependencies>

weixin-java-open/pom.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
55
xmlns="http://maven.apache.org/POM/4.0.0">
6-
76
<modelVersion>4.0.0</modelVersion>
87
<parent>
98
<groupId>com.github.binarywang</groupId>
109
<artifactId>wx-java</artifactId>
1110
<version>3.3.2.B</version>
1211
</parent>
12+
1313
<artifactId>weixin-java-open</artifactId>
14-
<name>WxJava - Open</name>
15-
<description>微信开放平台Java SDK</description>
14+
<name>WxJava - Open Java SDK</name>
15+
<description>微信开放平台 Java SDK</description>
16+
1617
<developers>
1718
<developer>
1819
<name>007</name>

weixin-java-pay/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>weixin-java-pay</artifactId>
13-
<name>WxJava - PAY</name>
13+
<name>WxJava - PAY Java SDK</name>
1414
<description>微信支付 Java SDK</description>
1515

1616
<dependencies>

0 commit comments

Comments
 (0)