Completed
Pull Request — master (#310)
by
unknown
18:39
created

WeChatMessage.send_video()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.5786

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 16
ccs 1
cts 6
cp 0.1666
crap 1.5786
rs 9.4285
c 0
b 0
f 0
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
        """ https://work.weixin.qq.com/api/doc#10167/文本卡片消息 """
49
        return self._send_message(
50
            agent_id,
51
            user_ids,
52
            party_ids,
53
            tag_ids,
54
            msg={
55
                'msgtype': 'textcard',
56
                'textcard': {
57
                    'title': title,
58
                    'description': description,
59
                    'url': url,
60
                    'btntxt': btntxt,
61
                },
62 10
            }
63
        )
64
65
    def send_image(self, agent_id, user_ids, media_id,
66
                   party_ids='', tag_ids='', safe=0):
67
        return self._send_message(
68
            agent_id,
69
            user_ids,
70
            party_ids,
71
            tag_ids,
72
            msg={
73
                'msgtype': 'image',
74
                'image': {
75
                    'media_id': media_id
76
                },
77
                'safe': safe
78 10
            }
79
        )
80
81
    def send_voice(self, agent_id, user_ids, media_id,
82
                   party_ids='', tag_ids='', safe=0):
83
        return self._send_message(
84
            agent_id,
85
            user_ids,
86
            party_ids,
87
            tag_ids,
88
            msg={
89
                'msgtype': 'voice',
90
                'voice': {
91
                    'media_id': media_id
92
                },
93
                'safe': safe
94
            }
95
        )
96
97 10
    def send_video(self, agent_id, user_ids, media_id, title=None,
98
                   description=None, party_ids='', tag_ids='', safe=0):
99
        video_data = optionaldict()
100
        video_data['media_id'] = media_id
101
        video_data['title'] = title
102
        video_data['description'] = description
103
104
        return self._send_message(
105
            agent_id,
106
            user_ids,
107
            party_ids,
108
            tag_ids,
109
            msg={
110
                'msgtype': 'video',
111
                'video': dict(video_data),
112
                'safe': safe
113 10
            }
114
        )
115
116
    def send_file(self, agent_id, user_ids, media_id,
117
                  party_ids='', tag_ids='', safe=0):
118
        return self._send_message(
119
            agent_id,
120
            user_ids,
121
            party_ids,
122
            tag_ids,
123
            msg={
124
                'msgtype': 'file',
125
                'file': {
126
                    'media_id': media_id
127
                },
128
                'safe': safe
129
            }
130
        )
131
132
    def send_articles(self, agent_id, user_ids, articles,
133
                      party_ids='', tag_ids=''):
134
        articles_data = []
135
        for article in articles:
136 10
            articles_data.append({
137
                'title': article['title'],
138
                'description': article['description'],
139
                'url': article['url'],
140
                'picurl': article['image']
141
            })
142
        return self._send_message(
143
            agent_id,
144
            user_ids,
145
            party_ids,
146
            tag_ids,
147
            msg={
148
                'msgtype': 'news',
149
                'news': {
150
                    'articles': articles_data
151
                }
152
            }
153
        )
154
155
    def send_mp_articles(self, agent_id, user_ids, articles,
156
                         party_ids='', tag_ids='', safe=0):
157
        articles_data = []
158
        for article in articles:
159
            articles_data.append({
160
                'thumb_media_id': article['thumb_media_id'],
161
                'author': article['author'],
162
                'title': article['title'],
163
                'content': article['content'],
164
                'content_source_url': article['content_source_url'],
165
                'digest': article['digest'],
166
                'show_cover_pic': article['show_cover_pic']
167
            })
168
        return self._send_message(
169
            agent_id,
170
            user_ids,
171
            party_ids,
172
            tag_ids,
173
            msg={
174
                'msgtype': 'mpnews',
175
                'mpnews': {
176
                    'articles': articles_data
177
                },
178
                'safe': safe
179
            }
180
        )
181