|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
10 |
|
from __future__ import absolute_import, unicode_literals |
|
3
|
|
|
|
|
4
|
10 |
|
import re |
|
5
|
|
|
|
|
6
|
10 |
|
import six |
|
7
|
10 |
|
from optionaldict import optionaldict |
|
8
|
|
|
|
|
9
|
10 |
|
from wechatpy.client.api.base import BaseWeChatAPI |
|
10
|
10 |
|
from wechatpy.utils import random_string |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
10 |
|
class WeChatMessage(BaseWeChatAPI): |
|
14
|
|
|
|
|
15
|
10 |
|
OPENID_RE = re.compile(r'^[\w\-]{28}$', re.I) |
|
16
|
|
|
|
|
17
|
10 |
|
def _send_custom_message(self, data, account=None): |
|
18
|
10 |
|
data = data or {} |
|
19
|
10 |
|
if account: |
|
20
|
10 |
|
data['customservice'] = {'kf_account': account} |
|
21
|
10 |
|
return self._post( |
|
22
|
|
|
'message/custom/send', |
|
23
|
|
|
data=data |
|
24
|
|
|
) |
|
25
|
|
|
|
|
26
|
10 |
|
def send_text(self, user_id, content, account=None): |
|
27
|
|
|
""" |
|
28
|
|
|
发送文本消息 |
|
29
|
|
|
|
|
30
|
|
|
详情请参考 |
|
31
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
32
|
|
|
|
|
33
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
34
|
|
|
:param content: 消息正文 |
|
35
|
|
|
:param account: 可选,客服账号 |
|
36
|
|
|
:return: 返回的 JSON 数据包 |
|
37
|
|
|
|
|
38
|
|
|
使用示例:: |
|
39
|
|
|
|
|
40
|
|
|
from wechatpy import WeChatClient |
|
41
|
|
|
|
|
42
|
|
|
client = WeChatClient('appid', 'secret') |
|
43
|
|
|
res = client.message.send_text('openid', 'text') |
|
44
|
|
|
|
|
45
|
|
|
""" |
|
46
|
10 |
|
data = { |
|
47
|
|
|
'touser': user_id, |
|
48
|
|
|
'msgtype': 'text', |
|
49
|
|
|
'text': {'content': content} |
|
50
|
|
|
} |
|
51
|
10 |
|
return self._send_custom_message(data, account=account) |
|
52
|
|
|
|
|
53
|
10 |
|
def send_image(self, user_id, media_id, account=None): |
|
54
|
|
|
""" |
|
55
|
|
|
发送图片消息 |
|
56
|
|
|
|
|
57
|
|
|
详情请参考 |
|
58
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
59
|
|
|
|
|
60
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
61
|
|
|
:param media_id: 图片的媒体ID。 可以通过 :func:`upload_media` 上传。 |
|
62
|
|
|
:param account: 可选,客服账号 |
|
63
|
|
|
:return: 返回的 JSON 数据包 |
|
64
|
|
|
|
|
65
|
|
|
使用示例:: |
|
66
|
|
|
|
|
67
|
|
|
from wechatpy import WeChatClient |
|
68
|
|
|
|
|
69
|
|
|
client = WeChatClient('appid', 'secret') |
|
70
|
|
|
res = client.message.send_image('openid', 'media_id') |
|
71
|
|
|
|
|
72
|
|
|
""" |
|
73
|
10 |
|
data = { |
|
74
|
|
|
'touser': user_id, |
|
75
|
|
|
'msgtype': 'image', |
|
76
|
|
|
'image': { |
|
77
|
|
|
'media_id': media_id |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
10 |
|
return self._send_custom_message(data, account=account) |
|
81
|
|
|
|
|
82
|
10 |
|
def send_voice(self, user_id, media_id, account=None): |
|
83
|
|
|
""" |
|
84
|
|
|
发送语音消息 |
|
85
|
|
|
|
|
86
|
|
|
详情请参考 |
|
87
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
88
|
|
|
|
|
89
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
90
|
|
|
:param media_id: 发送的语音的媒体ID。 可以通过 :func:`upload_media` 上传。 |
|
91
|
|
|
:param account: 可选,客服账号 |
|
92
|
|
|
:return: 返回的 JSON 数据包 |
|
93
|
|
|
|
|
94
|
|
|
使用示例:: |
|
95
|
|
|
|
|
96
|
|
|
from wechatpy import WeChatClient |
|
97
|
|
|
|
|
98
|
|
|
client = WeChatClient('appid', 'secret') |
|
99
|
|
|
res = client.message.send_voice('openid', 'media_id') |
|
100
|
|
|
|
|
101
|
|
|
""" |
|
102
|
10 |
|
data = { |
|
103
|
|
|
'touser': user_id, |
|
104
|
|
|
'msgtype': 'voice', |
|
105
|
|
|
'voice': { |
|
106
|
|
|
'media_id': media_id |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
10 |
|
return self._send_custom_message(data, account=account) |
|
110
|
|
|
|
|
111
|
10 |
|
def send_video(self, user_id, media_id, title=None, |
|
112
|
|
|
description=None, account=None): |
|
113
|
|
|
""" |
|
114
|
|
|
发送视频消息 |
|
115
|
|
|
|
|
116
|
|
|
详情请参考 |
|
117
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
118
|
|
|
|
|
119
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
120
|
|
|
:param media_id: 发送的视频的媒体ID。 可以通过 :func:`upload_media` 上传。 |
|
121
|
|
|
:param title: 视频消息的标题 |
|
122
|
|
|
:param description: 视频消息的描述 |
|
123
|
|
|
:param account: 可选,客服账号 |
|
124
|
|
|
:return: 返回的 JSON 数据包 |
|
125
|
|
|
|
|
126
|
|
|
使用示例:: |
|
127
|
|
|
|
|
128
|
|
|
from wechatpy import WeChatClient |
|
129
|
|
|
|
|
130
|
|
|
client = WeChatClient('appid', 'secret') |
|
131
|
|
|
res = client.message.send_video('openid', 'media_id', 'title', 'description') |
|
132
|
|
|
""" |
|
133
|
10 |
|
video_data = { |
|
134
|
|
|
'media_id': media_id, |
|
135
|
|
|
} |
|
136
|
10 |
|
if title: |
|
137
|
10 |
|
video_data['title'] = title |
|
138
|
10 |
|
if description: |
|
139
|
10 |
|
video_data['description'] = description |
|
140
|
|
|
|
|
141
|
10 |
|
data = { |
|
142
|
|
|
'touser': user_id, |
|
143
|
|
|
'msgtype': 'video', |
|
144
|
|
|
'video': video_data |
|
145
|
|
|
} |
|
146
|
10 |
|
return self._send_custom_message(data, account=account) |
|
147
|
|
|
|
|
148
|
10 |
|
def send_music(self, user_id, url, hq_url, thumb_media_id, |
|
149
|
|
|
title=None, description=None, account=None): |
|
150
|
|
|
""" |
|
151
|
|
|
发送音乐消息 |
|
152
|
|
|
|
|
153
|
|
|
详情请参考 |
|
154
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
155
|
|
|
|
|
156
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
157
|
|
|
:param url: 音乐链接 |
|
158
|
|
|
:param hq_url: 高品质音乐链接,wifi环境优先使用该链接播放音乐 |
|
159
|
|
|
:param thumb_media_id: 缩略图的媒体ID。 可以通过 :func:`upload_media` 上传。 |
|
160
|
|
|
:param title: 音乐标题 |
|
161
|
|
|
:param description: 音乐描述 |
|
162
|
|
|
:param account: 可选,客服账号 |
|
163
|
|
|
:return: 返回的 JSON 数据包 |
|
164
|
|
|
""" |
|
165
|
10 |
|
music_data = { |
|
166
|
|
|
'musicurl': url, |
|
167
|
|
|
'hqmusicurl': hq_url, |
|
168
|
|
|
'thumb_media_id': thumb_media_id |
|
169
|
|
|
} |
|
170
|
10 |
|
if title: |
|
171
|
10 |
|
music_data['title'] = title |
|
172
|
10 |
|
if description: |
|
173
|
10 |
|
music_data['description'] = description |
|
174
|
|
|
|
|
175
|
10 |
|
data = { |
|
176
|
|
|
'touser': user_id, |
|
177
|
|
|
'msgtype': 'music', |
|
178
|
|
|
'music': music_data |
|
179
|
|
|
} |
|
180
|
10 |
|
return self._send_custom_message(data, account=account) |
|
181
|
|
|
|
|
182
|
10 |
|
def send_articles(self, user_id, articles, account=None): |
|
183
|
|
|
""" |
|
184
|
|
|
发送图文消息 |
|
185
|
|
|
|
|
186
|
|
|
详情请参考 |
|
187
|
|
|
http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html |
|
188
|
|
|
|
|
189
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
190
|
|
|
:param articles: 一个包含至多10个图文的数组, 或者微信图文消息素材 media_id |
|
191
|
|
|
:param account: 可选,客服账号 |
|
192
|
|
|
:return: 返回的 JSON 数据包 |
|
193
|
|
|
""" |
|
194
|
10 |
|
if isinstance(articles, (tuple, list)): |
|
195
|
10 |
|
articles_data = [] |
|
196
|
10 |
|
for article in articles: |
|
197
|
10 |
|
articles_data.append({ |
|
198
|
|
|
'title': article['title'], |
|
199
|
|
|
'description': article['description'], |
|
200
|
|
|
'url': article['url'], |
|
201
|
|
|
'picurl': article.get('image', article.get('picurl')), |
|
202
|
|
|
}) |
|
203
|
10 |
|
data = { |
|
204
|
|
|
'touser': user_id, |
|
205
|
|
|
'msgtype': 'news', |
|
206
|
|
|
'news': { |
|
207
|
|
|
'articles': articles_data |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
else: |
|
211
|
|
|
data = { |
|
212
|
|
|
'touser': user_id, |
|
213
|
|
|
'msgtype': 'mpnews', |
|
214
|
|
|
'mpnews': { |
|
215
|
|
|
'media_id': articles, |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
10 |
|
return self._send_custom_message(data, account=account) |
|
219
|
|
|
|
|
220
|
10 |
|
def send_link(self, user_id, article, account=None): |
|
221
|
|
|
""" |
|
222
|
|
|
发送图文消息(单图文) |
|
223
|
|
|
|
|
224
|
|
|
详情请参考 |
|
225
|
|
|
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html#method-http |
|
226
|
|
|
|
|
227
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
228
|
|
|
:param article: |
|
229
|
|
|
:return: 返回的 JSON 数据包 |
|
230
|
|
|
""" |
|
231
|
|
|
|
|
232
|
|
|
data = { |
|
233
|
|
|
'touser': user_id, |
|
234
|
|
|
'msgtype': 'link', |
|
235
|
|
|
'link': { |
|
236
|
|
|
'title': article['title'], |
|
237
|
|
|
'description': article['description'], |
|
238
|
|
|
'url': article['url'], |
|
239
|
|
|
'picurl': article.get('image', article.get('picurl')), |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
return self._send_custom_message(data, account=account) |
|
243
|
|
|
|
|
244
|
10 |
|
def send_card(self, user_id, card_id, card_ext=None, account=None): |
|
245
|
|
|
""" |
|
246
|
|
|
发送卡券消息 |
|
247
|
|
|
|
|
248
|
|
|
详情请参参考 |
|
249
|
|
|
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 |
|
250
|
|
|
|
|
251
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
252
|
|
|
:param card_id: 卡券 ID |
|
253
|
|
|
:param card_ext: 可选,卡券扩展信息 |
|
254
|
|
|
:param account: 可选,客服账号 |
|
255
|
|
|
:return: 返回的 JSON 数据包 |
|
256
|
|
|
""" |
|
257
|
10 |
|
wxcard = { |
|
258
|
|
|
'card_id': card_id, |
|
259
|
|
|
} |
|
260
|
10 |
|
if card_ext: |
|
261
|
|
|
wxcard['card_ext'] = card_ext |
|
262
|
10 |
|
data = { |
|
263
|
|
|
'touser': user_id, |
|
264
|
|
|
'msgtype': 'wxcard', |
|
265
|
|
|
'wxcard': wxcard, |
|
266
|
|
|
} |
|
267
|
10 |
|
return self._send_custom_message(data, account=account) |
|
268
|
|
|
|
|
269
|
10 |
|
def send_mini_program_page(self, user_id, miniprogrampage, account=None): |
|
270
|
|
|
"""发送小程序卡片(要求小程序与公众号已关联) |
|
271
|
|
|
|
|
272
|
|
|
详情请参参考 |
|
273
|
|
|
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 |
|
274
|
|
|
|
|
275
|
|
|
:param user_id: 用户 ID openid |
|
276
|
|
|
:param miniprogrampage: 小程序卡片信息 |
|
277
|
|
|
:param account: 可选,客服账号 |
|
278
|
|
|
:return: 返回的 JSON 数据包 |
|
279
|
|
|
""" |
|
280
|
10 |
|
data = { |
|
281
|
|
|
'touser': user_id, |
|
282
|
|
|
'msgtype': 'miniprogrampage', |
|
283
|
|
|
'miniprogrampage': miniprogrampage |
|
284
|
|
|
} |
|
285
|
10 |
|
return self._send_custom_message(data, account=account) |
|
286
|
|
|
|
|
287
|
10 |
|
def delete_mass(self, msg_id): |
|
288
|
|
|
""" |
|
289
|
|
|
删除群发消息 |
|
290
|
|
|
|
|
291
|
|
|
详情请参考 |
|
292
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
293
|
|
|
|
|
294
|
|
|
:param msg_id: 要删除的群发消息 ID |
|
295
|
|
|
:return: 返回的 JSON 数据包 |
|
296
|
|
|
|
|
297
|
|
|
使用示例:: |
|
298
|
|
|
|
|
299
|
|
|
from wechatpy import WeChatClient |
|
300
|
|
|
|
|
301
|
|
|
client = WeChatClient('appid', 'secret') |
|
302
|
|
|
res = client.message.delete_mass('message id') |
|
303
|
|
|
|
|
304
|
|
|
""" |
|
305
|
|
|
return self._post( |
|
306
|
|
|
'message/mass/delete', |
|
307
|
|
|
data={ |
|
308
|
|
|
'msg_id': msg_id |
|
309
|
|
|
} |
|
310
|
|
|
) |
|
311
|
|
|
|
|
312
|
10 |
|
def _send_mass_message(self, group_or_users, msg_type, msg, |
|
313
|
|
|
is_to_all=False, preview=False, |
|
314
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
315
|
10 |
|
data = { |
|
316
|
|
|
'msgtype': msg_type, |
|
317
|
|
|
'send_ignore_reprint': send_ignore_reprint, |
|
318
|
|
|
} |
|
319
|
10 |
|
if client_msg_id is not None: |
|
320
|
|
|
data['clientmsgid'] = client_msg_id |
|
321
|
10 |
|
if not preview: |
|
322
|
10 |
|
if isinstance(group_or_users, (tuple, list)): |
|
323
|
|
|
# send by user ids |
|
324
|
10 |
|
data['touser'] = group_or_users |
|
325
|
10 |
|
endpoint = 'message/mass/send' |
|
326
|
|
|
else: |
|
327
|
|
|
# send by group id |
|
328
|
|
|
data['filter'] = { |
|
329
|
|
|
'group_id': group_or_users, |
|
330
|
|
|
'is_to_all': is_to_all, |
|
331
|
|
|
} |
|
332
|
|
|
endpoint = 'message/mass/sendall' |
|
333
|
|
|
else: |
|
334
|
|
|
if not isinstance(group_or_users, six.string_types): |
|
335
|
|
|
raise ValueError('group_or_users should be string types') |
|
336
|
|
|
# 预览接口 |
|
337
|
|
|
if self.OPENID_RE.match(group_or_users): |
|
338
|
|
|
# 按照 openid 预览群发 |
|
339
|
|
|
data['touser'] = group_or_users |
|
340
|
|
|
else: |
|
341
|
|
|
# 按照微信号预览群发 |
|
342
|
|
|
data['towxname'] = group_or_users |
|
343
|
|
|
endpoint = 'message/mass/preview' |
|
344
|
|
|
|
|
345
|
10 |
|
data.update(msg) |
|
346
|
10 |
|
return self._post( |
|
347
|
|
|
endpoint, |
|
348
|
|
|
data=data |
|
349
|
|
|
) |
|
350
|
|
|
|
|
351
|
10 |
|
def send_mass_text(self, group_or_users, content, |
|
352
|
|
|
is_to_all=False, preview=False, |
|
353
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
354
|
|
|
""" |
|
355
|
|
|
群发文本消息 |
|
356
|
|
|
|
|
357
|
|
|
详情请参考 |
|
358
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
359
|
|
|
|
|
360
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
361
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
362
|
|
|
:param content: 消息正文 |
|
363
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
364
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
365
|
|
|
:type is_to_all: bool |
|
366
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
367
|
|
|
:type preview: bool |
|
368
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
369
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
370
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
371
|
|
|
send_ignore_reprint 默认为0。 |
|
372
|
|
|
:type send_ignore_reprint: int |
|
373
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
374
|
|
|
:type client_msg_id: str |
|
375
|
|
|
|
|
376
|
|
|
:return: 返回的 JSON 数据包 |
|
377
|
|
|
""" |
|
378
|
10 |
|
return self._send_mass_message( |
|
379
|
|
|
group_or_users, |
|
380
|
|
|
'text', |
|
381
|
|
|
{ |
|
382
|
|
|
'text': { |
|
383
|
|
|
'content': content |
|
384
|
|
|
} |
|
385
|
|
|
}, |
|
386
|
|
|
is_to_all, |
|
387
|
|
|
preview, |
|
388
|
|
|
send_ignore_reprint, |
|
389
|
|
|
client_msg_id, |
|
390
|
|
|
) |
|
391
|
|
|
|
|
392
|
10 |
|
def send_mass_image(self, group_or_users, media_id, |
|
393
|
|
|
is_to_all=False, preview=False, |
|
394
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
395
|
|
|
""" |
|
396
|
|
|
群发图片消息 |
|
397
|
|
|
|
|
398
|
|
|
详情请参考 |
|
399
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
400
|
|
|
|
|
401
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
402
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
403
|
|
|
:param media_id: 图片的媒体 ID。 可以通过 :func:`upload_media` 上传。 |
|
404
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
405
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
406
|
|
|
:type is_to_all: bool |
|
407
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
408
|
|
|
:type preview: bool |
|
409
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
410
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
411
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
412
|
|
|
send_ignore_reprint 默认为0。 |
|
413
|
|
|
:type send_ignore_reprint: int |
|
414
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
415
|
|
|
:type client_msg_id: str |
|
416
|
|
|
|
|
417
|
|
|
:return: 返回的 JSON 数据包 |
|
418
|
|
|
""" |
|
419
|
10 |
|
return self._send_mass_message( |
|
420
|
|
|
group_or_users, |
|
421
|
|
|
'image', |
|
422
|
|
|
{ |
|
423
|
|
|
'image': { |
|
424
|
|
|
'media_id': media_id |
|
425
|
|
|
} |
|
426
|
|
|
}, |
|
427
|
|
|
is_to_all, |
|
428
|
|
|
preview, |
|
429
|
|
|
send_ignore_reprint, |
|
430
|
|
|
client_msg_id, |
|
431
|
|
|
) |
|
432
|
|
|
|
|
433
|
10 |
|
def send_mass_voice(self, group_or_users, media_id, |
|
434
|
|
|
is_to_all=False, preview=False, |
|
435
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
436
|
|
|
""" |
|
437
|
|
|
群发语音消息 |
|
438
|
|
|
|
|
439
|
|
|
详情请参考 |
|
440
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
441
|
|
|
|
|
442
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
443
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
444
|
|
|
:param media_id: 语音的媒体 ID。可以通过 :func:`upload_media` 上传。 |
|
445
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
446
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
447
|
|
|
:type is_to_all: bool |
|
448
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
449
|
|
|
:type preview: bool |
|
450
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
451
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
452
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
453
|
|
|
send_ignore_reprint 默认为0。 |
|
454
|
|
|
:type send_ignore_reprint: int |
|
455
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
456
|
|
|
:type client_msg_id: str |
|
457
|
|
|
|
|
458
|
|
|
:return: 返回的 JSON 数据包 |
|
459
|
|
|
""" |
|
460
|
10 |
|
return self._send_mass_message( |
|
461
|
|
|
group_or_users, |
|
462
|
|
|
'voice', |
|
463
|
|
|
{ |
|
464
|
|
|
'voice': { |
|
465
|
|
|
'media_id': media_id |
|
466
|
|
|
} |
|
467
|
|
|
}, |
|
468
|
|
|
is_to_all, |
|
469
|
|
|
preview, |
|
470
|
|
|
send_ignore_reprint, |
|
471
|
|
|
client_msg_id, |
|
472
|
|
|
) |
|
473
|
|
|
|
|
474
|
10 |
|
def send_mass_video(self, group_or_users, media_id, title=None, |
|
475
|
|
|
description=None, is_to_all=False, preview=False, |
|
476
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
477
|
|
|
""" |
|
478
|
|
|
群发视频消息 |
|
479
|
|
|
|
|
480
|
|
|
详情请参考 |
|
481
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
482
|
|
|
|
|
483
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
484
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
485
|
|
|
:param media_id: 视频的媒体 ID。可以通过 :func:`upload_video` 上传。 |
|
486
|
|
|
:param title: 视频标题 |
|
487
|
|
|
:param description: 视频描述 |
|
488
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
489
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
490
|
|
|
:type is_to_all: bool |
|
491
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
492
|
|
|
:type preview: bool |
|
493
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
494
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
495
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
496
|
|
|
send_ignore_reprint 默认为0。 |
|
497
|
|
|
:type send_ignore_reprint: int |
|
498
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
499
|
|
|
:type client_msg_id: str |
|
500
|
|
|
|
|
501
|
|
|
:return: 返回的 JSON 数据包 |
|
502
|
|
|
""" |
|
503
|
10 |
|
video_data = { |
|
504
|
|
|
'media_id': media_id |
|
505
|
|
|
} |
|
506
|
10 |
|
if title: |
|
507
|
10 |
|
video_data['title'] = title |
|
508
|
10 |
|
if description: |
|
509
|
10 |
|
video_data['description'] = description |
|
510
|
10 |
|
return self._send_mass_message( |
|
511
|
|
|
group_or_users, |
|
512
|
|
|
'mpvideo', |
|
513
|
|
|
{ |
|
514
|
|
|
'mpvideo': video_data |
|
515
|
|
|
}, |
|
516
|
|
|
is_to_all, |
|
517
|
|
|
preview, |
|
518
|
|
|
send_ignore_reprint, |
|
519
|
|
|
client_msg_id, |
|
520
|
|
|
) |
|
521
|
|
|
|
|
522
|
10 |
|
def send_mass_article(self, group_or_users, media_id, |
|
523
|
|
|
is_to_all=False, preview=False, |
|
524
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
525
|
|
|
""" |
|
526
|
|
|
群发图文消息 |
|
527
|
|
|
|
|
528
|
|
|
详情请参考 |
|
529
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
530
|
|
|
|
|
531
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
532
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
533
|
|
|
:param media_id: 图文的媒体 ID。可以通过 :func:`upload_articles` 上传。 |
|
534
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
535
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
536
|
|
|
:type is_to_all: bool |
|
537
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
538
|
|
|
:type preview: bool |
|
539
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
540
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
541
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
542
|
|
|
send_ignore_reprint 默认为0。 |
|
543
|
|
|
:type send_ignore_reprint: int |
|
544
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
545
|
|
|
:type client_msg_id: str |
|
546
|
|
|
|
|
547
|
|
|
:return: 返回的 JSON 数据包 |
|
548
|
|
|
""" |
|
549
|
10 |
|
return self._send_mass_message( |
|
550
|
|
|
group_or_users, |
|
551
|
|
|
'mpnews', |
|
552
|
|
|
{ |
|
553
|
|
|
'mpnews': { |
|
554
|
|
|
'media_id': media_id |
|
555
|
|
|
} |
|
556
|
|
|
}, |
|
557
|
|
|
is_to_all, |
|
558
|
|
|
preview, |
|
559
|
|
|
send_ignore_reprint, |
|
560
|
|
|
client_msg_id, |
|
561
|
|
|
) |
|
562
|
|
|
|
|
563
|
10 |
|
def get_mass(self, msg_id): |
|
564
|
|
|
""" |
|
565
|
|
|
查询群发消息发送状态 |
|
566
|
|
|
|
|
567
|
|
|
详情请参考 |
|
568
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
569
|
|
|
|
|
570
|
|
|
:param msg_id: 群发消息后返回的消息id |
|
571
|
|
|
:return: 返回的 JSON 数据包 |
|
572
|
|
|
|
|
573
|
|
|
使用示例:: |
|
574
|
|
|
|
|
575
|
|
|
from wechatpy import WeChatClient |
|
576
|
|
|
|
|
577
|
|
|
client = WeChatClient('appid', 'secret') |
|
578
|
|
|
res = client.message.get_mass('mass message id') |
|
579
|
|
|
|
|
580
|
|
|
""" |
|
581
|
10 |
|
return self._post( |
|
582
|
|
|
'message/mass/get', |
|
583
|
|
|
data={ |
|
584
|
|
|
'msg_id': msg_id |
|
585
|
|
|
} |
|
586
|
|
|
) |
|
587
|
|
|
|
|
588
|
10 |
|
def send_template(self, user_id, template_id, data, url=None, mini_program=None): |
|
589
|
|
|
""" |
|
590
|
|
|
发送模板消息 |
|
591
|
|
|
|
|
592
|
|
|
详情请参考 |
|
593
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1445241432&lang=zh_CN |
|
594
|
|
|
|
|
595
|
|
|
:param user_id: 用户 ID 。 就是你收到的 `Message` 的 source |
|
596
|
|
|
:param template_id: 模板 ID。在公众平台线上模板库中选用模板获得 |
|
597
|
|
|
:param url: 链接地址 |
|
598
|
|
|
:param data: 模板消息数据 |
|
599
|
|
|
:param mini_program: 跳小程序所需数据, 如:`{'appid': 'appid', 'pagepath': 'index?foo=bar'}` |
|
600
|
|
|
:return: 返回的 JSON 数据包 |
|
601
|
|
|
""" |
|
602
|
|
|
tpl_data = optionaldict( |
|
603
|
|
|
touser=user_id, |
|
604
|
|
|
template_id=template_id, |
|
605
|
|
|
url=url, |
|
606
|
|
|
miniprogram=mini_program, |
|
607
|
|
|
data=data, |
|
608
|
|
|
) |
|
609
|
|
|
return self._post( |
|
610
|
|
|
'message/template/send', |
|
611
|
|
|
data=tpl_data |
|
612
|
|
|
) |
|
613
|
|
|
|
|
614
|
10 |
|
def get_autoreply_info(self): |
|
615
|
|
|
""" |
|
616
|
|
|
获取自动回复规则 |
|
617
|
|
|
|
|
618
|
|
|
详情请参考 |
|
619
|
|
|
http://mp.weixin.qq.com/wiki/7/7b5789bb1262fb866d01b4b40b0efecb.html |
|
620
|
|
|
|
|
621
|
|
|
:return: 返回的 JSON 数据包 |
|
622
|
|
|
|
|
623
|
|
|
使用示例:: |
|
624
|
|
|
|
|
625
|
|
|
from wechatpy import WeChatClient |
|
626
|
|
|
|
|
627
|
|
|
client = WeChatClient('appid', 'secret') |
|
628
|
|
|
info = client.message.get_autoreply_info() |
|
629
|
|
|
|
|
630
|
|
|
""" |
|
631
|
10 |
|
return self._get('get_current_autoreply_info') |
|
632
|
|
|
|
|
633
|
10 |
|
def send_mass_card(self, group_or_users, card_id, |
|
634
|
|
|
is_to_all=False, preview=False, |
|
635
|
|
|
send_ignore_reprint=0, client_msg_id=None): |
|
636
|
|
|
""" |
|
637
|
|
|
群发卡券消息 |
|
638
|
|
|
|
|
639
|
|
|
详情请参考 |
|
640
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1481187827_i0l21 |
|
641
|
|
|
|
|
642
|
|
|
:param group_or_users: 值为整型数字时为按分组群发,值为列表/元组时为按 OpenID 列表群发 |
|
643
|
|
|
当 is_to_all 为 True 时,传入 None 即对所有用户发送。 |
|
644
|
|
|
:param card_id: 卡券 ID |
|
645
|
|
|
:param is_to_all: 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户 |
|
646
|
|
|
选择false可根据group_id发送给指定群组的用户 |
|
647
|
|
|
:type is_to_all: bool |
|
648
|
|
|
:param preview: 是否发送预览,此时 group_or_users 参数应为一个openid字符串 |
|
649
|
|
|
:type preview: bool |
|
650
|
|
|
:param send_ignore_reprint: 指定待群发的文章被判定为转载时,是否继续群发。 |
|
651
|
|
|
当 send_ignore_reprint 参数设置为1时,文章被判定为转载时,且原创文允许转载时,将继续进行群发操作。 |
|
652
|
|
|
当 send_ignore_reprint 参数设置为0时,文章被判定为转载时,将停止群发操作。 |
|
653
|
|
|
send_ignore_reprint 默认为0。 |
|
654
|
|
|
:type send_ignore_reprint: int |
|
655
|
|
|
:param client_msg_id: 开发者侧群发 msgid,长度限制 64 字节 |
|
656
|
|
|
:type client_msg_id: str |
|
657
|
|
|
|
|
658
|
|
|
:return: 返回的 JSON 数据包 |
|
659
|
|
|
""" |
|
660
|
10 |
|
return self._send_mass_message( |
|
661
|
|
|
group_or_users, |
|
662
|
|
|
'wxcard', |
|
663
|
|
|
{ |
|
664
|
|
|
'wxcard': { |
|
665
|
|
|
'card_id': card_id |
|
666
|
|
|
} |
|
667
|
|
|
}, |
|
668
|
|
|
is_to_all, |
|
669
|
|
|
preview, |
|
670
|
|
|
send_ignore_reprint, |
|
671
|
|
|
client_msg_id, |
|
672
|
|
|
) |
|
673
|
|
|
|
|
674
|
10 |
|
def get_subscribe_authorize_url(self, scene, template_id, redirect_url, reserved=None): |
|
675
|
|
|
""" |
|
676
|
|
|
构造请求用户授权的url |
|
677
|
|
|
详情请参阅: |
|
678
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1500374289_66bvB |
|
679
|
|
|
|
|
680
|
|
|
:param scene: 订阅场景值,开发者可以填0-10000的整形值,用来标识订阅场景值 |
|
681
|
|
|
:type scene: int |
|
682
|
|
|
:param template_id: 订阅消息模板ID,登录公众平台后台,在接口权限列表处可查看订阅模板ID |
|
683
|
|
|
:param redirect_url: 授权后重定向的回调地址 |
|
684
|
|
|
:param reserved: 用于保持请求和回调的状态,授权请后原样带回给第三方。该参数可用于防止csrf攻击。若不指定则随机生成。 |
|
685
|
|
|
""" |
|
686
|
10 |
|
if reserved is None: |
|
687
|
|
|
reserved = random_string() |
|
688
|
10 |
|
base_url = 'https://mp.weixin.qq.com/mp/subscribemsg' |
|
689
|
10 |
|
params = [ |
|
690
|
|
|
('action', 'get_confirm'), |
|
691
|
|
|
('appid', self.appid), |
|
692
|
|
|
('scene', scene), |
|
693
|
|
|
('template_id', template_id), |
|
694
|
|
|
('redirect_url', redirect_url), |
|
695
|
|
|
('reserved', reserved), |
|
696
|
|
|
] |
|
697
|
10 |
|
encoded_params = six.moves.urllib.parse.urlencode(params) |
|
698
|
10 |
|
url = '{base}?{params}#wechat_redirect'.format(base=base_url, params=encoded_params) |
|
699
|
10 |
|
return url |
|
700
|
|
|
|
|
701
|
10 |
|
def send_subscribe_template(self, openid, template_id, scene, title, data, url=None): |
|
702
|
|
|
""" |
|
703
|
|
|
一次性订阅消息,通过API推送订阅模板消息给到授权微信用户。 |
|
704
|
|
|
详情请参阅: |
|
705
|
|
|
https://mp.weixin.qq.com/wiki?id=mp1500374289_66bvB |
|
706
|
|
|
|
|
707
|
|
|
:param openid: 填接收消息的用户openid |
|
708
|
|
|
:param template_id: 订阅消息模板ID |
|
709
|
|
|
:param scene: 订阅场景值,开发者可以填0-10000的整形值,用来标识订阅场景值 |
|
710
|
|
|
:type scene: int |
|
711
|
|
|
:param title: 消息标题,15字以内 |
|
712
|
|
|
:param data: 消息正文,value为消息内容,color为颜色,200字以内 |
|
713
|
|
|
:type data: dict |
|
714
|
|
|
:param url: 点击消息跳转的链接,需要有ICP备案 |
|
715
|
|
|
""" |
|
716
|
|
|
post_data = { |
|
717
|
|
|
'touser': openid, |
|
718
|
|
|
'template_id': template_id, |
|
719
|
|
|
'url': url, |
|
720
|
|
|
'scene': scene, |
|
721
|
|
|
'title': title, |
|
722
|
|
|
'data': data, |
|
723
|
|
|
} |
|
724
|
|
|
if url is not None: |
|
725
|
|
|
post_data['url'] = url |
|
726
|
|
|
return self._post( |
|
727
|
|
|
'message/template/subscribe', |
|
728
|
|
|
data=post_data, |
|
729
|
|
|
) |
|
730
|
|
|
|
|
731
|
10 |
|
def send_msg_menu(self, openid, msgmenu, account=None): |
|
732
|
|
|
""" |
|
733
|
|
|
发送菜单消息 |
|
734
|
|
|
|
|
735
|
|
|
详情请参考 |
|
736
|
|
|
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html#7 |
|
737
|
|
|
|
|
738
|
|
|
:param openid: 填接收消息的用户openid |
|
739
|
|
|
:param msgmenu: 菜单字典 |
|
740
|
|
|
:param account: 可选,客服账号 |
|
741
|
|
|
:return: 返回的 JSON 数据包 |
|
742
|
|
|
""" |
|
743
|
|
|
data = { |
|
744
|
|
|
'touser': openid, |
|
745
|
|
|
'msgtype': 'msgmenu', |
|
746
|
|
|
'msgmenu': msgmenu |
|
747
|
|
|
} |
|
748
|
|
|
return self._send_custom_message(data, account=account) |
|
749
|
|
|
|