mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
#256 企业微信发送消息增加文本卡片消息的支持
This commit is contained in:
parent
131402f8f6
commit
22344ebe2c
@ -35,6 +35,7 @@ public class WxConsts {
|
|||||||
public static final String CUSTOM_MSG_NEWS = "news";//图文消息(点击跳转到外链)
|
public static final String CUSTOM_MSG_NEWS = "news";//图文消息(点击跳转到外链)
|
||||||
public static final String CUSTOM_MSG_MPNEWS = "mpnews";//图文消息(点击跳转到图文消息页面)
|
public static final String CUSTOM_MSG_MPNEWS = "mpnews";//图文消息(点击跳转到图文消息页面)
|
||||||
public static final String CUSTOM_MSG_FILE = "file";//发送文件(CP专用)
|
public static final String CUSTOM_MSG_FILE = "file";//发送文件(CP专用)
|
||||||
|
public static final String CUSTOM_MSG_TEXTCARD = "textcard";//文本卡片消息(CP专用)
|
||||||
public static final String CUSTOM_MSG_WXCARD = "wxcard";//卡券消息
|
public static final String CUSTOM_MSG_WXCARD = "wxcard";//卡券消息
|
||||||
public static final String CUSTOM_MSG_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
|
public static final String CUSTOM_MSG_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
|
||||||
public static final String CUSTOM_MSG_SAFE_NO = "0";
|
public static final String CUSTOM_MSG_SAFE_NO = "0";
|
||||||
|
@ -30,6 +30,7 @@ public class WxCpMessage implements Serializable {
|
|||||||
private String musicUrl;
|
private String musicUrl;
|
||||||
private String hqMusicUrl;
|
private String hqMusicUrl;
|
||||||
private String safe;
|
private String safe;
|
||||||
|
private String url;
|
||||||
private List<NewArticle> articles = new ArrayList<>();
|
private List<NewArticle> articles = new ArrayList<>();
|
||||||
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
|
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
|
||||||
|
|
||||||
@ -40,6 +41,13 @@ public class WxCpMessage implements Serializable {
|
|||||||
return new TextBuilder();
|
return new TextBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得文本卡片消息builder
|
||||||
|
*/
|
||||||
|
public static TextCardBuilder TEXTCARD() {
|
||||||
|
return new TextCardBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得图片消息builder
|
* 获得图片消息builder
|
||||||
*/
|
*/
|
||||||
@ -220,4 +228,11 @@ public class WxCpMessage implements Serializable {
|
|||||||
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
|
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return this.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.bean.messagebuilder;
|
|||||||
|
|
||||||
import me.chanjar.weixin.common.api.WxConsts;
|
import me.chanjar.weixin.common.api.WxConsts;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
public class BaseBuilder<T> {
|
public class BaseBuilder<T> {
|
||||||
protected String msgType;
|
protected String msgType;
|
||||||
@ -43,8 +44,7 @@ public class BaseBuilder<T> {
|
|||||||
m.setToUser(this.toUser);
|
m.setToUser(this.toUser);
|
||||||
m.setToParty(this.toParty);
|
m.setToParty(this.toParty);
|
||||||
m.setToTag(this.toTag);
|
m.setToTag(this.toTag);
|
||||||
m.setSafe(
|
m.setSafe(StringUtils.defaultIfBlank(this.safe, WxConsts.CUSTOM_MSG_SAFE_NO));
|
||||||
(this.safe == null || "".equals(this.safe)) ? WxConsts.CUSTOM_MSG_SAFE_NO : this.safe);
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package me.chanjar.weixin.cp.bean.messagebuilder;
|
||||||
|
|
||||||
|
import me.chanjar.weixin.common.api.WxConsts;
|
||||||
|
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 文本卡片消息Builder
|
||||||
|
* 用法: WxCustomMessage m = WxCustomMessage.TEXTCARD().title(...)....toUser(...).build();
|
||||||
|
* Created by Binary Wang on 2017-7-2.
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
*/
|
||||||
|
public class TextCardBuilder extends BaseBuilder<TextCardBuilder> {
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
public TextCardBuilder() {
|
||||||
|
this.msgType = WxConsts.CUSTOM_MSG_TEXTCARD;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextCardBuilder title(String title) {
|
||||||
|
this.title = title;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextCardBuilder description(String description) {
|
||||||
|
this.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextCardBuilder url(String url) {
|
||||||
|
this.url = url;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WxCpMessage build() {
|
||||||
|
WxCpMessage m = super.build();
|
||||||
|
m.setTitle(this.title);
|
||||||
|
m.setDescription(this.description);
|
||||||
|
m.setUrl(this.url);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,14 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
|
|||||||
messageJson.add("text", text);
|
messageJson.add("text", text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (WxConsts.CUSTOM_MSG_TEXTCARD.equals(message.getMsgType())) {
|
||||||
|
JsonObject text = new JsonObject();
|
||||||
|
text.addProperty("title", message.getTitle());
|
||||||
|
text.addProperty("description", message.getDescription());
|
||||||
|
text.addProperty("url", message.getUrl());
|
||||||
|
messageJson.add("textcard", text);
|
||||||
|
}
|
||||||
|
|
||||||
if (WxConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgType())) {
|
if (WxConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgType())) {
|
||||||
JsonObject image = new JsonObject();
|
JsonObject image = new JsonObject();
|
||||||
image.addProperty("media_id", message.getMediaId());
|
image.addProperty("media_id", message.getMediaId());
|
||||||
|
@ -57,6 +57,5 @@ public class WxCpMessageAPITest {
|
|||||||
System.out.println(messageSendResult.getInvalidPartyList());
|
System.out.println(messageSendResult.getInvalidPartyList());
|
||||||
System.out.println(messageSendResult.getInvalidUserList());
|
System.out.println(messageSendResult.getInvalidUserList());
|
||||||
System.out.println(messageSendResult.getInvalidTagList());
|
System.out.println(messageSendResult.getInvalidTagList());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package me.chanjar.weixin.cp.bean;
|
package me.chanjar.weixin.cp.bean;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.api.WxConsts;
|
|
||||||
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
||||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||||
import org.testng.annotations.*;
|
import org.testng.annotations.*;
|
||||||
@ -10,82 +9,32 @@ import static org.testng.Assert.*;
|
|||||||
@Test
|
@Test
|
||||||
public class WxCpMessageTest {
|
public class WxCpMessageTest {
|
||||||
|
|
||||||
public void testTextReply() {
|
|
||||||
WxCpMessage reply = new WxCpMessage();
|
|
||||||
reply.setToUser("OPENID");
|
|
||||||
reply.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
|
||||||
reply.setContent("sfsfdsdf");
|
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testTextBuild() {
|
public void testTextBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
|
WxCpMessage reply = WxCpMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"},\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testImageReply() {
|
public void testTextCardBuild() {
|
||||||
WxCpMessage reply = new WxCpMessage();
|
WxCpMessage reply = WxCpMessage.TEXTCARD().toUser("OPENID")
|
||||||
reply.setToUser("OPENID");
|
.title("领奖通知")
|
||||||
reply.setMsgType(WxConsts.CUSTOM_MSG_IMAGE);
|
.description( "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
|
||||||
reply.setMediaId("MEDIA_ID");
|
.url("http://www.qq.com").build();
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\",\"description\":\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\",\"url\":\"http://www.qq.com\"},\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testImageBuild() {
|
public void testImageBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
WxCpMessage reply = WxCpMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"},\"safe\":\"0\"}");
|
||||||
}
|
|
||||||
|
|
||||||
public void testVoiceReply() {
|
|
||||||
WxCpMessage reply = new WxCpMessage();
|
|
||||||
reply.setToUser("OPENID");
|
|
||||||
reply.setMsgType(WxConsts.CUSTOM_MSG_VOICE);
|
|
||||||
reply.setMediaId("MEDIA_ID");
|
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testVoiceBuild() {
|
public void testVoiceBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
WxCpMessage reply = WxCpMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"},\"safe\":\"0\"}");
|
||||||
}
|
|
||||||
|
|
||||||
public void testVideoReply() {
|
|
||||||
WxCpMessage reply = new WxCpMessage();
|
|
||||||
reply.setToUser("OPENID");
|
|
||||||
reply.setMsgType(WxConsts.CUSTOM_MSG_VIDEO);
|
|
||||||
reply.setMediaId("MEDIA_ID");
|
|
||||||
reply.setThumbMediaId("MEDIA_ID");
|
|
||||||
reply.setTitle("TITLE");
|
|
||||||
reply.setDescription("DESCRIPTION");
|
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testVideoBuild() {
|
public void testVideoBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
|
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
|
||||||
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"safe\":\"0\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
||||||
}
|
|
||||||
|
|
||||||
public void testNewsReply() {
|
|
||||||
WxCpMessage reply = new WxCpMessage();
|
|
||||||
reply.setToUser("OPENID");
|
|
||||||
reply.setMsgType(WxConsts.CUSTOM_MSG_NEWS);
|
|
||||||
|
|
||||||
NewArticle article1 = new NewArticle();
|
|
||||||
article1.setUrl("URL");
|
|
||||||
article1.setPicUrl("PIC_URL");
|
|
||||||
article1.setDescription("Is Really A Happy Day");
|
|
||||||
article1.setTitle("Happy Day");
|
|
||||||
reply.getArticles().add(article1);
|
|
||||||
|
|
||||||
NewArticle article2 = new NewArticle();
|
|
||||||
article2.setUrl("URL");
|
|
||||||
article2.setPicUrl("PIC_URL");
|
|
||||||
article2.setDescription("Is Really A Happy Day");
|
|
||||||
article2.setTitle("Happy Day");
|
|
||||||
reply.getArticles().add(article2);
|
|
||||||
|
|
||||||
|
|
||||||
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() {
|
public void testNewsBuild() {
|
||||||
@ -103,7 +52,7 @@ public class WxCpMessageTest {
|
|||||||
|
|
||||||
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
||||||
|
|
||||||
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\"}]}}");
|
assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"safe\":\"0\",\"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 testMpnewsBuild_with_articles() {
|
public void testMpnewsBuild_with_articles() {
|
||||||
@ -136,7 +85,7 @@ public class WxCpMessageTest {
|
|||||||
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").mediaId("mmm").build();
|
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").mediaId("mmm").build();
|
||||||
|
|
||||||
assertEquals(reply.toJson(),
|
assertEquals(reply.toJson(),
|
||||||
"{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"mpnews\":{\"media_id\":\"mmm\"}}");
|
"{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"safe\":\"0\",\"mpnews\":{\"media_id\":\"mmm\"}}");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user