调整细节&新增小视频消息

This commit is contained in:
jinyu
2015-04-08 18:28:32 +08:00
parent 78e3757029
commit 624ccff953
17 changed files with 137 additions and 83 deletions
@@ -1,9 +1,12 @@
package com.foxinmy.weixin4j.qy.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.msg.model.Base;
import com.foxinmy.weixin4j.msg.model.Notifyable;
import com.foxinmy.weixin4j.qy.message.NotifyMessage;
import com.foxinmy.weixin4j.token.TokenHolder;
@@ -63,11 +66,20 @@ public class NotifyApi extends QyApi {
* @see com.foxinmy.weixin4j.qy.message.NotifyMessage
*/
public JSONObject sendNotify(NotifyMessage notify) throws WeixinException {
Base box = notify.getBox();
if (!(box instanceof Notifyable)) {
throw new WeixinException(String.format(
"%s not implement Notifyable", box.getClass()));
}
String msgtype = box.getMediaType().name();
JSONObject obj = (JSONObject) JSON.toJSON(notify);
obj.put("msgtype", msgtype);
obj.put(msgtype, box);
String message_send_uri = getRequestUri("message_send_uri");
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(message_send_uri, token.getAccessToken()),
notify.toJson());
obj.toJSONString());
return response.getAsJson();
}
@@ -5,8 +5,6 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.msg.model.Base;
@@ -55,7 +53,7 @@ public class NotifyMessage implements Serializable {
/**
* 消息对象
*/
@JSONField(name = "%s")
@JSONField(serialize = false)
private Base box;
public NotifyMessage(Base box, int agentid) {
@@ -149,20 +147,6 @@ public class NotifyMessage implements Serializable {
this.box = box;
}
/**
* 消息json化
*
* @return
* {"image":{"media_id":"123"},"agentid":0,"msgtype":"image","safe":0
* ,"touser":"@all"}
*/
public String toJson() {
String msgtype = box.getMediaType().name();
JSONObject obj = (JSONObject) JSON.toJSON(this);
obj.put("msgtype", msgtype);
return String.format(obj.toJSONString(), msgtype);
}
@Override
public String toString() {
return "NotifyMessage [touser=" + touser + ", toparty=" + toparty
@@ -36,58 +36,58 @@ public class NotifyMsgTest extends TokenTest {
}
@Test
public void text() {
public void text() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Text("content"), 0);
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void image() {
public void image() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Image("123"), 0);
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void voice() {
public void voice() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Voice("123"), 0);
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void video() {
public void video() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Video("123"), 0);
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void file() {
public void file() throws WeixinException {
File file = new File("file");
NotifyMessage notify = new NotifyMessage(file, 0);
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void news() {
public void news() throws WeixinException {
News news = new News();
NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void mpnews() {
public void mpnews() throws WeixinException {
MpNews news = new MpNews();
NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("thumbMediaId1", "title1", "content1");
news.pushArticle("thumbMediaId2", "title1", "content2");
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void send1() throws WeixinException {
Text text = new Text("this is a text");
JSONObject result = notifyApi.sendNotify(new NotifyMessage(text, 0));
JSONObject result = notifyApi.sendNotify(new NotifyMessage(text, 1));
Assert.assertEquals(0, result.getIntValue("errcode"));
}
}