1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.util.List;
4
5 import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject;
7 import com.foxinmy.weixin4j.exception.WeixinException;
8 import com.foxinmy.weixin4j.http.weixin.ApiResult;
9 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10 import com.foxinmy.weixin4j.model.Token;
11 import com.foxinmy.weixin4j.qy.message.ChatMessage;
12 import com.foxinmy.weixin4j.qy.model.ChatInfo;
13 import com.foxinmy.weixin4j.qy.model.ChatMute;
14 import com.foxinmy.weixin4j.qy.type.ChatType;
15 import com.foxinmy.weixin4j.token.TokenManager;
16 import com.foxinmy.weixin4j.tuple.ChatTuple;
17 import com.foxinmy.weixin4j.util.ObjectId;
18 import com.foxinmy.weixin4j.util.StringUtil;
19
20
21
22
23
24
25
26
27
28
29
30 public class ChatApi extends QyApi {
31 private final TokenManager tokenManager;
32
33 public ChatApi(TokenManager tokenManager) {
34 this.tokenManager = tokenManager;
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48 public String createChat(ChatInfo chatInfo) throws WeixinException {
49 String chatId = chatInfo.getChatId();
50 JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
51 if (StringUtil.isBlank(chatId)) {
52 chatId = ObjectId.get().toHexString();
53 obj.put("chatid", chatId);
54 }
55 String message_chat_create_uri = getRequestUri("message_chat_create_uri");
56 Token token = tokenManager.getCache();
57 weixinExecutor.post(
58 String.format(message_chat_create_uri, token.getAccessToken()),
59 obj.toJSONString());
60 return chatId;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74 public ChatInfo getChat(String chatId) throws WeixinException {
75 String message_chat_get_uri = getRequestUri("message_chat_get_uri");
76 Token token = tokenManager.getCache();
77 WeixinResponse response = weixinExecutor.get(String.format(
78 message_chat_get_uri, token.getAccessToken(), chatId));
79 return response.getAsJson().getObject("chat_info", ChatInfo.class);
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public ApiResult updateChat(ChatInfo chatInfo, String operator,
100 List<String> addUsers, List<String> deleteUsers)
101 throws WeixinException {
102 JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
103 obj.remove("userlist");
104 obj.put("op_user", operator);
105 obj.put("add_user_list", addUsers);
106 obj.put("del_user_list", deleteUsers);
107 String message_chat_update_uri = getRequestUri("message_chat_update_uri");
108 Token token = tokenManager.getCache();
109 WeixinResponse response = weixinExecutor.post(
110 String.format(message_chat_update_uri, token.getAccessToken()),
111 obj.toJSONString());
112 return response.getAsResult();
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public ApiResult quitChat(String chatId, String operator)
128 throws WeixinException {
129 JSONObject obj = new JSONObject();
130 obj.put("chatid", chatId);
131 obj.put("op_user", operator);
132 String message_chat_quit_uri = getRequestUri("message_chat_quit_uri");
133 Token token = tokenManager.getCache();
134 WeixinResponse response = weixinExecutor.post(
135 String.format(message_chat_quit_uri, token.getAccessToken()),
136 obj.toJSONString());
137 return response.getAsResult();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public ApiResult clearChatNotify(String targetId, String owner,
155 ChatType chatType) throws WeixinException {
156 JSONObject chat = new JSONObject();
157 chat.put("type", chatType.name());
158 chat.put("id", targetId);
159 String message_chat_clearnotify_uri = getRequestUri("message_chat_clearnotify_uri");
160 Token token = tokenManager.getCache();
161 WeixinResponse response = weixinExecutor.post(
162 String.format(message_chat_clearnotify_uri,
163 token.getAccessToken()),
164 String.format("{\"op_user\": \"%s\",\"chat\":%s", owner,
165 chat.toJSONString()));
166 return response.getAsResult();
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public List<String> setChatMute(List<ChatMute> chatMutes)
183 throws WeixinException {
184 JSONObject mute = new JSONObject();
185 mute.put("user_mute_list", chatMutes);
186 String message_chat_setmute_uri = getRequestUri("message_chat_setmute_uri");
187 Token token = tokenManager.getCache();
188 WeixinResponse response = weixinExecutor
189 .post(String.format(message_chat_setmute_uri,
190 token.getAccessToken()), mute.toJSONString());
191 return JSON.parseArray(response.getAsJson().getString("invaliduser"),
192 String.class);
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 public ApiResult sendChatMessage(ChatMessage message)
207 throws WeixinException {
208 ChatTuple tuple = message.getChatTuple();
209 String msgtype = tuple.getMessageType();
210 JSONObject msg = new JSONObject();
211 JSONObject receiver = new JSONObject();
212 receiver.put("id", message.getTargetId());
213 receiver.put("type", message.getChatType().name());
214 msg.put("receiver", receiver);
215 msg.put("sender", message.getSenderId());
216 msg.put("msgtype", msgtype);
217 msg.put(msgtype, tuple);
218 String message_chat_send_uri = getRequestUri("message_chat_send_uri");
219 Token token = tokenManager.getCache();
220 WeixinResponse response = weixinExecutor.post(
221 String.format(message_chat_send_uri, token.getAccessToken()),
222 msg.toJSONString());
223 return response.getAsResult();
224 }
225 }