调整了Tuple消息元件&新增视频上传接口&新增企业好聊天服务接口
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
package com.foxinmy.weixin4j.qy.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.message.ChatMessage;
|
||||
import com.foxinmy.weixin4j.qy.model.ChatInfo;
|
||||
import com.foxinmy.weixin4j.qy.model.ChatMute;
|
||||
import com.foxinmy.weixin4j.qy.type.ChatType;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.tuple.ChatTuple;
|
||||
import com.foxinmy.weixin4j.util.ObjectId;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 聊天服务接口
|
||||
*
|
||||
* @className ChatApi
|
||||
* @author jy
|
||||
* @date 2015年7月31日
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%9C%8D%E5%8A%A1">企业号消息服务</a>
|
||||
*/
|
||||
public class ChatApi extends QyApi {
|
||||
private final TokenHolder tokenHolder;
|
||||
|
||||
public ChatApi(TokenHolder tokenHolder) {
|
||||
this.tokenHolder = tokenHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建会话 <font color="red">如果会话id为空,程序会自动生成一个唯一ID</font>
|
||||
*
|
||||
* @param chatInfo
|
||||
* 会话信息
|
||||
* @return 会话ID
|
||||
* @see com.foxinmy.weixin4j.qy.model.ChatInfo
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E5.88.9B.E5.BB.BA.E4.BC.9A.E8.AF.9D">创建会话</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String createChat(ChatInfo chatInfo) throws WeixinException {
|
||||
String chatId = chatInfo.getChatId();
|
||||
JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
|
||||
if (StringUtil.isBlank(chatId)) {
|
||||
chatId = ObjectId.get().toHexString();
|
||||
obj.put("chatid", chatId);
|
||||
}
|
||||
String message_chat_create_uri = getRequestUri("message_chat_create_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
weixinClient.post(
|
||||
String.format(message_chat_create_uri, token.getAccessToken()),
|
||||
obj.toJSONString());
|
||||
return chatId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话
|
||||
*
|
||||
* @param chatId
|
||||
* 会话ID
|
||||
* @return 会话信息
|
||||
* @see com.foxinmy.weixin4j.qy.model.ChatInfo
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.9A.E8.AF.9D">获取会话</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public ChatInfo getChat(String chatId) throws WeixinException {
|
||||
String message_chat_get_uri = getRequestUri("message_chat_get_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.get(String.format(
|
||||
message_chat_get_uri, token.getAccessToken(), chatId));
|
||||
return response.getAsJson().getObject("chat_info", ChatInfo.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会话
|
||||
*
|
||||
* @param chatInfo
|
||||
* 会话信息 至少保持会话ID不能为空
|
||||
* @param operator
|
||||
* 操作人userid
|
||||
* @param addUsers
|
||||
* 会话新增成员列表
|
||||
* @param deleteUsers
|
||||
* 会话退出成员列表
|
||||
* @return 处理结果
|
||||
* @see com.foxinmy.weixin4j.qy.model.ChatInfo
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E4.BF.AE.E6.94.B9.E4.BC.9A.E8.AF.9D.E4.BF.A1.E6.81.AF">修改会话信息</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult updateChat(ChatInfo chatInfo, String operator,
|
||||
List<String> addUsers, List<String> deleteUsers)
|
||||
throws WeixinException {
|
||||
JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
|
||||
obj.remove("userlist");
|
||||
obj.put("op_user", operator);
|
||||
obj.put("add_user_list", addUsers);
|
||||
obj.put("del_user_list", deleteUsers);
|
||||
String message_chat_update_uri = getRequestUri("message_chat_update_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(message_chat_update_uri, token.getAccessToken()),
|
||||
obj.toJSONString());
|
||||
return response.getAsJsonResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出会话
|
||||
*
|
||||
* @param chatId
|
||||
* 会话ID
|
||||
* @param operator
|
||||
* 操作人userid
|
||||
* @return 处理结果
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E9.80.80.E5.87.BA.E4.BC.9A.E8.AF.9D">退出会话</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult quitChat(String chatId, String operator)
|
||||
throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("chatid", chatId);
|
||||
obj.put("op_user", operator);
|
||||
String message_chat_quit_uri = getRequestUri("message_chat_quit_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(message_chat_quit_uri, token.getAccessToken()),
|
||||
obj.toJSONString());
|
||||
return response.getAsJsonResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除会话未读状态
|
||||
*
|
||||
* @param v
|
||||
* 会话值,为userid|chatid,分别表示:成员id|会话id
|
||||
* @param owner
|
||||
* 会话所有者的userid
|
||||
* @param chatType
|
||||
* 会话类型:single|group,分别表示:群聊|单聊
|
||||
* @return 处理结果
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E6.B8.85.E9.99.A4.E4.BC.9A.E8.AF.9D.E6.9C.AA.E8.AF.BB.E7.8A.B6.E6.80.81">清除会话未读状态</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult clearNotify(String targetId, String owner,
|
||||
ChatType chatType) throws WeixinException {
|
||||
JSONObject chat = new JSONObject();
|
||||
chat.put("type", chatType.name());
|
||||
chat.put("id", targetId);
|
||||
String message_chat_clearnotify_uri = getRequestUri("message_chat_clearnotify_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(message_chat_clearnotify_uri,
|
||||
token.getAccessToken()),
|
||||
String.format("{\"op_user\": \"%s\",\"chat\":%s", owner,
|
||||
chat.toJSONString()));
|
||||
return response.getAsJsonResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置成员接收到的消息是否提醒。主要场景是用于对接企业im的在线状态,如成员处于在线状态时,可以设置该成员的消息免打扰。当成员离线时,关闭免打扰状态
|
||||
* ,对微信端进行提醒。
|
||||
*
|
||||
* @param chatMutes
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.AE.BE.E7.BD.AE.E6.88.90.E5.91.98.E6.96.B0.E6.B6.88.E6.81.AF.E5.85.8D.E6.89.93.E6.89.B0"
|
||||
* >设置成员新消息免打扰</a>
|
||||
* @return 列表中不存在的成员,剩余合法成员会继续执行。
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public List<String> setMute(List<ChatMute> chatMutes)
|
||||
throws WeixinException {
|
||||
JSONObject mute = new JSONObject();
|
||||
mute.put("user_mute_list", chatMutes);
|
||||
String message_chat_setmute_uri = getRequestUri("message_chat_setmute_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient
|
||||
.post(String.format(message_chat_setmute_uri,
|
||||
token.getAccessToken()), mute.toJSONString());
|
||||
return JSON.parseArray(response.getAsJson().getString("invaliduser"),
|
||||
String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param message
|
||||
* 消息对象
|
||||
* @return 处理结果
|
||||
* @see com.foxinmy.weixin4j.qy.message.ChatMessage
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E5.8F.91.E6.B6.88.E6.81.AF">发送消息</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult sendMessage(ChatMessage message) throws WeixinException {
|
||||
ChatTuple tuple = message.getChatTuple();
|
||||
String msgtype = tuple.getMessageType();
|
||||
JSONObject msg = new JSONObject();
|
||||
JSONObject receiver = new JSONObject();
|
||||
receiver.put("id", message.getTargetId());
|
||||
receiver.put("type", message.getChatType().name());
|
||||
msg.put("receiver", receiver);
|
||||
msg.put("sender", message.getSenderId());
|
||||
msg.put("msgtype", msgtype);
|
||||
msg.put(msgtype, tuple);
|
||||
String message_chat_send_uri = getRequestUri("message_chat_send_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(message_chat_send_uri, token.getAccessToken()),
|
||||
msg.toJSONString());
|
||||
return response.getAsJsonResult();
|
||||
}
|
||||
}
|
||||
@@ -120,4 +120,19 @@ material_media_del_uri={api_base_url}/material/del?access_token=%s&media_id=%s&a
|
||||
# \u83b7\u53d6\u5a92\u4f53\u7d20\u6750\u603b\u6570
|
||||
material_media_count_uri={api_base_url}/material/get_count?access_token=%s&agentid=%d
|
||||
# \u83b7\u53d6\u5a92\u4f53\u7d20\u6750\u5217\u8868
|
||||
material_media_list_uri={api_base_url}/material/batchget?access_token=%s
|
||||
material_media_list_uri={api_base_url}/material/batchget?access_token=%s
|
||||
|
||||
# \u6d88\u606f\u670d\u52a1-\u521b\u5efa\u4f1a\u8bdd
|
||||
message_chat_create_uri={api_base_url}/chat/create?access_token=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u83b7\u53d6\u4f1a\u8bdd
|
||||
message_chat_get_uri={api_base_url}/chat/get?access_token=%s&chatid=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u66f4\u65b0\u4f1a\u8bdd
|
||||
message_chat_update_uri={api_base_url}/chat/update?access_token=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u9000\u51fa\u4f1a\u8bdd
|
||||
message_chat_quit_uri={api_base_url}/chat/quit?access_token=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u6e05\u9664\u4f1a\u8bdd\u672a\u8bfb\u72b6\u6001
|
||||
message_chat_clearnotify_uri={api_base_url}/chat/clearnotify?access_token=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u8bbe\u7f6e\u6210\u5458\u65b0\u6d88\u606f\u514d\u6253\u6270
|
||||
message_chat_setmute_uri={api_base_url}/chat/setmute?access_token=%s
|
||||
# \u6d88\u606f\u670d\u52a1-\u53d1\u6d88\u606f
|
||||
message_chat_send_uri={api_base_url}/chat/send?access_token=%s
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.foxinmy.weixin4j.qy.message;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.foxinmy.weixin4j.qy.type.ChatType;
|
||||
import com.foxinmy.weixin4j.tuple.ChatTuple;
|
||||
|
||||
/**
|
||||
* 聊天消息对象
|
||||
*
|
||||
* @className ChatMessage
|
||||
* @author jy
|
||||
* @date 2015年8月1日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.tuple.Text
|
||||
* @see com.foxinmy.weixin4j.tuple.Image
|
||||
* @see com.foxinmy.weixin4j.tuple.File
|
||||
*/
|
||||
public class ChatMessage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -4973029130270955777L;
|
||||
|
||||
/**
|
||||
* 成员id|会话id
|
||||
*/
|
||||
private String targetId;
|
||||
/**
|
||||
* 群聊|单聊
|
||||
*/
|
||||
private ChatType chatType;
|
||||
/**
|
||||
* 发送人id
|
||||
*/
|
||||
private String senderId;
|
||||
/**
|
||||
* 消息对象
|
||||
*/
|
||||
private ChatTuple chatTuple;
|
||||
|
||||
public ChatMessage(String targetId, ChatType chatType, String senderId,
|
||||
ChatTuple chatTuple) {
|
||||
this.targetId = targetId;
|
||||
this.chatType = chatType;
|
||||
this.senderId = senderId;
|
||||
this.chatTuple = chatTuple;
|
||||
}
|
||||
|
||||
public String getTargetId() {
|
||||
return targetId;
|
||||
}
|
||||
|
||||
public ChatType getChatType() {
|
||||
return chatType;
|
||||
}
|
||||
|
||||
public String getSenderId() {
|
||||
return senderId;
|
||||
}
|
||||
|
||||
public ChatTuple getChatTuple() {
|
||||
return chatTuple;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatMessage [targetId=" + targetId + ", chatType=" + chatType
|
||||
+ ", senderId=" + senderId + ", chatTuple=" + chatTuple + "]";
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import com.foxinmy.weixin4j.qy.model.IdParameter;
|
||||
import com.foxinmy.weixin4j.tuple.NotifyTuple;
|
||||
|
||||
/**
|
||||
* 发送消息对象
|
||||
* 客服消息对象
|
||||
*
|
||||
* @className NotifyMessage
|
||||
* @author jy
|
||||
@@ -38,6 +38,7 @@ public class NotifyMessage implements Serializable {
|
||||
/**
|
||||
* 消息对象
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NotifyTuple tuple;
|
||||
/**
|
||||
* 发送对象
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.foxinmy.weixin4j.qy.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* 聊天会话信息
|
||||
*
|
||||
* @className ChatInfo
|
||||
* @author jy
|
||||
* @date 2015年7月31日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class ChatInfo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1899784347096501375L;
|
||||
/**
|
||||
* 会话id
|
||||
*/
|
||||
@JSONField(name = "chatid")
|
||||
private String chatId;
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 管理员userid
|
||||
*/
|
||||
private String owner;
|
||||
/**
|
||||
* 会话成员列表
|
||||
*/
|
||||
@JSONField(name = "userlist")
|
||||
private List<String> members;
|
||||
|
||||
public ChatInfo(String chatId, String name, String owner) {
|
||||
this(chatId, name, owner, null);
|
||||
}
|
||||
|
||||
@JSONCreator
|
||||
public ChatInfo(@JSONField(name = "chatId") String chatId,
|
||||
@JSONField(name = "name") String name,
|
||||
@JSONField(name = "owner") String owner,
|
||||
@JSONField(name = "userlist") List<String> members) {
|
||||
this.chatId = chatId;
|
||||
this.name = name;
|
||||
this.owner = owner;
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
public String getChatId() {
|
||||
return chatId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public List<String> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(List<String> members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatInfo [chatId=" + chatId + ", name=" + name + ", owner="
|
||||
+ owner + ", members=" + members + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.foxinmy.weixin4j.qy.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* 成员新消息免打扰
|
||||
*
|
||||
* @className ChatMute
|
||||
* @author jy
|
||||
* @date 2015年8月1日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class ChatMute implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6734443056426236273L;
|
||||
|
||||
private String userid;
|
||||
private int status;
|
||||
|
||||
/**
|
||||
* 默认关闭免打扰
|
||||
*
|
||||
* @param userid
|
||||
*/
|
||||
public ChatMute(String userid) {
|
||||
this.userid = userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入true时开启免打扰
|
||||
*
|
||||
* @param userid
|
||||
* 成员userid
|
||||
* @param status
|
||||
* 是否开启免打扰
|
||||
*/
|
||||
public ChatMute(String userid, boolean status) {
|
||||
this.userid = userid;
|
||||
this.status = status ? 1 : 0;
|
||||
}
|
||||
|
||||
public String getUserid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
@JSONField(deserialize = false)
|
||||
public boolean getFormatStatus() {
|
||||
return status == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatMute [userid=" + userid + ", status=" + getFormatStatus()
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.foxinmy.weixin4j.qy.type;
|
||||
|
||||
/**
|
||||
* 会话类型
|
||||
*
|
||||
* @className ChatType
|
||||
* @author jy
|
||||
* @date 2015年7月31日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public enum ChatType {
|
||||
/**
|
||||
* 单聊
|
||||
*/
|
||||
single,
|
||||
/**
|
||||
* 群聊
|
||||
*/
|
||||
group
|
||||
}
|
||||
Reference in New Issue
Block a user