forked from lzcstar/ZJU-nCov-Hitcarder
-
Notifications
You must be signed in to change notification settings - Fork 65
/
message.py
61 lines (53 loc) · 1.66 KB
/
message.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
import requests
import json
import os
import time
def dingtalk(msg, dingtalk_token, tries=5):
dingtalk_url = 'https://oapi.dingtalk.com/robot/send?access_token='+dingtalk_token
data = {
"msgtype": "text",
"text": {
"content": msg
},
"at": {
"isAtAll": False
}
}
header = {'Content-Type': 'application/json'}
for _ in range(tries):
try:
r = requests.post(dingtalk_url,
data=json.dumps(data), headers=header).json()
print(r)
if r["errcode"] == 0:
return True
except:
pass
print('Retrying...')
time.sleep(5)
return False
def serverchan(text, desp, serverchan_key, tries=5):
text, desp = text[:100], desp[:100]
text = 'Server酱服务即将下线,请切换到其他通知通道(建议使用钉钉)\n' + text
for _ in range(tries):
try:
r = requests.get("https://sc.ftqq.com/" + serverchan_key
+ ".send?text=" + text + "&desp=" + desp).json()
print(r)
if r["errno"] == 0:
return True
except:
pass
print('Retrying...')
time.sleep(5)
return False
if __name__ == "__main__":
msg = "打卡"*1000
dingtalk_token = os.environ.get('DINGTALK_TOKEN')
if dingtalk_token:
ret = dingtalk(msg, dingtalk_token)
print('send_dingtalk_message', ret)
serverchan_key = os.environ.get('SERVERCHAN_KEY')
if serverchan_key:
ret = serverchan(msg, '', serverchan_key)
print('send_serverChan_message', ret)