-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.py
45 lines (42 loc) · 2.15 KB
/
notification.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
import config
import urllib.request
def sendNotificationFinish(type:str, name:str, done_what:str, at_where:str):
for key in config.notifications:
v = config.notifications[key]
if v['type'] == 'telegram':
if 'token' in v and 'chat_id' in v:
try:
text = f"[SUCCESS] Task {type} {name} finishes to {done_what} at {at_where}"
url = f'https://api.telegram.org/bot{v["token"]}/sendMessage'
data = urllib.parse.urlencode({'chat_id': v["chat_id"], 'text': text}).encode('utf-8')
opener = None
if config.proxies != None:
proxy_handler = urllib.request.ProxyHandler(config.proxies)
opener = urllib.request.build_opener(proxy_handler)
else:
opener = urllib.request.build_opener()
request = urllib.request.Request(url, data=data)
response = opener.open(request)
except Exception as e:
print(e)
def sendNotificationWarning(type:str, name:str, done_what:str, at_where:str):
for key in config.notifications:
v = config.notifications[key]
if v['type'] == 'telegram':
if 'token' in v and 'chat_id' in v:
try:
text = f"[WARNING] Task {type} {name} fails to {done_what} at {at_where}"
url = f'https://api.telegram.org/bot{v["token"]}/sendMessage'
data = urllib.parse.urlencode({'chat_id': v["chat_id"], 'text': text}).encode('utf-8')
opener = None
if config.proxies != None:
proxy_handler = urllib.request.ProxyHandler(config.proxies)
opener = urllib.request.build_opener(proxy_handler)
else:
opener = urllib.request.build_opener()
request = urllib.request.Request(url, data=data)
response = opener.open(request)
except Exception as e:
print(e)
def sendNotificationError():
pass