Skip to content

Commit fd8e02a

Browse files
authored
🎨 #2476【公众号】客服消息增加草稿箱图文消息类型
1 parent a647fe8 commit fd8e02a

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public static class KefuMsgType {
126126
* 模板卡片消息.
127127
*/
128128
public static final String TEMPLATE_CARD = "template_card";
129+
130+
/**
131+
* 发送图文消息(点击跳转到图文消息页面)使用通过 “发布” 系列接口得到的 article_id(草稿箱功能上线后不再支持客服接口中带 media_id 的 mpnews 类型的图文消息)
132+
*/
133+
public static final String MP_NEWS_ARTICLE = "mpnewsarticle";
129134
}
130135

131136
/**

Diff for: weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessage.java

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class WxMpKefuMessage implements Serializable {
3737
private String headContent;
3838
private String tailContent;
3939
private List<WxArticle> articles = new ArrayList<>();
40+
private String mpNewsArticleId;
4041

4142
/**
4243
* 菜单消息里的菜单内容.
@@ -113,6 +114,13 @@ public static MiniProgramPageBuilder MINIPROGRAMPAGE() {
113114
return new MiniProgramPageBuilder();
114115
}
115116

117+
/**
118+
* 发送图文消息(点击跳转到图文消息页面)使用通过 “发布” 系列接口得到的 article_id(草稿箱功能上线后不再支持客服接口中带 media_id 的 mpnews 类型的图文消息)
119+
*/
120+
public static MpNewsArticleBuilder MPNEWSARTICLE() {
121+
return new MpNewsArticleBuilder();
122+
}
123+
116124
/**
117125
* <pre>
118126
* 请使用
@@ -127,6 +135,7 @@ public static MiniProgramPageBuilder MINIPROGRAMPAGE() {
127135
* {@link WxConsts.KefuMsgType#MINIPROGRAMPAGE}
128136
* {@link WxConsts.KefuMsgType#TASKCARD}
129137
* {@link WxConsts.KefuMsgType#MSGMENU}
138+
* {@link WxConsts.KefuMsgType#MP_NEWS_ARTICLE}
130139
* </pre>
131140
*/
132141
public void setMsgType(String msgType) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.chanjar.weixin.mp.builder.kefu;
2+
3+
import me.chanjar.weixin.common.api.WxConsts;
4+
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
5+
6+
/**
7+
* 图文消息builder
8+
* <pre>
9+
* 用法:
10+
* WxMpKefuMessage m = WxMpKefuMessage.MPNEWSARTICLE().articleId("xxxxx").toUser(...).build();
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/leejuncheng">JCLee</a>
14+
*/
15+
public final class MpNewsArticleBuilder extends BaseBuilder<MpNewsArticleBuilder>{
16+
private String articleId;
17+
18+
public MpNewsArticleBuilder() {
19+
this.msgType = WxConsts.KefuMsgType.MP_NEWS_ARTICLE;
20+
}
21+
22+
public MpNewsArticleBuilder articleId(String articleId) {
23+
this.articleId = articleId;
24+
return this;
25+
}
26+
27+
@Override
28+
public WxMpKefuMessage build() {
29+
WxMpKefuMessage m = super.build();
30+
m.setMpNewsArticleId(this.articleId);
31+
return m;
32+
}
33+
}

Diff for: weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpKefuMessageGsonAdapter.java

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public JsonElement serialize(WxMpKefuMessage message, Type typeOfSrc, JsonSerial
9696
messageJson.add("msgmenu", msgmenuJsonObject);
9797
break;
9898
}
99+
case KefuMsgType.MP_NEWS_ARTICLE:
100+
JsonObject mpNewsArticleJson = new JsonObject();
101+
mpNewsArticleJson.addProperty("article_id", message.getMpNewsArticleId());
102+
messageJson.add("mpnewsarticle", mpNewsArticleJson);
103+
break;
99104
default: {
100105
throw new WxRuntimeException("非法消息类型,暂不支持");
101106
}

Diff for: weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessageTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,13 @@ public void testMsgMenuBuild() {
166166
"{\"touser\":\"OPENID\",\"msgtype\":\"msgmenu\",\"msgmenu\":{\"head_content\":\"head_content\",\"list\":[{\"id\":\"101\",\"content\":\"msgmenu1\"},{\"id\":\"102\",\"content\":\"msgmenu2\"}],\"tail_content\":\"tail_content\"}}");
167167
}
168168

169+
public void testMpNewsArticleBuilder() {
170+
WxMpKefuMessage reply = WxMpKefuMessage.MPNEWSARTICLE()
171+
.toUser("OPENID")
172+
.articleId("ARTICLE_ID")
173+
.build();
174+
Assert.assertEquals(reply.toJson(),
175+
"{\"touser\":\"OPENID\",\"msgtype\":\"mpnewsarticle\",\"mpnewsarticle\":{\"article_id\":\"ARTICLE_ID\"}}");
176+
}
177+
169178
}

0 commit comments

Comments
 (0)