1 | ;
|
2 |
|
3 | var util = require('./util');
|
4 | var wrapper = util.wrapper;
|
5 | var postJSON = util.postJSON;
|
6 |
|
7 | /**
|
8 | * 客服消息,发送文字消息
|
9 | * 详细细节 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
|
10 | * Examples:
|
11 | * ```
|
12 | * api.sendText('openid', 'Hello world', callback);
|
13 | * ```
|
14 | * Callback:
|
15 | *
|
16 | * - `err`, 调用失败时得到的异常
|
17 | * - `result`, 调用正常时得到的对象
|
18 | *
|
19 | * @param {String} openid 用户的openid
|
20 | * @param {String} text 发送的消息内容
|
21 | * @param {Function} callback 回调函数
|
22 | */
|
23 | exports.sendText = function (openid, text, callback) {
|
24 | this.preRequest(this._sendText, arguments);
|
25 | };
|
26 |
|
27 | /*!
|
28 | * 客服消息,发送文字消息的未封装版本
|
29 | */
|
30 | exports._sendText = function (openid, text, callback) {
|
31 | // {
|
32 | // "touser":"OPENID",
|
33 | // "msgtype":"text",
|
34 | // "text": {
|
35 | // "content":"Hello World"
|
36 | // }
|
37 | // }
|
38 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
39 | var data = {
|
40 | 'touser': openid,
|
41 | 'msgtype': 'text',
|
42 | 'text': {
|
43 | 'content': text
|
44 | }
|
45 | };
|
46 | this.request(url, postJSON(data), wrapper(callback));
|
47 | };
|
48 |
|
49 | /**
|
50 | * 客服消息,发送图片消息
|
51 | * 详细细节 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
|
52 | * Examples:
|
53 | * ```
|
54 | * api.sendImage('openid', 'media_id', callback);
|
55 | * ```
|
56 | * Callback:
|
57 | *
|
58 | * - `err`, 调用失败时得到的异常
|
59 | * - `result`, 调用正常时得到的对象
|
60 | *
|
61 | * @param {String} openid 用户的openid
|
62 | * @param {String} mediaId 媒体文件的ID,参见uploadMedia方法
|
63 | * @param {Function} callback 回调函数
|
64 | */
|
65 | exports.sendImage = function (openid, mediaId, callback) {
|
66 | this.preRequest(this._sendImage, arguments);
|
67 | };
|
68 |
|
69 | /*!
|
70 | * 客服消息,发送图片消息的未封装版本
|
71 | */
|
72 | exports._sendImage = function (openid, mediaId, callback) {
|
73 | // {
|
74 | // "touser":"OPENID",
|
75 | // "msgtype":"image",
|
76 | // "image": {
|
77 | // "media_id":"MEDIA_ID"
|
78 | // }
|
79 | // }
|
80 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
81 | var data = {
|
82 | 'touser': openid,
|
83 | 'msgtype':'image',
|
84 | 'image': {
|
85 | 'media_id': mediaId
|
86 | }
|
87 | };
|
88 | this.request(url, postJSON(data), wrapper(callback));
|
89 | };
|
90 |
|
91 | /**
|
92 | * 客服消息,发送语音消息
|
93 | * 详细细节 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
|
94 | * Examples:
|
95 | * ```
|
96 | * api.sendVoice('openid', 'media_id', callback);
|
97 | * ```
|
98 | * Callback:
|
99 | *
|
100 | * - `err`, 调用失败时得到的异常
|
101 | * - `result`, 调用正常时得到的对象
|
102 | *
|
103 | * @param {String} openid 用户的openid
|
104 | * @param {String} mediaId 媒体文件的ID
|
105 | * @param {Function} callback 回调函数
|
106 | */
|
107 | exports.sendVoice = function (openid, mediaId, callback) {
|
108 | this.preRequest(this._sendVoice, arguments);
|
109 | };
|
110 |
|
111 | /*!
|
112 | * 客服消息,发送语音消息的未封装版本
|
113 | */
|
114 | exports._sendVoice = function (openid, mediaId, callback) {
|
115 | // {
|
116 | // "touser":"OPENID",
|
117 | // "msgtype":"voice",
|
118 | // "voice": {
|
119 | // "media_id":"MEDIA_ID"
|
120 | // }
|
121 | // }
|
122 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
123 | var data = {
|
124 | 'touser': openid,
|
125 | 'msgtype': 'voice',
|
126 | 'voice': {
|
127 | 'media_id': mediaId
|
128 | }
|
129 | };
|
130 | this.request(url, postJSON(data), wrapper(callback));
|
131 | };
|
132 |
|
133 | /**
|
134 | * 客服消息,发送视频消息
|
135 | * 详细细节 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
|
136 | * Examples:
|
137 | * ```
|
138 | * api.sendVideo('openid', 'media_id', 'thumb_media_id', callback);
|
139 | * ```
|
140 | * Callback:
|
141 | *
|
142 | * - `err`, 调用失败时得到的异常
|
143 | * - `result`, 调用正常时得到的对象
|
144 | *
|
145 | * @param {String} openid 用户的openid
|
146 | * @param {String} mediaId 媒体文件的ID
|
147 | * @param {String} thumbMediaId 缩略图文件的ID
|
148 | * @param {Function} callback 回调函数
|
149 | */
|
150 | exports.sendVideo = function (openid, mediaId, thumbMediaId, callback) {
|
151 | this.preRequest(this._sendVideo, arguments);
|
152 | };
|
153 |
|
154 | /*!
|
155 | * 客服消息,发送视频消息的未封装版本
|
156 | */
|
157 | exports._sendVideo = function (openid, mediaId, thumbMediaId, callback) {
|
158 | // {
|
159 | // "touser":"OPENID",
|
160 | // "msgtype":"video",
|
161 | // "video": {
|
162 | // "media_id":"MEDIA_ID"
|
163 | // "thumb_media_id":"THUMB_MEDIA_ID"
|
164 | // }
|
165 | // }
|
166 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
167 | var data = {
|
168 | 'touser': openid,
|
169 | 'msgtype':'video',
|
170 | 'video': {
|
171 | 'media_id': mediaId,
|
172 | 'thumb_media_id': thumbMediaId
|
173 | }
|
174 | };
|
175 | this.request(url, postJSON(data), wrapper(callback));
|
176 | };
|
177 |
|
178 | /**
|
179 | * 客服消息,发送音乐消息
|
180 | * 详细细节 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
|
181 | * Examples:
|
182 | * ```
|
183 | * var music = {
|
184 | * title: '音乐标题', // 可选
|
185 | * description: '描述内容', // 可选
|
186 | * musicurl: 'http://url.cn/xxx', 音乐文件地址
|
187 | * hqmusicurl: "HQ_MUSIC_URL",
|
188 | * thumb_media_id: "THUMB_MEDIA_ID"
|
189 | * };
|
190 | * api.sendMusic('openid', music, callback);
|
191 | * ```
|
192 | * Callback:
|
193 | *
|
194 | * - `err`, 调用失败时得到的异常
|
195 | * - `result`, 调用正常时得到的对象
|
196 | *
|
197 | * @param {String} openid 用户的openid
|
198 | * @param {Object} music 音乐文件
|
199 | * @param {Function} callback 回调函数
|
200 | */
|
201 | exports.sendMusic = function (openid, music, callback) {
|
202 | this.preRequest(this._sendMusic, arguments);
|
203 | };
|
204 |
|
205 | /*!
|
206 | * 客服消息,发送音乐消息的未封装版本
|
207 | */
|
208 | exports._sendMusic = function (openid, music, callback) {
|
209 | // {
|
210 | // "touser":"OPENID",
|
211 | // "msgtype":"music",
|
212 | // "music": {
|
213 | // "title":"MUSIC_TITLE", // 可选
|
214 | // "description":"MUSIC_DESCRIPTION", // 可选
|
215 | // "musicurl":"MUSIC_URL",
|
216 | // "hqmusicurl":"HQ_MUSIC_URL",
|
217 | // "thumb_media_id":"THUMB_MEDIA_ID"
|
218 | // }
|
219 | // }
|
220 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
221 | var data = {
|
222 | 'touser': openid,
|
223 | 'msgtype':'music',
|
224 | 'music': music
|
225 | };
|
226 | this.request(url, postJSON(data), wrapper(callback));
|
227 | };
|
228 |
|
229 | /**
|
230 | * 客服消息,发送图文消息(点击跳转到外链)
|
231 | * 详细细节 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
|
232 | * Examples:
|
233 | * ```
|
234 | * var articles = [
|
235 | * {
|
236 | * "title":"Happy Day",
|
237 | * "description":"Is Really A Happy Day",
|
238 | * "url":"URL",
|
239 | * "picurl":"PIC_URL"
|
240 | * },
|
241 | * {
|
242 | * "title":"Happy Day",
|
243 | * "description":"Is Really A Happy Day",
|
244 | * "url":"URL",
|
245 | * "picurl":"PIC_URL"
|
246 | * }];
|
247 | * api.sendNews('openid', articles, callback);
|
248 | * ```
|
249 | * Callback:
|
250 | *
|
251 | * - `err`, 调用失败时得到的异常
|
252 | * - `result`, 调用正常时得到的对象
|
253 | *
|
254 | * @param {String} openid 用户的openid
|
255 | * @param {Array} articles 图文列表
|
256 | * @param {Function} callback 回调函数
|
257 | */
|
258 | exports.sendNews = function (openid, articles, callback) {
|
259 | this.preRequest(this._sendNews, arguments);
|
260 | };
|
261 |
|
262 | /*!
|
263 | * 客服消息,发送图文消息(点击跳转到外链)的未封装版本
|
264 | */
|
265 | exports._sendNews = function (openid, articles, callback) {
|
266 | // {
|
267 | // "touser":"OPENID",
|
268 | // "msgtype":"news",
|
269 | // "news":{
|
270 | // "articles": [
|
271 | // {
|
272 | // "title":"Happy Day",
|
273 | // "description":"Is Really A Happy Day",
|
274 | // "url":"URL",
|
275 | // "picurl":"PIC_URL"
|
276 | // },
|
277 | // {
|
278 | // "title":"Happy Day",
|
279 | // "description":"Is Really A Happy Day",
|
280 | // "url":"URL",
|
281 | // "picurl":"PIC_URL"
|
282 | // }]
|
283 | // }
|
284 | // }
|
285 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
286 | var data = {
|
287 | 'touser': openid,
|
288 | 'msgtype':'news',
|
289 | 'news': {
|
290 | 'articles': articles
|
291 | }
|
292 | };
|
293 | this.request(url, postJSON(data), wrapper(callback));
|
294 | };
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | /**
|
300 | * 客服消息,发送图文消息(点击跳转到图文消息页面)
|
301 | * 详细细节 http://mp.weixin.qq.com/wiki/14/d9be34fe03412c92517da10a5980e7ee.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF
|
302 | * Examples:
|
303 | * ```
|
304 | * api.sendMpNews('openid', 'mediaId' , callback);
|
305 | * ```
|
306 | * Callback:
|
307 | *
|
308 | * - `err`, 调用失败时得到的异常
|
309 | * - `result`, 调用正常时得到的对象
|
310 | *
|
311 | * @param {String} openid 用户的openid
|
312 | * @param {String} mediaId 图文消息的id
|
313 | * @param {Function} callback 回调函数
|
314 | */
|
315 | exports.sendMpNews = function (openid, mediaId, callback) {
|
316 | this.preRequest(this._sendMpNews, arguments);
|
317 | };
|
318 |
|
319 | /*!
|
320 | * 客服消息,发送图文消息(点击跳转到图文消息页面) 的未封装版本
|
321 | */
|
322 | exports._sendMpNews = function (openid, mediaId, callback) {
|
323 | //{
|
324 | // "touser":"OPENID",
|
325 | // "msgtype":"mpnews",
|
326 | // "mpnews":
|
327 | // {
|
328 | // "media_id":"MEDIA_ID"
|
329 | // }
|
330 | //}
|
331 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
332 | var data = {
|
333 | 'touser': openid,
|
334 | 'msgtype':'mpnews',
|
335 | 'mpnews': {
|
336 | 'media_id': mediaId
|
337 | }
|
338 | };
|
339 | this.request(url, postJSON(data), wrapper(callback));
|
340 | };
|
341 |
|
342 | /**
|
343 | * 客服消息,发送卡卷消息
|
344 | * 详细细节 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
|
345 | * Examples:
|
346 | * ```
|
347 | * api.sendCard('openid', 'card', callback);
|
348 | * ```
|
349 | * Callback:
|
350 | *
|
351 | * - `err`, 调用失败时得到的异常
|
352 | * - `result`, 调用正常时得到的对象
|
353 | *
|
354 | * @param {String} openid 用户的openid
|
355 | * @param {Object} wxcard 卡卷相关信息
|
356 | * @param {Function} callback 回调函数
|
357 | */
|
358 | exports.sendCard = function (openid, card, callback) {
|
359 | this.preRequest(this._sendCard, arguments);
|
360 | };
|
361 |
|
362 | /*!
|
363 | * 客服消息,发送卡卷消息的未封装版本
|
364 | */
|
365 | exports._sendCard = function (openid, card, callback) {
|
366 | var url = this.endpoint + '/cgi-bin/message/custom/send?access_token=' + this.token.accessToken;
|
367 | var that = this;
|
368 | this.getCardExt(card, function (err, result) {
|
369 | var data = {
|
370 | 'touser': openid,
|
371 | 'msgtype':'wxcard',
|
372 | 'wxcard': {
|
373 | 'card_id': card.card_id,
|
374 | 'card_ext': result
|
375 | }
|
376 | };
|
377 | that.request(url, postJSON(data), wrapper(callback));
|
378 | });
|
379 | };
|