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

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
@@ -347,7 +347,23 @@ public class WeixinProxy {
*
* @param notify
* 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#sendNotify(NotifyMessage,String) }
* @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
return notifyApi.sendNotify(notify);
}
/**
* 发送客服消息(在48小时内不限制发送次数)
*
* @param notify
* 客服消息对象
* @param kfAccount
* 客服账号 可为空
* @throws WeixinException
* @return 处理结果
* @see <a
* href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a>
* @see com.foxinmy.weixin4j.msg.model.Text
@@ -358,8 +374,9 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.msg.model.News
* @see com.foxinmy.weixin4j.mp.api.NotifyApi
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
return notifyApi.sendNotify(notify);
public JsonResult sendNotify(NotifyMessage notify, String kfAccount)
throws WeixinException {
return notifyApi.sendNotify(notify, kfAccount);
}
/**
@@ -1,10 +1,14 @@
package com.foxinmy.weixin4j.mp.api;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.message.NotifyMessage;
import com.foxinmy.weixin4j.msg.model.Base;
import com.foxinmy.weixin4j.msg.model.Notifyable;
import com.foxinmy.weixin4j.token.TokenHolder;
@@ -32,7 +36,23 @@ public class NotifyApi extends MpApi {
*
* @param notify
* 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.api.NotifyApi#sendNotify(NotifyMessage, String)}
* @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
return sendNotify(notify, null);
}
/**
* 发送客服消息(在48小时内不限制发送次数)
*
* @param notify
* 客服消息对象
* @param kfAccount
* 客服账号 可为空
* @throws WeixinException
* @return 处理结果
* @see <a
* href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a>
* @see com.foxinmy.weixin4j.msg.model.Text
@@ -43,16 +63,28 @@ public class NotifyApi extends MpApi {
* @see com.foxinmy.weixin4j.msg.model.News
* @see com.foxinmy.weixin4j.mp.message.NotifyMessage
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
if (!(notify.getBox() instanceof Notifyable)) {
public JsonResult sendNotify(NotifyMessage notify, String kfAccount)
throws WeixinException {
Base box = notify.getBox();
if (!(box instanceof Notifyable)) {
throw new WeixinException(String.format(
"%s not implement Notifyable", notify.getBox().getClass()));
"%s not implement Notifyable", box.getClass()));
}
String msgtype = box.getMediaType().name();
JSONObject obj = new JSONObject();
obj.put("touser", notify.getTouser());
obj.put("msgtype", msgtype);
obj.put(msgtype, box);
if (StringUtils.isNotBlank(kfAccount)) {
JSONObject kf = new JSONObject();
kf.put("kf_account", kfAccount);
obj.put("customservice", kf);
}
String custom_notify_uri = getRequestUri("custom_notify_uri");
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(custom_notify_uri, token.getAccessToken()),
notify.toJson());
obj.toJSONString());
return response.getAsJsonResult();
}
@@ -26,4 +26,6 @@
* DataApi `数据统计API`
* OauthApi `oauth授权API`
* OauthApi `oauth授权API`
* CashApi `现金API`
@@ -149,7 +149,7 @@ datacube_uri={api_base_url}/datacube/%s?access_token=%s
# \u4e0a\u4f20\u6c38\u4e45\u56fe\u6587\u7d20\u6750
material_article_upload_uri={api_cgi_url}/material/add_news?access_token=%s
# \u4e0a\u4f20\u6c38\u4e45\u5a92\u4f53\u7d20\u6750
material_media_upload_uri={file_base_url}/material/add_material?access_token=%s
material_media_upload_uri={api_cgi_url}/material/add_material?access_token=%s
# \u4e0b\u8f7d\u6c38\u4e45\u5a92\u4f53\u7d20\u6750
material_media_download_uri={api_cgi_url}/material/get_material?access_token=%s
# \u66f4\u65b0\u6c38\u4e45\u56fe\u6587\u7d20\u6750
@@ -2,9 +2,6 @@ package com.foxinmy.weixin4j.mp.message;
import java.io.Serializable;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.msg.model.Base;
/**
@@ -33,13 +30,8 @@ public class NotifyMessage implements Serializable {
/**
* 消息对象
*/
@JSONField(name = "%s")
private Base box;
public NotifyMessage(Base box) {
this(null, box);
}
public NotifyMessage(String touser, Base box) {
this.touser = touser;
this.box = box;
@@ -61,18 +53,6 @@ public class NotifyMessage implements Serializable {
this.box = box;
}
/**
* 客服消息json化
*
* @return {"touser": "to","msgtype": "text","text": {"content": "123"}}
*/
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 + ", box=" + box + "]";
@@ -24,7 +24,7 @@ import com.foxinmy.weixin4j.type.MediaType;
/**
* 客服消息测试
*
* @className MessageNotifyTest
* @className NotifyMsgTest
* @author jy.hu
* @date 2014年4月10日
* @since JDK 1.7
@@ -42,42 +42,42 @@ public class NotifyMsgTest extends TokenTest {
}
@Test
public void text() {
public void text() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Text("ttt"));
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void image() {
public void image() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Image("image"));
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void voice() {
public void voice() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Voice("voice"));
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void video() {
public void video() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Video("video"));
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test
public void music() {
public void music() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Music("music"));
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("to", news);
news.pushArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notify.toJson());
System.out.println(notifyApi.sendNotify(notify));
}
@Test