WeChatMessage.send_video()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.5786

Importance

Changes 0
Metric Value
cc 1
eloc 15
nop 9
dl 0
loc 16
rs 9.65
c 0
b 0
f 0
ccs 1
cts 6
cp 0.1666
crap 1.5786

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 WeChatMessage(BaseWeChatAPI):
10
    """
11
    发送应用消息
12
13
    https://work.weixin.qq.com/api/doc#90000/90135/90236
14
15
    支持:
16
    * 文本消息
17
    * 图片消息
18
    * 语音消息
19
    * 视频消息
20
    * 文件消息
21
    * 文本卡片消息
22
    * 图文消息
23
    * 图文消息(mpnews)
24
    * markdown消息
25
    * 小程序通知消息
26
    """
27
28 10
    def send(self, agent_id, user_ids, party_ids='',
29
             tag_ids='', msg=None):
30
        """
31
        通用的消息发送接口。msg 内需要指定 msgtype 和对应类型消息必须的字段。
32
        如果部分接收人无权限或不存在,发送仍然执行,但会返回无效的部分(即invaliduser或invalidparty或invalidtag),常见的原因是接收人不在应用的可见范围内。
33
        user_ids、party_ids、tag_ids 不能同时为空,后面不再强调。
34
35
        :param agent_id: 必填,企业应用的id,整型。可在应用的设置页面查看。
36
        :param user_ids: 成员ID列表。
37
        :param party_ids: 部门ID列表。
38
        :param tag_ids: 标签ID列表。
39
        :param msg: 发送消息的 dict 对象
40
        :type msg: dict | None
41
        :return: 接口调用结果
42
        """
43
        msg = msg or {}
44
        if isinstance(user_ids, (tuple, list)):
45
            user_ids = '|'.join(user_ids)
46
        if isinstance(party_ids, (tuple, list)):
47
            party_ids = '|'.join(party_ids)
48
        if isinstance(tag_ids, (tuple, list)):
49
            tag_ids = '|'.join(tag_ids)
50
51
        data = {
52
            'touser': user_ids,
53
            'toparty': party_ids,
54
            'totag': tag_ids,
55
            'agentid': agent_id
56
        }
57
        data.update(msg)
58
        return self._post('message/send', data=data)
59
60 10
    def send_text(self, agent_id, user_ids, content,
61
                  party_ids='', tag_ids='', safe=0):
62
        return self.send(
63
            agent_id,
64
            user_ids,
65
            party_ids,
66
            tag_ids,
67
            msg={
68
                'msgtype': 'text',
69
                'text': {'content': content},
70
                'safe': safe
71
            }
72
        )
73
74 10
    def send_text_card(self, agent_id, user_ids, title, description, url, btntxt='详情',
75
                       party_ids='', tag_ids=''):
76
        """ 文本卡片消息
77
78
        https://work.weixin.qq.com/api/doc#90000/90135/90236/文本卡片消息
79
80
        请求示例:
81
        {
82
           "touser" : "UserID1|UserID2|UserID3",
83
           "toparty" : "PartyID1 | PartyID2",
84
           "totag" : "TagID1 | TagID2",
85
           "msgtype" : "textcard",
86
           "agentid" : 1,
87
           "textcard" : {
88
                "title" : "领奖通知",
89
                "description" : "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,
90
                    领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
91
                "url" : "URL",
92
                "btntxt":"更多"
93
           }
94
        }
95
96
        特殊说明:
97
        卡片消息的展现形式非常灵活,支持使用br标签或者空格来进行换行处理,也支持使用div标签来使用不同的字体颜色,
98
        目前内置了3种文字颜色:灰色(gray)、高亮(highlight)、默认黑色(normal),将其作为div标签的class属性即可,
99
        具体用法请参考上面的示例。
100
101
        :param agent_id: 必填,企业应用的id,整型。可在应用的设置页面查看。
102
        :param user_ids: 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。
103
        :param title: 标题,不超过128个字节,超过会自动截断。
104
        :param description: 必填,描述,不超过512个字节,超过会自动截断
105
        :param url: 必填,点击后跳转的链接。
106
        :param btntxt: 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。
107
        :param party_ids: 部门ID列表。
108
        :param tag_ids: 标签ID列表。
109
        """
110
        return self.send(
111
            agent_id,
112
            user_ids,
113
            party_ids,
114
            tag_ids,
115
            msg={
116
                'msgtype': 'textcard',
117
                'textcard': {
118
                    'title': title,
119
                    'description': description,
120
                    'url': url,
121
                    'btntxt': btntxt,
122
                },
123
            }
124
        )
125
126 10
    def send_image(self, agent_id, user_ids, media_id,
127
                   party_ids='', tag_ids='', safe=0):
128
        return self.send(
129
            agent_id,
130
            user_ids,
131
            party_ids,
132
            tag_ids,
133
            msg={
134
                'msgtype': 'image',
135
                'image': {
136
                    'media_id': media_id
137
                },
138
                'safe': safe
139
            }
140
        )
141
142 10
    def send_voice(self, agent_id, user_ids, media_id,
143
                   party_ids='', tag_ids='', safe=0):
144
        return self.send(
145
            agent_id,
146
            user_ids,
147
            party_ids,
148
            tag_ids,
149
            msg={
150
                'msgtype': 'voice',
151
                'voice': {
152
                    'media_id': media_id
153
                },
154
                'safe': safe
155
            }
156
        )
157
158 10
    def send_video(self, agent_id, user_ids, media_id, title=None,
159
                   description=None, party_ids='', tag_ids='', safe=0):
160
        video_data = optionaldict()
161
        video_data['media_id'] = media_id
162
        video_data['title'] = title
163
        video_data['description'] = description
164
165
        return self.send(
166
            agent_id,
167
            user_ids,
168
            party_ids,
169
            tag_ids,
170
            msg={
171
                'msgtype': 'video',
172
                'video': dict(video_data),
173
                'safe': safe
174
            }
175
        )
176
177 10
    def send_file(self, agent_id, user_ids, media_id,
178
                  party_ids='', tag_ids='', safe=0):
179
        return self.send(
180
            agent_id,
181
            user_ids,
182
            party_ids,
183
            tag_ids,
184
            msg={
185
                'msgtype': 'file',
186
                'file': {
187
                    'media_id': media_id
188
                },
189
                'safe': safe
190
            }
191
        )
192
193 10
    def send_articles(self, agent_id, user_ids, articles,
194
                      party_ids='', tag_ids=''):
195
        articles_data = []
196
        for article in articles:
197
            articles_data.append({
198
                'title': article['title'],
199
                'description': article['description'],
200
                'url': article['url'],
201
                'picurl': article['image']
202
            })
203
        return self.send(
204
            agent_id,
205
            user_ids,
206
            party_ids,
207
            tag_ids,
208
            msg={
209
                'msgtype': 'news',
210
                'news': {
211
                    'articles': articles_data
212
                }
213
            }
214
        )
215
216 10
    def send_mp_articles(self, agent_id, user_ids, articles,
217
                         party_ids='', tag_ids='', safe=0):
218
        articles_data = []
219
        for article in articles:
220
            articles_data.append({
221
                'thumb_media_id': article['thumb_media_id'],
222
                'author': article['author'],
223
                'title': article['title'],
224
                'content': article['content'],
225
                'content_source_url': article['content_source_url'],
226
                'digest': article['digest'],
227
                'show_cover_pic': article['show_cover_pic']
228
            })
229
        return self.send(
230
            agent_id,
231
            user_ids,
232
            party_ids,
233
            tag_ids,
234
            msg={
235
                'msgtype': 'mpnews',
236
                'mpnews': {
237
                    'articles': articles_data
238
                },
239
                'safe': safe
240
            }
241
        )
242
243 10
    def send_markdown(self, agent_id, user_ids, content, party_ids='', tag_ids=''):
244
        """markdown消息
245
246
        https://work.weixin.qq.com/api/doc#90000/90135/90236/markdown%E6%B6%88%E6%81%AF
247
248
        > 目前仅支持markdown语法的子集
249
        > 微工作台(原企业号)不支持展示markdown消息
250
251
        :param agent_id: 企业应用的id,整型。可在应用的设置页面查看
252
        :type agent_id: string
253
        :param content: markdown内容,最长不超过2048个字节,必须是utf8编码
254
        :type content: string
255
        :param user_ids: 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。
256
            特殊情况:指定为@all,则向关注该企业应用的全部成员发送
257
        :type user_ids: list or tuple or string
258
        :param party_ids: 部门ID列表,最多支持100个。当touser为@all时忽略本参数
259
        :type party_ids: list or tuple or string
260
        :param tag_ids: 标签ID列表,最多支持100个。当touser为@all时忽略本参数
261
        :type tag_ids: list or tuple or string
262
        :return: 接口调用结果
263
        :rtype: dict
264
        """
265
        msg = {
266
            "msgtype": "markdown",
267
            "markdown": {"content": content}
268
        }
269
        return self.send(
270
            agent_id,
271
            user_ids,
272
            party_ids,
273
            tag_ids,
274
            msg=msg
275
        )
276