-
Notifications
You must be signed in to change notification settings - Fork 9
/
wechatpusher.py
86 lines (63 loc) · 2.36 KB
/
wechatpusher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
import json
class WeChatPusher:
"""企业微信推送"""
def __init__(self, corpid, agentid, corpsecret):
# 企业ID
self.corpid = corpid
# 企业应用ID
self.agentid = agentid
# 应用的凭证密钥
self.corpsecret = corpsecret
# access_token
self.access_token = ''
# access_token 获取调用接口
self.token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?'
# 应用消息发送接口
self.send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='
# 请求 session
self.sess = requests.Session()
def _get_token(self):
"""获取 access_token"""
res = json.loads(
self.sess.get(self.token_url + 'corpid=' + self.corpid + '&corpsecret=' + self.corpsecret).text)
if res['errcode'] != 0:
return res['errcode']
self.access_token = res['access_token']
return 0
def send(self, title, description='', url='', btntxt='', user='@all'):
"""应用消息发送"""
if description == '':
description = title
if url == '':
url = 'https://github.com/Mythologyli/WeChatPusher'
if self.access_token == '':
res_get = self._get_token() # 更新 access_token
if res_get != 0:
return res_get
data = {
'touser': user,
'toparty': '@all',
'totag': '@all',
'msgtype': 'textcard',
'agentid': self.agentid,
'textcard': {
'title': title,
'description': description,
'url': url,
'btntxt': btntxt
},
'safe': 0,
'enable_id_trans': 0,
'enable_duplicate_check': 0,
'duplicate_check_interval': 1800
}
res = json.loads(self.sess.post(url=(self.send_url + self.access_token), data=json.dumps(data)).text)
if res['errcode'] != 0: # 可能由 access_token 过期造成
res_get = self._get_token() # 更新 access_token
if res_get != 0:
return res_get
res = json.loads(self.sess.post(url=(self.send_url + self.access_token), data=json.dumps(data)).text)
return res['errcode']
else:
return 0