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_image(self, agent_id, user_ids, media_id, |
47
|
|
|
party_ids='', tag_ids='', safe=0): |
48
|
|
|
return self._send_message( |
49
|
|
|
agent_id, |
50
|
|
|
user_ids, |
51
|
|
|
party_ids, |
52
|
|
|
tag_ids, |
53
|
|
|
msg={ |
54
|
|
|
'msgtype': 'image', |
55
|
|
|
'image': { |
56
|
|
|
'media_id': media_id |
57
|
|
|
}, |
58
|
|
|
'safe': safe |
59
|
|
|
} |
60
|
|
|
) |
61
|
|
|
|
62
|
10 |
|
def send_voice(self, agent_id, user_ids, media_id, |
63
|
|
|
party_ids='', tag_ids='', safe=0): |
64
|
|
|
return self._send_message( |
65
|
|
|
agent_id, |
66
|
|
|
user_ids, |
67
|
|
|
party_ids, |
68
|
|
|
tag_ids, |
69
|
|
|
msg={ |
70
|
|
|
'msgtype': 'voice', |
71
|
|
|
'voice': { |
72
|
|
|
'media_id': media_id |
73
|
|
|
}, |
74
|
|
|
'safe': safe |
75
|
|
|
} |
76
|
|
|
) |
77
|
|
|
|
78
|
10 |
|
def send_video(self, agent_id, user_ids, media_id, title=None, |
79
|
|
|
description=None, party_ids='', tag_ids='', safe=0): |
80
|
|
|
video_data = optionaldict() |
81
|
|
|
video_data['media_id'] = media_id |
82
|
|
|
video_data['title'] = title |
83
|
|
|
video_data['description'] = description |
84
|
|
|
|
85
|
|
|
return self._send_message( |
86
|
|
|
agent_id, |
87
|
|
|
user_ids, |
88
|
|
|
party_ids, |
89
|
|
|
tag_ids, |
90
|
|
|
msg={ |
91
|
|
|
'msgtype': 'video', |
92
|
|
|
'video': dict(video_data), |
93
|
|
|
'safe': safe |
94
|
|
|
} |
95
|
|
|
) |
96
|
|
|
|
97
|
10 |
|
def send_file(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': 'file', |
106
|
|
|
'file': { |
107
|
|
|
'media_id': media_id |
108
|
|
|
}, |
109
|
|
|
'safe': safe |
110
|
|
|
} |
111
|
|
|
) |
112
|
|
|
|
113
|
10 |
|
def send_articles(self, agent_id, user_ids, articles, |
114
|
|
|
party_ids='', tag_ids=''): |
115
|
|
|
articles_data = [] |
116
|
|
|
for article in articles: |
117
|
|
|
articles_data.append({ |
118
|
|
|
'title': article['title'], |
119
|
|
|
'description': article['description'], |
120
|
|
|
'url': article['url'], |
121
|
|
|
'picurl': article['image'] |
122
|
|
|
}) |
123
|
|
|
return self._send_message( |
124
|
|
|
agent_id, |
125
|
|
|
user_ids, |
126
|
|
|
party_ids, |
127
|
|
|
tag_ids, |
128
|
|
|
msg={ |
129
|
|
|
'msgtype': 'news', |
130
|
|
|
'news': { |
131
|
|
|
'articles': articles_data |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
) |
135
|
|
|
|
136
|
10 |
|
def send_mp_articles(self, agent_id, user_ids, articles, |
137
|
|
|
party_ids='', tag_ids='', safe=0): |
138
|
|
|
articles_data = [] |
139
|
|
|
for article in articles: |
140
|
|
|
articles_data.append({ |
141
|
|
|
'thumb_media_id': article['thumb_media_id'], |
142
|
|
|
'author': article['author'], |
143
|
|
|
'title': article['title'], |
144
|
|
|
'content': article['content'], |
145
|
|
|
'content_source_url': article['content_source_url'], |
146
|
|
|
'digest': article['digest'], |
147
|
|
|
'show_cover_pic': article['show_cover_pic'] |
148
|
|
|
}) |
149
|
|
|
return self._send_message( |
150
|
|
|
agent_id, |
151
|
|
|
user_ids, |
152
|
|
|
party_ids, |
153
|
|
|
tag_ids, |
154
|
|
|
msg={ |
155
|
|
|
'msgtype': 'mpnews', |
156
|
|
|
'mpnews': { |
157
|
|
|
'articles': articles_data |
158
|
|
|
}, |
159
|
|
|
'safe': safe |
160
|
|
|
} |
161
|
|
|
) |
162
|
|
|
|