1
|
|
|
# -*- coding: utf-8 -*- |
2
|
10 |
|
from __future__ import absolute_import, unicode_literals |
3
|
|
|
|
4
|
10 |
|
from optionaldict import optionaldict |
5
|
|
|
|
6
|
10 |
|
from wechatpy.client.api.base import BaseWeChatAPI |
7
|
|
|
|
8
|
|
|
|
9
|
10 |
|
class WeChatAppChat(BaseWeChatAPI): |
10
|
|
|
""" |
11
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90244 |
12
|
|
|
""" |
13
|
|
|
|
14
|
10 |
|
def create(self, chat_id=None, name=None, owner=None, user_list=None): |
15
|
|
|
""" |
16
|
|
|
创建群聊会话 |
17
|
|
|
|
18
|
|
|
详情请参考 |
19
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90245 |
20
|
|
|
|
21
|
|
|
限制说明: |
22
|
|
|
只允许企业自建应用调用,且应用的可见范围必须是根部门; |
23
|
|
|
群成员人数不可超过管理端配置的“群成员人数上限”,且最大不可超过500人; |
24
|
|
|
每企业创建群数不可超过1000/天; |
25
|
|
|
|
26
|
|
|
:param chat_id: 群聊的唯一标志,不能与已有的群重复;字符串类型,最长32个字符。只允许字符0-9及字母a-zA-Z。如果不填,系统会随机生成群id |
27
|
|
|
:param name: 群聊名,最多50个utf8字符,超过将截断 |
28
|
|
|
:param owner: 指定群主的id。如果不指定,系统会随机从userlist中选一人作为群主 |
29
|
|
|
:param user_list: 会话成员列表,成员用userid来标识。至少2人,至多500人 |
30
|
|
|
:return: 返回的 JSON 数据包 |
31
|
|
|
""" |
32
|
|
|
data = optionaldict( |
33
|
|
|
chatid=chat_id, |
34
|
|
|
name=name, |
35
|
|
|
owner=owner, |
36
|
|
|
userlist=user_list, |
37
|
|
|
) |
38
|
|
|
return self._post('appchat/create', data=data) |
39
|
|
|
|
40
|
10 |
|
def get(self, chat_id): |
41
|
|
|
""" |
42
|
|
|
获取群聊会话 |
43
|
|
|
|
44
|
|
|
详情请参考 |
45
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90247 |
46
|
|
|
|
47
|
|
|
:param chat_id: 群聊id |
48
|
|
|
:return: 会话信息 |
49
|
|
|
""" |
50
|
|
|
res = self._get('appchat/get', params={'chatid': chat_id}) |
51
|
|
|
return res['chat_info'] |
52
|
|
|
|
53
|
10 |
|
def update(self, chat_id, name=None, owner=None, |
54
|
|
|
add_user_list=None, del_user_list=None): |
55
|
|
|
""" |
56
|
|
|
修改群聊会话 |
57
|
|
|
|
58
|
|
|
详情请参考 |
59
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90246 |
60
|
|
|
|
61
|
|
|
:param chat_id: 群聊id |
62
|
|
|
:param name: 新的群聊名。若不需更新,请忽略此参数。最多50个utf8字符,超过将截断 |
63
|
|
|
:param owner: 新群主的id。若不需更新,请忽略此参数 |
64
|
|
|
:param add_user_list: 会话新增成员列表,成员用userid来标识 |
65
|
|
|
:param del_user_list: 会话退出成员列表,成员用userid来标识 |
66
|
|
|
:return: 返回的 JSON 数据包 |
67
|
|
|
""" |
68
|
|
|
data = optionaldict( |
69
|
|
|
chatid=chat_id, |
70
|
|
|
name=name, |
71
|
|
|
owner=owner, |
72
|
|
|
add_user_list=add_user_list, |
73
|
|
|
del_user_list=del_user_list, |
74
|
|
|
) |
75
|
|
|
return self._post('appchat/update', data=data) |
76
|
|
|
|
77
|
10 |
|
def send(self, chat_id, msg_type, **kwargs): |
78
|
|
|
""" |
79
|
|
|
应用推送消息 |
80
|
|
|
|
81
|
|
|
详情请参考:https://work.weixin.qq.com/api/doc#90000/90135/90248 |
82
|
|
|
:param chat_id: 群聊id |
83
|
|
|
:param msg_type: 消息类型,可以为text/image/voice/video/file/textcard/news/mpnews/markdown |
84
|
|
|
:param kwargs: 具体消息类型的扩展参数 |
85
|
|
|
:return: |
86
|
|
|
""" |
87
|
|
|
data = { |
88
|
|
|
'chatid': chat_id, |
89
|
|
|
'safe': kwargs.get('safe') or 0 |
90
|
|
|
} |
91
|
|
|
data.update(self._build_msg_content(msg_type, **kwargs)) |
92
|
|
|
|
93
|
|
|
return self._post('appchat/send', data=data) |
94
|
|
|
|
95
|
10 |
|
def send_msg(self, chat_id, msg_type, **kwargs): |
96
|
|
|
""" deprecated, use `send` instead """ |
97
|
|
|
return self.send(chat_id, msg_type, **kwargs) |
98
|
|
|
|
99
|
10 |
|
def send_text(self, chat_id, content, safe=0): |
100
|
|
|
""" |
101
|
|
|
发送文本消息 |
102
|
|
|
|
103
|
|
|
详情请参考:https://work.weixin.qq.com/api/doc#90000/90135/90248/文本消息/ |
104
|
|
|
|
105
|
|
|
:param chat_id: 群聊id |
106
|
|
|
:param content: 消息内容 |
107
|
|
|
:param safe: 表示是否是保密消息,0表示否,1表示是,默认0 |
108
|
|
|
:return: |
109
|
|
|
""" |
110
|
|
|
return self.send(chat_id, 'text', safe=safe, content=content) |
111
|
|
|
|
112
|
10 |
|
def _build_msg_content(self, msgtype='text', **kwargs): |
113
|
|
|
""" |
114
|
|
|
构造消息内容 |
115
|
|
|
|
116
|
|
|
:param content: 消息内容,最长不超过2048个字节 |
117
|
|
|
:param msgtype: 消息类型,可以为text/image/voice/video/file/textcard/news/mpnews/markdown |
118
|
|
|
:param kwargs: 具体消息类型的扩展参数 |
119
|
|
|
:return: |
120
|
|
|
""" |
121
|
|
|
data = {'msgtype': msgtype} |
122
|
|
|
if msgtype == 'text': |
123
|
|
|
data[msgtype] = {'content': kwargs.get('content')} |
124
|
|
|
elif msgtype == 'image' or msgtype == 'voice' or msgtype == 'file': |
125
|
|
|
data[msgtype] = {'media_id': kwargs.get('media_id')} |
126
|
|
|
elif msgtype == 'video': |
127
|
|
|
data[msgtype] = { |
128
|
|
|
'media_id': kwargs.get('media_id'), |
129
|
|
|
'title': kwargs.get('title'), |
130
|
|
|
'description': kwargs.get('description') |
131
|
|
|
} |
132
|
|
|
elif msgtype == 'textcard': |
133
|
|
|
data[msgtype] = { |
134
|
|
|
'title': kwargs.get('title'), |
135
|
|
|
'description': kwargs.get('description'), |
136
|
|
|
'url': kwargs.get('url'), |
137
|
|
|
'btntxt': kwargs.get('btntxt'), |
138
|
|
|
} |
139
|
|
|
elif msgtype == 'news': |
140
|
|
|
# { |
141
|
|
|
# "articles" : |
142
|
|
|
# [ |
143
|
|
|
# { |
144
|
|
|
# "title" : "中秋节礼品领取", |
145
|
|
|
# "description" : "今年中秋节公司有豪礼相送", |
146
|
|
|
# "url":"https://zhidao.baidu.com/question/2073647112026042748.html", |
147
|
|
|
# "picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png" |
148
|
|
|
# } |
149
|
|
|
# ] |
150
|
|
|
# } |
151
|
|
|
data[msgtype] = kwargs |
152
|
|
|
elif msgtype == 'mpnews': |
153
|
|
|
# { |
154
|
|
|
# "articles":[ |
155
|
|
|
# { |
156
|
|
|
# "title": "地球一小时", |
157
|
|
|
# "thumb_media_id": "biz_get(image)", |
158
|
|
|
# "author": "Author", |
159
|
|
|
# "content_source_url": "https://work.weixin.qq.com", |
160
|
|
|
# "content": "3月24日20:30-21:30 \n办公区将关闭照明一小时,请各部门同事相互转告", |
161
|
|
|
# "digest": "3月24日20:30-21:30 \n办公区将关闭照明一小时" |
162
|
|
|
# } |
163
|
|
|
# ] |
164
|
|
|
# } |
165
|
|
|
data[msgtype] = kwargs |
166
|
|
|
elif msgtype == 'markdown': |
167
|
|
|
# { |
168
|
|
|
# "content": "您的会议室已经预定,稍后会同步到`邮箱` |
169
|
|
|
# >**事项详情** |
170
|
|
|
# >事 项:<font color=\"info\">开会</font> |
171
|
|
|
# >组织者:@miglioguan |
172
|
|
|
# >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang |
173
|
|
|
# > |
174
|
|
|
# >会议室:<font color=\"info\">广州TIT 1楼 301</font> |
175
|
|
|
# >日 期:<font color=\"warning\">2018年5月18日</font> |
176
|
|
|
# >时 间:<font color=\"comment\">上午9:00-11:00</font> |
177
|
|
|
# > |
178
|
|
|
# >请准时参加会议。 |
179
|
|
|
# > |
180
|
|
|
# >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)" |
181
|
|
|
# } |
182
|
|
|
data[msgtype] = kwargs |
183
|
|
|
else: |
184
|
|
|
raise TypeError('不能识别的msgtype: %s' % msgtype) |
185
|
|
|
return data |
186
|
|
|
|