|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
10 |
|
from __future__ import absolute_import, unicode_literals |
|
3
|
10 |
|
from optionaldict import optionaldict |
|
4
|
|
|
|
|
5
|
10 |
|
from wechatpy.client.api.base import BaseWeChatAPI |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
10 |
|
class WeChatMessage(BaseWeChatAPI): |
|
9
|
|
|
|
|
10
|
10 |
|
def _send_message(self, agent_id, user_ids, party_ids='', |
|
11
|
|
|
tag_ids='', msg=None): |
|
12
|
|
|
msg = msg or {} |
|
13
|
|
|
if isinstance(user_ids, (tuple, list)): |
|
14
|
|
|
user_ids = '|'.join(user_ids) |
|
15
|
|
|
if isinstance(party_ids, (tuple, list)): |
|
16
|
|
|
party_ids = '|'.join(party_ids) |
|
17
|
|
|
if isinstance(tag_ids, (tuple, list)): |
|
18
|
|
|
tag_ids = '|'.join(tag_ids) |
|
19
|
|
|
|
|
20
|
|
|
data = { |
|
21
|
|
|
'touser': user_ids, |
|
22
|
|
|
'toparty': party_ids, |
|
23
|
|
|
'totag': tag_ids, |
|
24
|
|
|
'agentid': agent_id |
|
25
|
|
|
} |
|
26
|
|
|
data.update(msg) |
|
27
|
|
|
return self._post( |
|
28
|
|
|
'message/send', |
|
29
|
|
|
data=data |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
10 |
|
def send_text(self, agent_id, user_ids, content, |
|
33
|
|
|
party_ids='', tag_ids='', safe=0): |
|
34
|
|
|
return self._send_message( |
|
35
|
|
|
agent_id, |
|
36
|
|
|
user_ids, |
|
37
|
|
|
party_ids, |
|
38
|
|
|
tag_ids, |
|
39
|
|
|
msg={ |
|
40
|
|
|
'msgtype': 'text', |
|
41
|
|
|
'text': {'content': content}, |
|
42
|
|
|
'safe': safe |
|
43
|
|
|
} |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
10 |
|
def send_text_card(self, agent_id, user_ids, title, description, url, btntxt='详情', |
|
47
|
|
|
party_ids='', tag_ids=''): |
|
48
|
|
|
""" 文本卡片消息 |
|
49
|
|
|
https://work.weixin.qq.com/api/doc#10167/文本卡片消息 |
|
50
|
|
|
|
|
51
|
|
|
请求示例: |
|
52
|
|
|
{ |
|
53
|
|
|
"touser" : "UserID1|UserID2|UserID3", |
|
54
|
|
|
"toparty" : "PartyID1 | PartyID2", |
|
55
|
|
|
"totag" : "TagID1 | TagID2", |
|
56
|
|
|
"msgtype" : "textcard", |
|
57
|
|
|
"agentid" : 1, |
|
58
|
|
|
"textcard" : { |
|
59
|
|
|
"title" : "领奖通知", |
|
60
|
|
|
"description" : "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台, |
|
61
|
|
|
领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>", |
|
62
|
10 |
|
"url" : "URL", |
|
63
|
|
|
"btntxt":"更多" |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
特殊说明: |
|
68
|
|
|
卡片消息的展现形式非常灵活,支持使用br标签或者空格来进行换行处理,也支持使用div标签来使用不同的字体颜色, |
|
69
|
|
|
目前内置了3种文字颜色:灰色(gray)、高亮(highlight)、默认黑色(normal),将其作为div标签的class属性即可, |
|
70
|
|
|
具体用法请参考上面的示例。 |
|
71
|
|
|
|
|
72
|
|
|
:param agent_id: 必填,企业应用的id,整型。可在应用的设置页面查看。 |
|
73
|
|
|
:param user_ids: 成员ID列表。 |
|
74
|
|
|
:param title: 必填,成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。 |
|
75
|
|
|
:param description: 必填,描述,不超过512个字节,超过会自动截断 |
|
76
|
|
|
:param url: 必填,点击后跳转的链接。 |
|
77
|
|
|
:param btntxt: 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。 |
|
78
|
10 |
|
:param party_ids: 部门ID列表。 |
|
79
|
|
|
:param tag_ids: 标签ID列表。 |
|
80
|
|
|
""" |
|
81
|
|
|
return self._send_message( |
|
82
|
|
|
agent_id, |
|
83
|
|
|
user_ids, |
|
84
|
|
|
party_ids, |
|
85
|
|
|
tag_ids, |
|
86
|
|
|
msg={ |
|
87
|
|
|
'msgtype': 'textcard', |
|
88
|
|
|
'textcard': { |
|
89
|
|
|
'title': title, |
|
90
|
|
|
'description': description, |
|
91
|
|
|
'url': url, |
|
92
|
|
|
'btntxt': btntxt, |
|
93
|
|
|
}, |
|
94
|
|
|
} |
|
95
|
|
|
) |
|
96
|
|
|
|
|
97
|
10 |
|
def send_image(self, agent_id, user_ids, media_id, |
|
98
|
|
|
party_ids='', tag_ids='', safe=0): |
|
99
|
|
|
return self._send_message( |
|
100
|
|
|
agent_id, |
|
101
|
|
|
user_ids, |
|
102
|
|
|
party_ids, |
|
103
|
|
|
tag_ids, |
|
104
|
|
|
msg={ |
|
105
|
|
|
'msgtype': 'image', |
|
106
|
|
|
'image': { |
|
107
|
|
|
'media_id': media_id |
|
108
|
|
|
}, |
|
109
|
|
|
'safe': safe |
|
110
|
|
|
} |
|
111
|
|
|
) |
|
112
|
|
|
|
|
113
|
10 |
|
def send_voice(self, agent_id, user_ids, media_id, |
|
114
|
|
|
party_ids='', tag_ids='', safe=0): |
|
115
|
|
|
return self._send_message( |
|
116
|
|
|
agent_id, |
|
117
|
|
|
user_ids, |
|
118
|
|
|
party_ids, |
|
119
|
|
|
tag_ids, |
|
120
|
|
|
msg={ |
|
121
|
|
|
'msgtype': 'voice', |
|
122
|
|
|
'voice': { |
|
123
|
|
|
'media_id': media_id |
|
124
|
|
|
}, |
|
125
|
|
|
'safe': safe |
|
126
|
|
|
} |
|
127
|
|
|
) |
|
128
|
|
|
|
|
129
|
|
|
def send_video(self, agent_id, user_ids, media_id, title=None, |
|
130
|
|
|
description=None, party_ids='', tag_ids='', safe=0): |
|
131
|
|
|
video_data = optionaldict() |
|
132
|
|
|
video_data['media_id'] = media_id |
|
133
|
|
|
video_data['title'] = title |
|
134
|
|
|
video_data['description'] = description |
|
135
|
|
|
|
|
136
|
10 |
|
return self._send_message( |
|
137
|
|
|
agent_id, |
|
138
|
|
|
user_ids, |
|
139
|
|
|
party_ids, |
|
140
|
|
|
tag_ids, |
|
141
|
|
|
msg={ |
|
142
|
|
|
'msgtype': 'video', |
|
143
|
|
|
'video': dict(video_data), |
|
144
|
|
|
'safe': safe |
|
145
|
|
|
} |
|
146
|
|
|
) |
|
147
|
|
|
|
|
148
|
|
|
def send_file(self, agent_id, user_ids, media_id, |
|
149
|
|
|
party_ids='', tag_ids='', safe=0): |
|
150
|
|
|
return self._send_message( |
|
151
|
|
|
agent_id, |
|
152
|
|
|
user_ids, |
|
153
|
|
|
party_ids, |
|
154
|
|
|
tag_ids, |
|
155
|
|
|
msg={ |
|
156
|
|
|
'msgtype': 'file', |
|
157
|
|
|
'file': { |
|
158
|
|
|
'media_id': media_id |
|
159
|
|
|
}, |
|
160
|
|
|
'safe': safe |
|
161
|
|
|
} |
|
162
|
|
|
) |
|
163
|
|
|
|
|
164
|
|
|
def send_articles(self, agent_id, user_ids, articles, |
|
165
|
|
|
party_ids='', tag_ids=''): |
|
166
|
|
|
articles_data = [] |
|
167
|
|
|
for article in articles: |
|
168
|
|
|
articles_data.append({ |
|
169
|
|
|
'title': article['title'], |
|
170
|
|
|
'description': article['description'], |
|
171
|
|
|
'url': article['url'], |
|
172
|
|
|
'picurl': article['image'] |
|
173
|
|
|
}) |
|
174
|
|
|
return self._send_message( |
|
175
|
|
|
agent_id, |
|
176
|
|
|
user_ids, |
|
177
|
|
|
party_ids, |
|
178
|
|
|
tag_ids, |
|
179
|
|
|
msg={ |
|
180
|
|
|
'msgtype': 'news', |
|
181
|
|
|
'news': { |
|
182
|
|
|
'articles': articles_data |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
) |
|
186
|
|
|
|
|
187
|
|
|
def send_mp_articles(self, agent_id, user_ids, articles, |
|
188
|
|
|
party_ids='', tag_ids='', safe=0): |
|
189
|
|
|
articles_data = [] |
|
190
|
|
|
for article in articles: |
|
191
|
|
|
articles_data.append({ |
|
192
|
|
|
'thumb_media_id': article['thumb_media_id'], |
|
193
|
|
|
'author': article['author'], |
|
194
|
|
|
'title': article['title'], |
|
195
|
|
|
'content': article['content'], |
|
196
|
|
|
'content_source_url': article['content_source_url'], |
|
197
|
|
|
'digest': article['digest'], |
|
198
|
|
|
'show_cover_pic': article['show_cover_pic'] |
|
199
|
|
|
}) |
|
200
|
|
|
return self._send_message( |
|
201
|
|
|
agent_id, |
|
202
|
|
|
user_ids, |
|
203
|
|
|
party_ids, |
|
204
|
|
|
tag_ids, |
|
205
|
|
|
msg={ |
|
206
|
|
|
'msgtype': 'mpnews', |
|
207
|
|
|
'mpnews': { |
|
208
|
|
|
'articles': articles_data |
|
209
|
|
|
}, |
|
210
|
|
|
'safe': safe |
|
211
|
|
|
} |
|
212
|
|
|
) |
|
213
|
|
|
|