@@ -8,10 +8,7 @@ import me.chanjar.weixin.common.session.WxSession; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpTag; | |||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||
import me.chanjar.weixin.cp.bean.*; | |||
import java.io.File; | |||
import java.io.IOException; | |||
@@ -141,7 +138,7 @@ public interface WxCpService { | |||
* | |||
* @param message 要发送的消息对象 | |||
*/ | |||
void messageSend(WxCpMessage message) throws WxErrorException; | |||
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -17,10 +17,7 @@ import me.chanjar.weixin.common.util.http.*; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||
import me.chanjar.weixin.cp.api.WxCpService; | |||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpTag; | |||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||
import me.chanjar.weixin.cp.bean.*; | |||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.slf4j.Logger; | |||
@@ -131,9 +128,9 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ | |||
} | |||
@Override | |||
public void messageSend(WxCpMessage message) throws WxErrorException { | |||
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException { | |||
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send"; | |||
post(url, message.toJson()); | |||
return WxCpMessageSendResult.fromJson(this.post(url, message.toJson())); | |||
} | |||
@Override | |||
@@ -0,0 +1,103 @@ | |||
package me.chanjar.weixin.cp.bean; | |||
import com.google.common.base.Splitter; | |||
import com.google.gson.annotations.SerializedName; | |||
import me.chanjar.weixin.common.util.ToStringUtils; | |||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.Collections; | |||
import java.util.List; | |||
/** | |||
* <pre> | |||
* 消息发送结果对象类 | |||
* Created by Binary Wang on 2017-6-22. | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* </pre> | |||
*/ | |||
public class WxCpMessageSendResult { | |||
@Override | |||
public String toString() { | |||
return ToStringUtils.toSimpleString(this); | |||
} | |||
public static WxCpMessageSendResult fromJson(String json) { | |||
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpMessageSendResult.class); | |||
} | |||
@SerializedName("errcode") | |||
private Integer errCode; | |||
@SerializedName("errmsg") | |||
private String errMsg; | |||
@SerializedName("invaliduser") | |||
private String invalidUser; | |||
@SerializedName("invalidparty") | |||
private String invalidParty; | |||
@SerializedName("invalidtag") | |||
private String invalidTag; | |||
public Integer getErrCode() { | |||
return this.errCode; | |||
} | |||
public void setErrCode(Integer errCode) { | |||
this.errCode = errCode; | |||
} | |||
public String getErrMsg() { | |||
return this.errMsg; | |||
} | |||
public void setErrMsg(String errMsg) { | |||
this.errMsg = errMsg; | |||
} | |||
public String getInvalidUser() { | |||
return this.invalidUser; | |||
} | |||
public void setInvalidUser(String invalidUser) { | |||
this.invalidUser = invalidUser; | |||
} | |||
public String getInvalidParty() { | |||
return this.invalidParty; | |||
} | |||
public void setInvalidParty(String invalidParty) { | |||
this.invalidParty = invalidParty; | |||
} | |||
public String getInvalidTag() { | |||
return this.invalidTag; | |||
} | |||
public void setInvalidTag(String invalidTag) { | |||
this.invalidTag = invalidTag; | |||
} | |||
public List<String> getInvalidUserList() { | |||
return this.content2List(this.invalidUser); | |||
} | |||
private List<String> content2List(String content) { | |||
if(StringUtils.isBlank(content)){ | |||
return Collections.emptyList(); | |||
} | |||
return Splitter.on("|").splitToList(content); | |||
} | |||
public List<String> getInvalidPartyList() { | |||
return this.content2List(this.invalidParty); | |||
} | |||
public List<String> getInvalidTagList() { | |||
return this.content2List(this.invalidTag); | |||
} | |||
} |
@@ -5,38 +5,58 @@ import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; | |||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult; | |||
import org.testng.annotations.*; | |||
import static org.testng.Assert.*; | |||
/*** | |||
* 测试发送消息 | |||
* @author Daniel Qian | |||
* | |||
*/ | |||
@Test(groups = "customMessageAPI", dependsOnGroups = "baseAPI") | |||
@Test(groups = "customMessageAPI") | |||
@Guice(modules = ApiTestModule.class) | |||
public class WxCpMessageAPITest { | |||
@Inject | |||
protected WxCpServiceImpl wxService; | |||
private ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage; | |||
@BeforeTest | |||
public void setup() { | |||
configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage(); | |||
} | |||
public void testSendCustomMessage() throws WxErrorException { | |||
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage(); | |||
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>"); | |||
this.wxService.messageSend(message1); | |||
public void testSendMessage() throws WxErrorException { | |||
WxCpMessage message = new WxCpMessage(); | |||
message.setAgentId(configStorage.getAgentId()); | |||
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT); | |||
message.setToUser(configStorage.getUserId()); | |||
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>"); | |||
WxCpMessage message2 = WxCpMessage | |||
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()); | |||
} | |||
public void testSendMessage1() throws WxErrorException { | |||
WxCpMessage message = WxCpMessage | |||
.TEXT() | |||
.agentId(configStorage.getAgentId()) | |||
.toUser(configStorage.getUserId()) | |||
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>") | |||
.build(); | |||
this.wxService.messageSend(message2); | |||
} | |||
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()); | |||
} | |||
} |