Skip to content

Commit

Permalink
#1075 企业微信支持推送小程序通知消息
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Jun 16, 2019
1 parent cb4e3f3 commit a4f5aa3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static class KefuMsgType {
* 菜单消息.
*/
public static final String MSGMENU = "msgmenu";

/**
* 小程序通知消息.
*/
public static final String MINIPROGRAM_NOTICE = "miniprogram_notice";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
import me.chanjar.weixin.cp.constant.WxCpConsts;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.KefuMsgType.*;

/**
* 消息.
Expand Down Expand Up @@ -40,9 +44,13 @@ public class WxCpMessage implements Serializable {
private String btnTxt;
private List<NewArticle> articles = new ArrayList<>();
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
private String appId;
private String page;
private Boolean emphasisFirstItem;
private Map<String, String> contentItems;

/**
* 任务卡片特有的属性
* 任务卡片特有的属性.
*/
private String taskId;
private List<TaskCardButton> taskButtons = new ArrayList<>();
Expand Down Expand Up @@ -117,10 +125,16 @@ public static TaskCardBuilder TASKCARD() {
return new TaskCardBuilder();
}

/**
* 获得小程序通知消息builder.
*/
public static MiniProgramNoticeMsgBuilder newMiniProgramNoticeBuilder() {
return new MiniProgramNoticeMsgBuilder();
}

/**
* <pre>
* 请使用
* 请使用.
* {@link KefuMsgType#TEXT}
* {@link KefuMsgType#IMAGE}
* {@link KefuMsgType#VOICE}
Expand All @@ -130,6 +144,7 @@ public static TaskCardBuilder TASKCARD() {
* {@link KefuMsgType#MPNEWS}
* {@link KefuMsgType#MARKDOWN}
* {@link KefuMsgType#TASKCARD}
* {@link KefuMsgType#MINIPROGRAM_NOTICE}
* </pre>
*
* @param msgType 消息类型
Expand Down Expand Up @@ -169,19 +184,19 @@ public String toJson() {

private void handleMsgType(JsonObject messageJson) {
switch (this.getMsgType()) {
case KefuMsgType.TEXT: {
case TEXT: {
JsonObject text = new JsonObject();
text.addProperty("content", this.getContent());
messageJson.add("text", text);
break;
}
case KefuMsgType.MARKDOWN: {
case MARKDOWN: {
JsonObject text = new JsonObject();
text.addProperty("content", this.getContent());
messageJson.add("markdown", text);
break;
}
case KefuMsgType.TEXTCARD: {
case TEXTCARD: {
JsonObject text = new JsonObject();
text.addProperty("title", this.getTitle());
text.addProperty("description", this.getDescription());
Expand All @@ -190,25 +205,25 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("textcard", text);
break;
}
case KefuMsgType.IMAGE: {
case IMAGE: {
JsonObject image = new JsonObject();
image.addProperty("media_id", this.getMediaId());
messageJson.add("image", image);
break;
}
case KefuMsgType.FILE: {
case FILE: {
JsonObject image = new JsonObject();
image.addProperty("media_id", this.getMediaId());
messageJson.add("file", image);
break;
}
case KefuMsgType.VOICE: {
case VOICE: {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", this.getMediaId());
messageJson.add("voice", voice);
break;
}
case KefuMsgType.VIDEO: {
case VIDEO: {
JsonObject video = new JsonObject();
video.addProperty("media_id", this.getMediaId());
video.addProperty("thumb_media_id", this.getThumbMediaId());
Expand All @@ -217,7 +232,7 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("video", video);
break;
}
case KefuMsgType.NEWS: {
case NEWS: {
JsonObject newsJsonObject = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (NewArticle article : this.getArticles()) {
Expand All @@ -232,7 +247,7 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("news", newsJsonObject);
break;
}
case KefuMsgType.MPNEWS: {
case MPNEWS: {
JsonObject newsJsonObject = new JsonObject();
if (this.getMediaId() != null) {
newsJsonObject.addProperty("media_id", this.getMediaId());
Expand All @@ -255,7 +270,7 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("mpnews", newsJsonObject);
break;
}
case KefuMsgType.TASKCARD: {
case TASKCARD: {
JsonObject text = new JsonObject();
text.addProperty("title", this.getTitle());
text.addProperty("description", this.getDescription());
Expand Down Expand Up @@ -291,6 +306,25 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("taskcard", text);
break;
}
case MINIPROGRAM_NOTICE: {
JsonObject notice = new JsonObject();
notice.addProperty("appid", this.getAppId());
notice.addProperty("page", this.getPage());
notice.addProperty("description", this.getDescription());
notice.addProperty("title", this.getTitle());
notice.addProperty("emphasis_first_item", this.getEmphasisFirstItem());
JsonArray content = new JsonArray();
for (Map.Entry<String, String> item : this.getContentItems().entrySet()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("key", item.getKey());
articleJson.addProperty("value", item.getValue());
content.add(articleJson);
}
notice.add("content_item", content);

messageJson.add("miniprogram_notice", notice);
break;
}
default: {
// do nothing
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.chanjar.weixin.cp.bean.messagebuilder;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;

import java.util.Map;

/**
* <pre>
* miniprogram_notice 类型的消息 builder
* Created by Binary Wang on 2019/6/16.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class MiniProgramNoticeMsgBuilder extends BaseBuilder<MiniProgramNoticeMsgBuilder> {
private String title;
private String description;
private String appId;
private String page;
private Boolean emphasisFirstItem;
private Map<String, String> contentItems;

public MiniProgramNoticeMsgBuilder() {
this.msgType = WxConsts.KefuMsgType.MINIPROGRAM_NOTICE;
}

public MiniProgramNoticeMsgBuilder appId(String appId) {
this.appId = appId;
return this;
}

public MiniProgramNoticeMsgBuilder page(String page) {
this.page = page;
return this;
}

public MiniProgramNoticeMsgBuilder title(String title) {
this.title = title;
return this;
}

public MiniProgramNoticeMsgBuilder description(String description) {
this.description = description;
return this;
}

public MiniProgramNoticeMsgBuilder contentItems(Map<String, String> contentItems) {
this.contentItems = contentItems;
return this;
}

public MiniProgramNoticeMsgBuilder emphasisFirstItem(Boolean emphasisFirstItem) {
this.emphasisFirstItem = emphasisFirstItem;
return this;
}

@Override
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setContentItems(this.contentItems);
m.setAppId(this.appId);
m.setDescription(this.description);
m.setTitle(this.title);
m.setEmphasisFirstItem(this.emphasisFirstItem);
m.setPage(this.page);
return m;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.chanjar.weixin.cp.api;

import com.google.common.collect.ImmutableMap;
import org.testng.annotations.*;

import com.google.inject.Inject;
Expand Down Expand Up @@ -107,4 +108,27 @@ public void testSendMessage_textCard() throws WxErrorException {
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}

@Test
public void testSendMessage_miniprogram_notice() throws WxErrorException {
WxCpMessage message = WxCpMessage
.newMiniProgramNoticeBuilder()
.toUser(configStorage.getUserId())
.appId("wx123123123123123")
.page("pages/index?userid=zhangsan&orderid=123123123")
.title("会议室预订成功通知")
.description("4月27日 16:16")
.emphasisFirstItem(true)
.contentItems(ImmutableMap.of("会议室","402",
"会议地点","广州TIT-402会议室",
"会议时间","2018年8月1日 09:00-09:30"))
.build();

WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
assertNotNull(messageSendResult);
System.out.println(messageSendResult);
System.out.println(messageSendResult.getInvalidPartyList());
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}
}

0 comments on commit a4f5aa3

Please sign in to comment.