-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwxapp.py
216 lines (187 loc) · 6.64 KB
/
wxapp.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import requests, json, os
import time
import log
# 企业 id
CORP_ID = os.getenv("WECHAT_CORP_ID")
# 应用的凭证密钥
CORP_SECRET = os.getenv("WECHAT_CORP_SECRET")
# 应用的 agentid
AGENT_ID = int(os.getenv("WECHAT_AGENT_ID", "0"))
# 用户 id,推送给所有人时,设置为 "@all"
USER_ID = os.getenv("WECHAT_USER_ID", "@all")
# TOKEN
TOKEN = {
"access_token": None,
"expires_in": 7200,
"expires_time": None,
}
TOKEN_FILE = "_tmp_wechat.json"
# 获取应用 token 的 url
GET_TOKEN_URL = (
"https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
+ f"corpid={CORP_ID}&corpsecret={CORP_SECRET}"
)
# 消息推送 url
SEND_MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
def get_access_token():
"""
Retrieves the access token required for authentication.
Returns:
str: The access token.
Raises:
requests.exceptions.ConnectionError: If there is a connection error.
Exception: If there is an error retrieving the access token.
"""
global TOKEN
current_time = time.time()
if TOKEN["access_token"] and TOKEN["expires_time"] > current_time:
return TOKEN["access_token"]
if os.path.exists(TOKEN_FILE):
with open(TOKEN_FILE, "r") as f:
TOKEN = json.load(f)
if TOKEN["expires_time"] > current_time:
return TOKEN["access_token"]
try:
res = requests.get(GET_TOKEN_URL)
res.raise_for_status()
if res.json()["errcode"] != 0:
raise Exception(f"{res.text}")
log.logger.debug(log.SensitiveData(res.text))
# Update token
TOKEN["access_token"] = res.json()["access_token"]
TOKEN["expires_in"] = res.json()["expires_in"]
TOKEN["expires_time"] = current_time + TOKEN["expires_in"]
log.logger.info(
f"Update access token successful. Token: {TOKEN['access_token']}"
)
with open(TOKEN_FILE, "w") as f:
json.dump(TOKEN, f)
return TOKEN["access_token"]
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Get access token failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Get access token failed. Error: {e}")
raise e
def send_text(content):
payload = {
"touser": USER_ID,
"agentid": AGENT_ID,
"safe": 0,
"msgtype": "text",
"text": {
"content": content,
},
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
send_msg_url = SEND_MSG_URL + get_access_token()
try:
res = requests.post(send_msg_url, json=payload)
res.raise_for_status()
if res.json()["errcode"] != 0:
raise Exception(res.text)
log.logger.debug(f"Send text message successful. Response: {res.json()}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Send text message failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Send text message failed. Error: {e}")
raise e
def send_markdown(content):
payload = {
"touser": USER_ID,
"agentid": AGENT_ID,
"safe": 0,
"msgtype": "markdown",
"markdown": {
"content": content,
},
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
send_msg_url = SEND_MSG_URL + get_access_token()
try:
res = requests.post(send_msg_url, json=payload)
res.raise_for_status()
if res.json()["errcode"] != 0:
raise Exception(res.text)
log.logger.debug(f"Send markdown message successful. Response: {res.json()}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Send markdown message failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Send markdown message failed. Error: {e}")
raise e
def send_news_notice(card_detail):
payload = {
"touser": USER_ID,
"agentid": AGENT_ID,
"safe": 0,
"msgtype": "template_card",
"template_card": card_detail,
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
send_msg_url = SEND_MSG_URL + get_access_token()
try:
res = requests.post(send_msg_url, json=payload)
res.raise_for_status()
if res.json()["errcode"] != 0:
raise Exception(f"Send news notice failed. {res.text}")
log.logger.debug(f"Send news notice successful. Response: {res.json()}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Send news notice failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Send news notice failed. Error: {e}")
raise e
def send_welcome_card(welcome):
payload = {
"touser": USER_ID,
"agentid": AGENT_ID,
"safe": 0,
"msgtype": "template_card",
"template_card": {
"card_type": "text_notice",
"source": {
"desc": "🚀 Emby Notifier",
"desc_color": 0,
},
"main_title": {
"title": f"🎉 {welcome['content']}",
},
"quote_area": {
"quote_text": f"{welcome['intro']}",
},
"horizontal_content_list": [
{"keyname": "Author", "value": f"{welcome['author']}"},
{"keyname": "Version", "value": f"{welcome['version']}"},
{"keyname": "Update Time", "value": f"{welcome['update_time']}"},
],
"jump_list": [
{
"type": 1,
"url": f"{welcome['repo']}",
"title": "👾 github",
},
],
"card_action": {
"type": 1,
"url": f"{welcome['repo']}",
},
},
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
send_msg_url = SEND_MSG_URL + get_access_token()
try:
res = requests.post(send_msg_url, json=payload)
res.raise_for_status()
if res.json()["errcode"] != 0:
raise Exception(f"{res.text}")
log.logger.debug(f"Send news notice successful. Response: {res.json()}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Send news notice failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Send news notice failed. Error: {e}")
raise e