直接发送消息的支持

This commit is contained in:
Daniel Qian 2014-10-21 18:20:10 +08:00
parent b69aa77fbc
commit 258f35a192
23 changed files with 291 additions and 190 deletions

View File

@ -17,7 +17,6 @@ public class WxConsts {
public static final String XML_MSG_LOCATION = "location";
public static final String XML_MSG_LINK = "link";
public static final String XML_MSG_EVENT = "event";
public static final String XML_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
///////////////////////
// 客服消息的消息类型
@ -28,49 +27,8 @@ public class WxConsts {
public static final String CUSTOM_MSG_VIDEO = "video";
public static final String CUSTOM_MSG_MUSIC = "music";
public static final String CUSTOM_MSG_NEWS = "news";
///////////////////////
// 群发消息的消息类型
///////////////////////
public static final String MASS_MSG_NEWS = "mpnews";
public static final String MASS_MSG_TEXT = "text";
public static final String MASS_MSG_VOICE = "voice";
public static final String MASS_MSG_IMAGE = "image";
public static final String MASS_MSG_VIDEO = "mpvideo";
///////////////////////
// 群发消息后微信端推送给服务器的反馈消息
///////////////////////
public static final String MASS_ST_SUCCESS = "send success";
public static final String MASS_ST_FAIL = "send fail";
public static final String MASS_ST_涉嫌广告 = "err(10001)";
public static final String MASS_ST_涉嫌政治 = "err(20001)";
public static final String MASS_ST_涉嫌社会 = "err(20004)";
public static final String MASS_ST_涉嫌色情 = "err(20002)";
public static final String MASS_ST_涉嫌违法犯罪 = "err(20006)";
public static final String MASS_ST_涉嫌欺诈 = "err(20008)";
public static final String MASS_ST_涉嫌版权 = "err(20013)";
public static final String MASS_ST_涉嫌互推_互相宣传 = "err(22000)";
public static final String MASS_ST_涉嫌其他 = "err(21000)";
/**
* 群发反馈消息代码所对应的文字描述
*/
public static final Map<String, String> MASS_ST_2_DESC = new HashMap<String, String>();
static {
MASS_ST_2_DESC.put(MASS_ST_SUCCESS, "发送成功");
MASS_ST_2_DESC.put(MASS_ST_FAIL, "发送失败");
MASS_ST_2_DESC.put(MASS_ST_涉嫌广告, "涉嫌广告");
MASS_ST_2_DESC.put(MASS_ST_涉嫌政治, "涉嫌政治");
MASS_ST_2_DESC.put(MASS_ST_涉嫌社会, "涉嫌社会");
MASS_ST_2_DESC.put(MASS_ST_涉嫌色情, "涉嫌色情");
MASS_ST_2_DESC.put(MASS_ST_涉嫌违法犯罪, "涉嫌违法犯罪");
MASS_ST_2_DESC.put(MASS_ST_涉嫌欺诈, "涉嫌欺诈");
MASS_ST_2_DESC.put(MASS_ST_涉嫌版权, "涉嫌版权");
MASS_ST_2_DESC.put(MASS_ST_涉嫌互推_互相宣传, "涉嫌互推_互相宣传");
MASS_ST_2_DESC.put(MASS_ST_涉嫌其他, "涉嫌其他");
}
public static final String CUSTOM_MSG_FILE = "file";
///////////////////////
// 微信端推送过来的事件类型
///////////////////////

View File

@ -6,7 +6,7 @@ import java.io.InputStream;
import java.util.List;
import me.chanjar.weixin.enterprise.bean.*;
import me.chanjar.weixin.enterprise.bean.WxDepartment;
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.enterprise.bean.result.WxUser;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
@ -91,7 +91,7 @@ public interface WxCpService {
* @param message
* @throws WxErrorException
*/
public void messageSend(WxCustomMessage message) throws WxErrorException;
public void messageSend(WxCpMessage message) throws WxErrorException;
/**
* <pre>
@ -131,7 +131,7 @@ public interface WxCpService {
* @param name 分组名字30个字符以内
* @throws WxErrorException
*/
public WxDepartment departmentCreate(String name) throws WxErrorException;
public WxCpDepartment departmentCreate(String name) throws WxErrorException;
/**
* <pre>
@ -141,7 +141,7 @@ public interface WxCpService {
* @return
* @throws WxErrorException
*/
public List<WxDepartment> departmentGet() throws WxErrorException;
public List<WxCpDepartment> departmentGet() throws WxErrorException;
/**
* <pre>
@ -153,7 +153,7 @@ public interface WxCpService {
* @param group 要更新的groupgroup的id,name必须设置
* @throws WxErrorException
*/
public void departmentUpdate(WxDepartment group) throws WxErrorException;
public void departmentUpdate(WxCpDepartment group) throws WxErrorException;
/**
* <pre>
@ -163,7 +163,7 @@ public interface WxCpService {
* @param department
* @throws WxErrorException
*/
public void departmentDelete(WxDepartment department) throws WxErrorException;
public void departmentDelete(WxCpDepartment department) throws WxErrorException;
public void userCreate(WxUser user) throws WxErrorException;

View File

@ -9,7 +9,6 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import me.chanjar.weixin.enterprise.bean.*;
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil;
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.enterprise.util.crypto.SHA1;
import org.apache.commons.lang3.StringUtils;
@ -20,7 +19,7 @@ import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import me.chanjar.weixin.enterprise.bean.WxDepartment;
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
import me.chanjar.weixin.enterprise.bean.result.WxError;
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.enterprise.bean.result.WxUser;
@ -98,8 +97,8 @@ public class WxCpServiceImpl implements WxCpService {
}
}
public void messageSend(WxCustomMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
public void messageSend(WxCpMessage message) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
execute(new SimplePostRequestExecutor(), url, message.toJson());
}
@ -143,7 +142,7 @@ public class WxCpServiceImpl implements WxCpService {
}
public WxDepartment departmentCreate(String name) throws WxErrorException {
public WxCpDepartment departmentCreate(String name) throws WxErrorException {
// TODO
String url = "https://api.weixin.qq.com/cgi-bin/groups/create";
JsonObject json = new JsonObject();
@ -155,16 +154,16 @@ public class WxCpServiceImpl implements WxCpService {
new SimplePostRequestExecutor(),
url,
json.toString());
return WxDepartment.fromJson(responseContent);
return WxCpDepartment.fromJson(responseContent);
}
public void departmentUpdate(WxDepartment group) throws WxErrorException {
public void departmentUpdate(WxCpDepartment group) throws WxErrorException {
// TODO
String url = "https://api.weixin.qq.com/cgi-bin/groups/update";
execute(new SimplePostRequestExecutor(), url, group.toJson());
}
public List<WxDepartment> departmentGet() throws WxErrorException {
public List<WxCpDepartment> departmentGet() throws WxErrorException {
// TODO
String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
@ -174,11 +173,11 @@ public class WxCpServiceImpl implements WxCpService {
*/
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
return WxGsonBuilder.INSTANCE.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxDepartment>>() {
.fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxCpDepartment>>() {
}.getType());
}
public void departmentDelete(WxDepartment department) throws WxErrorException {
public void departmentDelete(WxCpDepartment department) throws WxErrorException {
// TODO
}

View File

@ -82,4 +82,17 @@ public class WxInMemoryCpConfigStorage implements WxCpConfigStorage {
public void setAgentId(String agentId) {
this.agentId = agentId;
}
@Override
public String toString() {
return "WxInMemoryCpConfigStorage{" +
"corpId='" + corpId + '\'' +
", corpSecret='" + corpSecret + '\'' +
", token='" + token + '\'' +
", accessToken='" + accessToken + '\'' +
", aesKey='" + aesKey + '\'' +
", agentId='" + agentId + '\'' +
", expiresIn=" + expiresIn +
'}';
}
}

View File

@ -7,7 +7,7 @@ import me.chanjar.weixin.enterprise.util.json.WxGsonBuilder;
* @author Daniel Qian
*
*/
public class WxDepartment {
public class WxCpDepartment {
private long id = -1;
private String name;
@ -31,8 +31,8 @@ public class WxDepartment {
this.count = count;
}
public static WxDepartment fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxDepartment.class);
public static WxCpDepartment fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxCpDepartment.class);
}
public String toJson() {

View File

@ -4,12 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.custombuilder.ImageBuilder;
import me.chanjar.weixin.enterprise.bean.custombuilder.MusicBuilder;
import me.chanjar.weixin.enterprise.bean.custombuilder.NewsBuilder;
import me.chanjar.weixin.enterprise.bean.custombuilder.TextBuilder;
import me.chanjar.weixin.enterprise.bean.custombuilder.VideoBuilder;
import me.chanjar.weixin.enterprise.bean.custombuilder.VoiceBuilder;
import me.chanjar.weixin.enterprise.bean.messagebuilder.*;
import me.chanjar.weixin.enterprise.util.json.WxGsonBuilder;
/**
@ -17,9 +12,12 @@ import me.chanjar.weixin.enterprise.util.json.WxGsonBuilder;
* @author Daniel Qian
*
*/
public class WxCustomMessage {
public class WxCpMessage {
private String toUser;
private String toParty;
private String toTag;
private String agentId;
private String msgType;
private String content;
private String mediaId;
@ -36,6 +34,31 @@ public class WxCustomMessage {
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getToParty() {
return toParty;
}
public void setToParty(String toParty) {
this.toParty = toParty;
}
public String getToTag() {
return toTag;
}
public void setToTag(String toTag) {
this.toTag = toTag;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getMsgType() {
return msgType;
}
@ -189,5 +212,13 @@ public class WxCustomMessage {
public static NewsBuilder NEWS() {
return new NewsBuilder();
}
/**
* 获得文件消息builder
* @return
*/
public static FileBuilder FILE() {
return new FileBuilder();
}
}

View File

@ -1,20 +0,0 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
public class BaseBuilder<T> {
protected String msgType;
protected String toUser;
public T toUser(String toUser) {
this.toUser = toUser;
return (T) this;
}
public WxCustomMessage build() {
WxCustomMessage m = new WxCustomMessage();
m.setMsgType(this.msgType);
m.setToUser(this.toUser);
return m;
}
}

View File

@ -0,0 +1,42 @@
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
public class BaseBuilder<T> {
protected String msgType;
protected String agentId;
protected String toUser;
protected String toParty;
protected String toTag;
public T agentId(String agentId) {
this.agentId = agentId;
return (T) this;
}
public T toUser(String toUser) {
this.toUser = toUser;
return (T) this;
}
public T toParty(String toParty) {
this.toParty = toParty;
return (T) this;
}
public T toTag(String toTag) {
this.toTag = toTag;
return (T) this;
}
public WxCpMessage build() {
WxCpMessage m = new WxCpMessage();
m.setAgentId(this.agentId);
m.setMsgType(this.msgType);
m.setToUser(this.toUser);
m.setToParty(this.toParty);
m.setToTag(this.toTag);
return m;
}
}

View File

@ -0,0 +1,31 @@
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 获得消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.FILE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class FileBuilder extends BaseBuilder<FileBuilder> {
private String mediaId;
public FileBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_FILE;
}
public FileBuilder mediaId(String media_id) {
this.mediaId = media_id;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}
}

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 获得消息builder
@ -23,8 +23,8 @@ public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 音乐消息builder
@ -52,8 +52,8 @@ public final class MusicBuilder extends BaseBuilder<MusicBuilder> {
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMusicUrl(this.musicUrl);
m.setHqMusicUrl(this.hqMusicUrl);
m.setTitle(title);

View File

@ -1,10 +1,10 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 图文消息builder
@ -17,19 +17,19 @@ import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
*/
public final class NewsBuilder extends BaseBuilder<NewsBuilder> {
private List<WxCustomMessage.WxArticle> articles = new ArrayList<WxCustomMessage.WxArticle>();
private List<WxCpMessage.WxArticle> articles = new ArrayList<WxCpMessage.WxArticle>();
public NewsBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_NEWS;
}
public NewsBuilder addArticle(WxCustomMessage.WxArticle article) {
public NewsBuilder addArticle(WxCpMessage.WxArticle article) {
this.articles.add(article);
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setArticles(this.articles);
return m;
}

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 文本消息builder
@ -23,8 +23,8 @@ public final class TextBuilder extends BaseBuilder<TextBuilder> {
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setContent(this.content);
return m;
}

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 视频消息builder
@ -47,8 +47,8 @@ public final class VideoBuilder extends BaseBuilder<VideoBuilder> {
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
m.setTitle(title);
m.setDescription(description);

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.enterprise.bean.custombuilder;
package me.chanjar.weixin.enterprise.bean.messagebuilder;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
/**
* 语音消息builder
@ -23,8 +23,8 @@ public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> {
return this;
}
public WxCustomMessage build() {
WxCustomMessage m = super.build();
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}

View File

@ -11,26 +11,36 @@ package me.chanjar.weixin.enterprise.util.json;
import java.lang.reflect.Type;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.commons.lang3.StringUtils;
/**
*
* @author Daniel Qian
*
*/
public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessage> {
public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
public JsonElement serialize(WxCustomMessage message, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("touser", message.getToUser());
messageJson.addProperty("agentid", message.getAgentId());
messageJson.addProperty("msgtype", message.getMsgType());
if (StringUtils.isNotBlank(message.getToUser())) {
messageJson.addProperty("touser", message.getToUser());
}
if (StringUtils.isNotBlank(message.getToParty())) {
messageJson.addProperty("toparty", message.getToUser());
}
if (StringUtils.isNotBlank(message.getToTag())) {
messageJson.addProperty("totag", message.getToUser());
}
if (WxConsts.CUSTOM_MSG_TEXT.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
@ -43,6 +53,12 @@ public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessag
messageJson.add("image", image);
}
if (WxConsts.CUSTOM_MSG_FILE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("file", image);
}
if (WxConsts.CUSTOM_MSG_VOICE.equals(message.getMsgType())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMediaId());
@ -70,7 +86,7 @@ public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessag
if (WxConsts.CUSTOM_MSG_NEWS.equals(message.getMsgType())) {
JsonArray articleJsonArray = new JsonArray();
for (WxCustomMessage.WxArticle article : message.getArticles()) {
for (WxCpMessage.WxArticle article : message.getArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("description", article.getDescription());

View File

@ -10,7 +10,7 @@ package me.chanjar.weixin.enterprise.util.json;
import java.lang.reflect.Type;
import me.chanjar.weixin.enterprise.bean.WxDepartment;
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
@ -25,9 +25,9 @@ import com.google.gson.JsonSerializer;
* @author Daniel Qian
*
*/
public class WxGroupGsonAdapter implements JsonSerializer<WxDepartment>, JsonDeserializer<WxDepartment> {
public class WxGroupGsonAdapter implements JsonSerializer<WxCpDepartment>, JsonDeserializer<WxCpDepartment> {
public JsonElement serialize(WxDepartment group, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WxCpDepartment group, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
JsonObject groupJson = new JsonObject();
groupJson.addProperty("name", group.getName());
@ -37,8 +37,8 @@ public class WxGroupGsonAdapter implements JsonSerializer<WxDepartment>, JsonDes
return json;
}
public WxDepartment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WxDepartment group = new WxDepartment();
public WxCpDepartment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WxCpDepartment group = new WxCpDepartment();
JsonObject groupJson = json.getAsJsonObject();
if (json.getAsJsonObject().get("group") != null) {
groupJson = json.getAsJsonObject().get("group").getAsJsonObject();

View File

@ -12,12 +12,12 @@ public class WxGsonBuilder {
static {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCustomMessage.class, new WxCustomMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxMassNews.class, new WxMassNewsGsonAdapter());
INSTANCE.registerTypeAdapter(WxMassGroupMessage.class, new WxMassMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMassOpenIdsMessage.class, new WxMassOpenIdsMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxDepartment.class, new WxGroupGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpDepartment.class, new WxGroupGsonAdapter());
INSTANCE.registerTypeAdapter(WxUser.class, new WxUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxUserList.class, new WxUserListGsonAdapter());
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxAccessTokenAdapter());

View File

@ -42,20 +42,43 @@ public class ApiTestModule implements Module {
@XmlAccessorType(XmlAccessType.FIELD)
public static class WxXmlCpConfigStorage extends WxInMemoryCpConfigStorage {
protected String openId;
protected String userId;
public String getOpenId() {
return openId;
protected String departmentId;
protected String tagId;
public String getUserId() {
return userId;
}
public void setOpenId(String openId) {
this.openId = openId;
public void setUserId(String userId) {
this.userId = userId;
}
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public String getTagId() {
return tagId;
}
public void setTagId(String tagId) {
this.tagId = tagId;
}
@Override
public String toString() {
return "SimpleWxConfigProvider [corpId=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken
+ ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]";
return super.toString() + " > WxXmlCpConfigStorage{" +
"userId='" + userId + '\'' +
", departmentId='" + departmentId + '\'' +
", tagId='" + tagId + '\'' +
'}';
}
}
}

View File

@ -0,0 +1,42 @@
package me.chanjar.weixin.enterprise.api;
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import com.google.inject.Inject;
/***
* 测试发送客服消息
* @author Daniel Qian
*
*/
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxCpMessageAPITest {
@Inject
protected WxCpServiceImpl wxService;
public void testSendCustomMessage() throws WxErrorException {
ApiTestModule.WxXmlCpConfigStorage configStorage = (ApiTestModule.WxXmlCpConfigStorage) wxService.wxCpConfigStorage;
WxCpMessage message1 = new WxCpMessage();
message1.setAgentId(configStorage.getAgentId());
message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
message1.setToUser(configStorage.getUserId());
message1.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
wxService.messageSend(message1);
WxCpMessage message2 = WxCpMessage
.TEXT()
.agentId(configStorage.getAgentId())
.toUser(configStorage.getUserId())
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
.build();
wxService.messageSend(message2);
}
}

View File

@ -1,33 +0,0 @@
package me.chanjar.weixin.enterprise.api;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import me.chanjar.weixin.enterprise.exception.WxErrorException;
import com.google.inject.Inject;
/***
* 测试发送客服消息
* @author Daniel Qian
*
*/
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxCustomMessageAPITest {
@Inject
protected WxCpServiceImpl wxService;
public void testSendCustomMessage() throws WxErrorException {
ApiTestModule.WxXmlCpConfigStorage configStorage = (ApiTestModule.WxXmlCpConfigStorage) wxService.wxCpConfigStorage;
WxCustomMessage message = new WxCustomMessage();
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
message.setToUser(configStorage.getOpenId());
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
wxService.messageSend(message);
}
}

View File

@ -2,7 +2,7 @@ package me.chanjar.weixin.enterprise.api;
import java.util.List;
import me.chanjar.weixin.enterprise.bean.WxDepartment;
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@ -23,19 +23,19 @@ public class WxGroupAPITest {
@Inject
protected WxCpServiceImpl wxService;
protected WxDepartment group;
protected WxCpDepartment group;
public void testGroupCreate() throws WxErrorException {
WxDepartment res = wxService.departmentCreate("测试分组1");
WxCpDepartment res = wxService.departmentCreate("测试分组1");
Assert.assertEquals(res.getName(), "测试分组1");
}
@Test(dependsOnMethods="testGroupCreate")
public void testGroupGet() throws WxErrorException {
List<WxDepartment> groupList = wxService.departmentGet();
List<WxCpDepartment> groupList = wxService.departmentGet();
Assert.assertNotNull(groupList);
Assert.assertTrue(groupList.size() > 0);
for (WxDepartment g : groupList) {
for (WxCpDepartment g : groupList) {
group = g;
Assert.assertNotNull(g.getName());
}

View File

@ -1,17 +1,16 @@
package me.chanjar.weixin.enterprise.bean;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
import org.testng.Assert;
import org.testng.annotations.Test;
import me.chanjar.weixin.enterprise.api.WxConsts;
import me.chanjar.weixin.enterprise.bean.WxCustomMessage.WxArticle;
import me.chanjar.weixin.enterprise.bean.WxCpMessage.WxArticle;
@Test
public class WxCustomMessageTest {
public class WxCpMessageTest {
public void testTextReply() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
reply.setContent("sfsfdsdf");
@ -19,12 +18,12 @@ public class WxCustomMessageTest {
}
public void testTextBuild() {
WxCustomMessage reply = WxCustomMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
WxCpMessage reply = WxCpMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}
public void testImageReply() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_IMAGE);
reply.setMediaId("MEDIA_ID");
@ -32,12 +31,12 @@ public class WxCustomMessageTest {
}
public void testImageBuild() {
WxCustomMessage reply = WxCustomMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
WxCpMessage reply = WxCpMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}
public void testVoiceReply() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_VOICE);
reply.setMediaId("MEDIA_ID");
@ -45,12 +44,12 @@ public class WxCustomMessageTest {
}
public void testVoiceBuild() {
WxCustomMessage reply = WxCustomMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
WxCpMessage reply = WxCpMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}
public void testVideoReply() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_VIDEO);
reply.setMediaId("MEDIA_ID");
@ -61,12 +60,12 @@ public class WxCustomMessageTest {
}
public void testVideoBuild() {
WxCustomMessage reply = WxCustomMessage.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();
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() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_MUSIC);
reply.setThumbMediaId("MEDIA_ID");
@ -78,7 +77,7 @@ public class WxCustomMessageTest {
}
public void testMusicBuild() {
WxCustomMessage reply = WxCustomMessage.MUSIC()
WxCpMessage reply = WxCpMessage.MUSIC()
.toUser("OPENID")
.title("TITLE")
.thumbMediaId("MEDIA_ID")
@ -90,7 +89,7 @@ public class WxCustomMessageTest {
}
public void testNewsReply() {
WxCustomMessage reply = new WxCustomMessage();
WxCpMessage reply = new WxCpMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxConsts.CUSTOM_MSG_NEWS);
@ -125,7 +124,7 @@ public class WxCustomMessageTest {
article2.setDescription("Is Really A Happy Day");
article2.setTitle("Happy Day");
WxCustomMessage reply = WxCustomMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"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\"}]}");
}