forked from lzcstar/ZJU-nCov-Hitcarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
91 lines (79 loc) · 2.6 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
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
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 pushplus(title, content, pushplus_token, tries=5):
title, content = title[:100], content[:100]
title = '微信通知服务可能即将下线,请切换到其他通知通道(建议使用钉钉)\n' + title
pushplus_url = 'http://pushplus.hxtrip.com/customer/push/send'
data = {
"token": pushplus_token,
"title": title,
"content": content
}
headers = {'Content-Type': 'application/json'}
for _ in range(tries):
try:
r = requests.post(pushplus_url, data=json.dumps(data),
headers=headers).text
print(r)
if '<code>200</code>' in r:
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)
pushplus_token = os.environ.get('PUSHPLUS_TOKEN')
if pushplus_token:
ret = pushplus(msg, '', pushplus_token)
print('send_pushplus_message', ret)