调整细节&新增小视频消息
This commit is contained in:
parent
78e3757029
commit
624ccff953
@ -226,4 +226,4 @@
|
||||
|
||||
+ **weixin4j-qy**: [MediaApi](./weixin4j-qy/weixin4j-qy-api/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java)新增批量上传成员和部门接口
|
||||
|
||||
<font color="red">released 1.3</font>
|
||||
+ <font color="red">released 1.3</font>
|
||||
@ -16,12 +16,14 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @see
|
||||
*/
|
||||
public abstract class AbstractActionMapping implements ActionMapping {
|
||||
|
||||
protected final static String DECOLLATOR = ":";
|
||||
|
||||
protected String getMappingKey(String xmlMsg) throws DocumentException {
|
||||
Document doc = DocumentHelper.parseText(xmlMsg);
|
||||
String msgType = doc.selectSingleNode("/xml/MsgType").getStringValue();
|
||||
if (msgType.equalsIgnoreCase(MessageType.event.name())) {
|
||||
msgType += "_"
|
||||
msgType += DECOLLATOR
|
||||
+ doc.selectSingleNode("/xml/Event").getStringValue();
|
||||
}
|
||||
return msgType.toLowerCase();
|
||||
|
||||
@ -27,7 +27,8 @@ public class AnnotationActionMapping extends AbstractActionMapping {
|
||||
actionMap = new HashMap<String, WeixinAction>();
|
||||
Set<Class<?>> weixinActions = ClassUtil.getClasses(actionPackage);
|
||||
for (Class<?> clazz : weixinActions) {
|
||||
ActionAnnotation action = clazz.getAnnotation(ActionAnnotation.class);
|
||||
ActionAnnotation action = clazz
|
||||
.getAnnotation(ActionAnnotation.class);
|
||||
if (action == null) {
|
||||
continue;
|
||||
}
|
||||
@ -41,9 +42,8 @@ public class AnnotationActionMapping extends AbstractActionMapping {
|
||||
EventType[] eventTypes = action.eventType();
|
||||
if (eventTypes != null && eventTypes.length > 0) {
|
||||
for (EventType e : eventTypes) {
|
||||
actionMap.put(
|
||||
(msgType.name() + "_" + e.name()).toLowerCase(),
|
||||
weixinAction);
|
||||
actionMap.put((msgType.name() + DECOLLATOR + e.name())
|
||||
.toLowerCase(), weixinAction);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -198,7 +198,6 @@ public class HttpRequest {
|
||||
.getFirstHeader(HttpHeaders.CONTENT_TYPE);
|
||||
Header disposition = httpResponse
|
||||
.getFirstHeader("Content-disposition");
|
||||
System.err.println(response.getAsString());
|
||||
// json
|
||||
if (contentType.getValue().contains(
|
||||
ContentType.APPLICATION_JSON.getMimeType())
|
||||
|
||||
@ -43,10 +43,10 @@ public class BaseMsg implements Serializable {
|
||||
*/
|
||||
@XStreamAlias("MsgId")
|
||||
private long msgId;
|
||||
@XStreamAlias("AgentID")
|
||||
/**
|
||||
* 企业号独有的应用ID
|
||||
*/
|
||||
@XStreamAlias("AgentID")
|
||||
private String agentId;
|
||||
|
||||
public BaseMsg() {
|
||||
|
||||
@ -41,6 +41,12 @@ public enum MessageType {
|
||||
* @see com.foxinmy.weixin4j.msg.VideoMessage
|
||||
*/
|
||||
video(VideoMessage.class),
|
||||
/**
|
||||
* 小视频消息
|
||||
*
|
||||
* @see com.foxinmy.weixin4j.msg.VideoMessage
|
||||
*/
|
||||
shortvideo(VideoMessage.class),
|
||||
/**
|
||||
* 位置消息
|
||||
*
|
||||
|
||||
@ -178,7 +178,7 @@ public class MessageUtil {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html">普通消息</a>
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html">事件触发</a>
|
||||
* href="http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html">事件消息</a>
|
||||
* @see com.foxinmy.weixin4j.type.MessageType
|
||||
* @see com.feican.weixin.msg.BaeMessage
|
||||
* @see com.foxinmy.weixin4j.msg.TextMessage
|
||||
|
||||
@ -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
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.VideoMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
/**
|
||||
* 小视频消息处理
|
||||
*
|
||||
* @className ShortvideoAction
|
||||
* @author jy
|
||||
* @date 2015年4月6日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.VideoMessage
|
||||
*/
|
||||
@ActionAnnotation(msgType = MessageType.shortvideo)
|
||||
public class ShortvideoAction extends DebugAction<VideoMessage> {
|
||||
|
||||
}
|
||||
@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user