Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

公众号客服消息添加 "发送小程序卡片" 类型 #615

Merged
merged 1 commit into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public static class KefuMsgType {
* 转发到客服的消息.
*/
public static final String TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";

/**
* 小程序卡片(要求小程序与公众号已关联)
*/
public static final String MINIPROGRAMPAGE="miniprogrampage";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class WxMpKefuMessage implements Serializable {
private String kfAccount;
private String cardId;
private String mpNewsMediaId;
private String miniProgramAppId;
private String miniProgramPagePath;
private List<WxArticle> articles = new ArrayList<>();

/**
Expand Down Expand Up @@ -88,6 +90,13 @@ public static WxCardBuilder WXCARD() {
return new WxCardBuilder();
}

/**
* 小程序卡片
*/
public static MiniProgramPageBuilder MINIPROGRAMPAGE() {
return new MiniProgramPageBuilder();
}

/**
* <pre>
* 请使用
Expand All @@ -99,6 +108,7 @@ public static WxCardBuilder WXCARD() {
* {@link WxConsts.KefuMsgType#NEWS}
* {@link WxConsts.KefuMsgType#MPNEWS}
* {@link WxConsts.KefuMsgType#WXCARD}
* {@link WxConsts.KefuMsgType#MINIPROGRAMPAGE}
* </pre>
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.chanjar.weixin.mp.builder.kefu;

import me.chanjar.weixin.common.api.WxConsts.KefuMsgType;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;

/**
* 小程序卡片 builder
* <pre>
* 用法:
* WxMpKefuMessage m = WxMpKefuMessage.MINIPROGRAMPAGE().title("xxxx").thumbMediaId("xxxxx").appId("xxxx").pagePath("****").toUser(...).build();
* </pre>
*
* @author boris.bao
*/
public final class MiniProgramPageBuilder extends BaseBuilder<MiniProgramPageBuilder> {

private String title;
private String appId;
private String pagePath;
private String thumbMediaId;

public MiniProgramPageBuilder() {
this.msgType = KefuMsgType.MINIPROGRAMPAGE;
}


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

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


public MiniProgramPageBuilder pagePath(String pagePath) {
this.pagePath = pagePath;
return this;
}


public MiniProgramPageBuilder thumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
return this;
}


@Override
public WxMpKefuMessage build() {
WxMpKefuMessage m = super.build();
m.setTitle(this.title);
m.setMiniProgramAppId(this.appId);
m.setMiniProgramPagePath(this.pagePath);
m.setThumbMediaId(this.thumbMediaId);
return m;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxConsts.KefuMsgType;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -79,6 +80,15 @@ public JsonElement serialize(WxMpKefuMessage message, Type typeOfSrc, JsonSerial
messageJson.add("wxcard", wxcard);
}

if (KefuMsgType.MINIPROGRAMPAGE.equals(message.getMsgType())) {
JsonObject miniProgramPage = new JsonObject();
miniProgramPage.addProperty("title", message.getTitle());
miniProgramPage.addProperty("appid", message.getMiniProgramAppId());
miniProgramPage.addProperty("pagepath", message.getMiniProgramPagePath());
miniProgramPage.addProperty("thumb_media_id", message.getThumbMediaId());
messageJson.add("miniprogrampage", miniProgramPage);
}

if (StringUtils.isNotBlank(message.getKfAccount())) {
JsonObject newsJsonObject = new JsonObject();
newsJsonObject.addProperty("kf_account", message.getKfAccount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage.WxArticle;
import org.testng.*;
import org.testng.annotations.*;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test
public class WxMpKefuMessageTest {
Expand All @@ -13,38 +13,44 @@ public void testTextReply() {
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.KefuMsgType.TEXT);
reply.setContent("sfsfdsdf");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
Assert
.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}

public void testTextBuild() {
WxMpKefuMessage reply = WxMpKefuMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
Assert
.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}

public void testImageReply() {
WxMpKefuMessage reply = new WxMpKefuMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.KefuMsgType.IMAGE);
reply.setMediaId("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}

public void testImageBuild() {
WxMpKefuMessage reply = WxMpKefuMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}

public void testVoiceReply() {
WxMpKefuMessage reply = new WxMpKefuMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.KefuMsgType.VOICE);
reply.setMediaId("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}

public void testVoiceBuild() {
WxMpKefuMessage reply = WxMpKefuMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}

public void testVideoReply() {
Expand All @@ -55,12 +61,15 @@ public void testVideoReply() {
reply.setThumbMediaId("MEDIA_ID");
reply.setTitle("TITLE");
reply.setDescription("DESCRIPTION");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
}

public void testVideoBuild() {
WxMpKefuMessage reply = WxMpKefuMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
WxMpKefuMessage reply = WxMpKefuMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID")
.thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
}

public void testMusicReply() {
Expand All @@ -72,7 +81,8 @@ public void testMusicReply() {
reply.setTitle("TITLE");
reply.setMusicUrl("MUSIC_URL");
reply.setHqMusicUrl("HQ_MUSIC_URL");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
}

public void testMusicBuild() {
Expand All @@ -84,7 +94,8 @@ public void testMusicBuild() {
.musicUrl("MUSIC_URL")
.hqMusicUrl("HQ_MUSIC_URL")
.build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
}

public void testNewsReply() {
Expand All @@ -106,8 +117,8 @@ public void testNewsReply() {
article2.setTitle("Happy Day");
reply.getArticles().add(article2);


Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
}

public void testNewsBuild() {
Expand All @@ -125,7 +136,22 @@ public void testNewsBuild() {

WxMpKefuMessage reply = WxMpKefuMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();

Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
}

public void testMiniProgramPageBuild() {

WxMpKefuMessage reply = WxMpKefuMessage.MINIPROGRAMPAGE()
.toUser("OPENID")
.title("title")
.appId("appid")
.pagePath("pagepath")
.thumbMediaId("thumb_media_id")
.build();

Assert.assertEquals(reply.toJson(),
"{\"touser\":\"OPENID\",\"msgtype\":\"miniprogrampage\",\"miniprogrampage\":{\"title\":\"title\",\"appid\":\"appid\",\"pagepath\":\"pagepath\",\"thumb_media_id\":\"thumb_media_id\"}}");
}

}