1 | ;
|
2 |
|
3 | var path = require('path');
|
4 | var fs = require('fs');
|
5 | var formstream = require('formstream');
|
6 |
|
7 | var util = require('./util');
|
8 | var wrapper = util.wrapper;
|
9 | var postJSON = util.postJSON;
|
10 | var make = util.make;
|
11 |
|
12 | /**
|
13 | * 获取客服聊天记录
|
14 | * 详细请看:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN
|
15 | *
|
16 | * Opts:
|
17 | * ```
|
18 | * {
|
19 | * "starttime" : 123456789, 起始时间,unix时间戳
|
20 | * "endtime" : 987654321, 结束时间,unix时间戳,每次查询时段不能超过24小时
|
21 | * "msgid" : 1, 消息id顺序从小到大,从1开始
|
22 | * "number" : 10000 每次获取条数,最多10000条
|
23 | * }
|
24 | * ```
|
25 | * Examples:
|
26 | * ```
|
27 | * api.getRecords(opts, callback);
|
28 | * ```
|
29 | * Callback:
|
30 | *
|
31 | * - `err`, 调用失败时得到的异常
|
32 | * - `result`, 调用正常时得到的对象
|
33 | *
|
34 | * Result:
|
35 | * ```
|
36 | * {
|
37 | * "recordlist": [
|
38 | * {
|
39 | * "worker": " test1",
|
40 | * "openid": "oDF3iY9WMaswOPWjCIp_f3Bnpljk",
|
41 | * "opercode": 2002,
|
42 | * "time": 1400563710,
|
43 | * "text": " 您好,客服test1为您服务。"
|
44 | * },
|
45 | * {
|
46 | * "worker": " test1",
|
47 | * "openid": "oDF3iY9WMaswOPWjCIp_f3Bnpljk",
|
48 | * "opercode": 2003,
|
49 | * "time": 1400563731,
|
50 | * "text": " 你好,有什么事情? "
|
51 | * },
|
52 | * ]
|
53 | * }
|
54 | * ```
|
55 | * @param {Object} opts 查询条件
|
56 | * @param {Function} callback 回调函数
|
57 | */
|
58 | make(exports, 'getRecords', function (opts, callback) {
|
59 | // https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
|
60 | opts.msgid = opts.msgid || 1;
|
61 | var url = this.endpoint + '/customservice/msgrecord/getmsglist?access_token=' + this.token.accessToken;
|
62 | this.request(url, postJSON(opts), wrapper(callback));
|
63 | });
|
64 |
|
65 | /**
|
66 | * 获取客服基本信息
|
67 | * 详细请看:http://dkf.qq.com/document-3_1.html
|
68 | *
|
69 | * Examples:
|
70 | * ```
|
71 | * api.getCustomServiceList(callback);
|
72 | * ```
|
73 | * Callback:
|
74 | *
|
75 | * - `err`, 调用失败时得到的异常
|
76 | * - `result`, 调用正常时得到的对象
|
77 | *
|
78 | * Result:
|
79 | * ```
|
80 | * {
|
81 | * "kf_list": [
|
82 | * {
|
83 | * "kf_account": "test1@test",
|
84 | * "kf_nick": "ntest1",
|
85 | * "kf_id": "1001"
|
86 | * },
|
87 | * {
|
88 | * "kf_account": "test2@test",
|
89 | * "kf_nick": "ntest2",
|
90 | * "kf_id": "1002"
|
91 | * },
|
92 | * {
|
93 | * "kf_account": "test3@test",
|
94 | * "kf_nick": "ntest3",
|
95 | * "kf_id": "1003"
|
96 | * }
|
97 | * ]
|
98 | * }
|
99 | * ```
|
100 | * @param {Function} callback 回调函数
|
101 | */
|
102 | make(exports, 'getCustomServiceList', function (callback) {
|
103 | // https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token= ACCESS_TOKEN
|
104 | var url = this.endpoint + '/cgi-bin/customservice/getkflist?access_token=' + this.token.accessToken;
|
105 | this.request(url, {dataType: 'json'}, wrapper(callback));
|
106 | });
|
107 |
|
108 | /**
|
109 | * 获取在线客服接待信息
|
110 | * 详细请看:http://dkf.qq.com/document-3_2.html
|
111 | *
|
112 | * Examples:
|
113 | * ```
|
114 | * api.getOnlineCustomServiceList(callback);
|
115 | * ```
|
116 | *
|
117 | * Callback:
|
118 | *
|
119 | * - `err`, 调用失败时得到的异常
|
120 | * - `result`, 调用正常时得到的对象
|
121 | *
|
122 | * Result:
|
123 | * ```
|
124 | * {
|
125 | * "kf_online_list": [
|
126 | * {
|
127 | * "kf_account": "test1@test",
|
128 | * "status": 1,
|
129 | * "kf_id": "1001",
|
130 | * "auto_accept": 0,
|
131 | * "accepted_case": 1
|
132 | * },
|
133 | * {
|
134 | * "kf_account": "test2@test",
|
135 | * "status": 1,
|
136 | * "kf_id": "1002",
|
137 | * "auto_accept": 0,
|
138 | * "accepted_case": 2
|
139 | * }
|
140 | * ]
|
141 | * }
|
142 | * ```
|
143 | * @param {Function} callback 回调函数
|
144 | */
|
145 | make(exports, 'getOnlineCustomServiceList', function (callback) {
|
146 | // https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token= ACCESS_TOKEN
|
147 | var url = this.endpoint + '/cgi-bin/customservice/getonlinekflist?access_token=' + this.token.accessToken;
|
148 | this.request(url, {dataType: 'json'}, wrapper(callback));
|
149 | });
|
150 |
|
151 | var md5 = function (input) {
|
152 | var crypto = require('crypto');
|
153 | var hash = crypto.createHash('md5');
|
154 | return hash.update(input).digest('hex');
|
155 | };
|
156 |
|
157 | /**
|
158 | * 添加客服账号
|
159 | * 详细请看:http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html
|
160 | *
|
161 | * Examples:
|
162 | * ```
|
163 | * api.addKfAccount('test@test', 'nickname', 'password', callback);
|
164 | * ```
|
165 | *
|
166 | * Callback:
|
167 | *
|
168 | * - `err`, 调用失败时得到的异常
|
169 | * - `result`, 调用正常时得到的对象
|
170 | *
|
171 | * Result:
|
172 | * ```
|
173 | * {
|
174 | * "errcode" : 0,
|
175 | * "errmsg" : "ok",
|
176 | * }
|
177 | * ```
|
178 | * @param {String} account 账号名字,格式为:前缀@公共号名字
|
179 | * @param {String} nick 昵称
|
180 | * @param {String} password 密码,可以直接传递明文,wechat模块自动进行md5加密
|
181 | * @param {Function} callback 回调函数
|
182 | */
|
183 | make(exports, 'addKfAccount', function (account, nick, password, callback) {
|
184 | // https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
|
185 | var url = this.endpoint + '/customservice/kfaccount/add?access_token=' + this.token.accessToken;
|
186 | var data = {
|
187 | 'kf_account': account,
|
188 | 'nickname': nick,
|
189 | 'password': md5(password)
|
190 | };
|
191 |
|
192 | this.request(url, postJSON(data), wrapper(callback));
|
193 | });
|
194 |
|
195 | /**
|
196 | * 设置客服账号
|
197 | * 详细请看:http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html
|
198 | *
|
199 | * Examples:
|
200 | * ```
|
201 | * api.updateKfAccount('test@test', 'nickname', 'password', callback);
|
202 | * ```
|
203 | *
|
204 | * Callback:
|
205 | *
|
206 | * - `err`, 调用失败时得到的异常
|
207 | * - `result`, 调用正常时得到的对象
|
208 | *
|
209 | * Result:
|
210 | * ```
|
211 | * {
|
212 | * "errcode" : 0,
|
213 | * "errmsg" : "ok",
|
214 | * }
|
215 | * ```
|
216 | * @param {String} account 账号名字,格式为:前缀@公共号名字
|
217 | * @param {String} nick 昵称
|
218 | * @param {String} password 密码,可以直接传递明文,wechat模块自动进行md5加密
|
219 | * @param {Function} callback 回调函数
|
220 | */
|
221 | make(exports, 'updateKfAccount', function (account, nick, password, callback) {
|
222 | // https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
|
223 | var url = this.endpoint + '/customservice/kfaccount/update?access_token=' + this.token.accessToken;
|
224 | var data = {
|
225 | 'kf_account': account,
|
226 | 'nickname': nick,
|
227 | 'password': md5(password)
|
228 | };
|
229 |
|
230 | this.request(url, postJSON(data), wrapper(callback));
|
231 | });
|
232 |
|
233 | /**
|
234 | * 删除客服账号
|
235 | * 详细请看:http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html
|
236 | *
|
237 | * Examples:
|
238 | * ```
|
239 | * api.deleteKfAccount('test@test', callback);
|
240 | * ```
|
241 | *
|
242 | * Callback:
|
243 | *
|
244 | * - `err`, 调用失败时得到的异常
|
245 | * - `result`, 调用正常时得到的对象
|
246 | *
|
247 | * Result:
|
248 | * ```
|
249 | * {
|
250 | * "errcode" : 0,
|
251 | * "errmsg" : "ok",
|
252 | * }
|
253 | * ```
|
254 | * @param {String} account 账号名字,格式为:前缀@公共号名字
|
255 | * @param {Function} callback 回调函数
|
256 | */
|
257 | make(exports, 'deleteKfAccount', function (account, callback) {
|
258 | // https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN
|
259 | var url = this.endpoint + '/customservice/kfaccount/del?access_token=' + this.token.accessToken + '&kf_account=' + account;
|
260 | this.request(url, {dataType: 'json'}, wrapper(callback));
|
261 | });
|
262 |
|
263 | /**
|
264 | * 设置客服头像
|
265 | * 详细请看:http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html
|
266 | *
|
267 | * Examples:
|
268 | * ```
|
269 | * api.setKfAccountAvatar('test@test', '/path/to/avatar.png', callback);
|
270 | * ```
|
271 | *
|
272 | * Callback:
|
273 | *
|
274 | * - `err`, 调用失败时得到的异常
|
275 | * - `result`, 调用正常时得到的对象
|
276 | *
|
277 | * Result:
|
278 | * ```
|
279 | * {
|
280 | * "errcode" : 0,
|
281 | * "errmsg" : "ok",
|
282 | * }
|
283 | * ```
|
284 | * @param {String} account 账号名字,格式为:前缀@公共号名字
|
285 | * @param {String} filepath 头像路径
|
286 | * @param {Function} callback 回调函数
|
287 | */
|
288 | make(exports, 'setKfAccountAvatar', function (account, filepath, callback) {
|
289 | // http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT
|
290 | var that = this;
|
291 | fs.stat(filepath, function (err, stat) {
|
292 | if (err) {
|
293 | return callback(err);
|
294 | }
|
295 | var form = formstream();
|
296 | form.file('media', filepath, path.basename(filepath), stat.size);
|
297 | var url = this.endpoint + '/customservice/kfaccount/uploadheadimg?access_token=' + that.token.accessToken + '&kf_account=' + account;
|
298 | var opts = {
|
299 | dataType: 'json',
|
300 | type: 'POST',
|
301 | timeout: 60000, // 60秒超时
|
302 | headers: form.headers(),
|
303 | stream: form
|
304 | };
|
305 | that.request(url, opts, wrapper(callback));
|
306 | });
|
307 | });
|