-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwechatpush.py
More file actions
136 lines (125 loc) · 5.32 KB
/
Copy pathwechatpush.py
File metadata and controls
136 lines (125 loc) · 5.32 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import json
import requests
import base64
from requests.adapters import HTTPAdapter
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=5))
s.mount('https://', HTTPAdapter(max_retries=5))
def send_to_wecom(text, wecom_cid, wecom_aid, wecom_secret, wecom_touid='HuangJinKai'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = s.get(get_token_url).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": wecom_touid,
"agentid": wecom_aid,
"msgtype": "text",
"text": {
"content": text
},
"duplicate_check_interval": 600
}
response = s.post(send_msg_url, data=json.dumps(data)).content
return response
else:
return False
def send_to_wecom_image(base64_content, wecom_cid, wecom_aid, wecom_secret, wecom_touid='HuangJinKai'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = s.get(get_token_url, timeout=5).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image'
upload_response = s.post(upload_url, files={
"picture": base64.b64decode(base64_content)
}).json()
if "media_id" in upload_response:
media_id = upload_response['media_id']
else:
return False
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": wecom_touid,
"agentid": wecom_aid,
"msgtype": "image",
"image": {
"media_id": media_id
},
"duplicate_check_interval": 600
}
response = s.post(send_msg_url, data=json.dumps(data)).content
return response
else:
return False
def send_to_wecom_image_url(image_url, wecom_cid, wecom_aid, wecom_secret, wecom_touid='HuangJinKai'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = s.get(get_token_url, timeout=5).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image'
upload_response = s.post(upload_url, files={
"picture": s.get(image_url, timeout=5).content
}).json()
if "media_id" in upload_response:
media_id = upload_response['media_id']
else:
return False
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": wecom_touid,
"agentid": wecom_aid,
"msgtype": "image",
"image": {
"media_id": media_id
},
"duplicate_check_interval": 600
}
response = s.post(send_msg_url, data=json.dumps(data)).content
return response
else:
return False
def send_to_wecom_markdown(text, wecom_cid, wecom_aid, wecom_secret, wecom_touid='HuangJinKai'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = s.get(get_token_url, timeout=5).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": wecom_touid,
"agentid": wecom_aid,
"msgtype": "markdown",
"markdown": {
"content": text
},
"duplicate_check_interval": 600
}
response = s.post(send_msg_url, data=json.dumps(data)).content
return response
else:
return False
def send_to_wecom_news(news_title, news_desc, news_url, news_pic, wecom_cid, wecom_aid, wecom_secret, wecom_touid='HuangJinKai'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = s.get(get_token_url, timeout=5).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": wecom_touid,
"agentid": wecom_aid,
"msgtype": "news",
"news": {
"articles": [
{
"title": news_title,
"description": news_desc,
"url": news_url,
"picurl": news_pic,
}
]
},
"duplicate_check_interval": 600
}
response = s.post(send_msg_url, data=json.dumps(data)).content
return response
else:
return False