-
Notifications
You must be signed in to change notification settings - Fork 151
/
send_release_notify.py
69 lines (51 loc) · 1.44 KB
/
send_release_notify.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
import requests
import os
bot_token = os.environ['TELEGRAM_TOKEN']
send_to = os.environ['TG_SEND_TO']
message_template = """New release of v%s
%s
Release page: [%s](%s)
"""
message_template2 = """New release of v%s
```
%s
```
Release page: [%s](%s)
"""
def send_message(msg: str):
data = {'text': msg, 'chat_id': send_to, 'parse_mode': 'markdown'}
resp = requests.post(f'https://api.telegram.org/bot{bot_token}/sendMessage', json=data)
data = resp.json()
# print(data)
if not data.get('ok'):
print(data)
raise RuntimeError(data.get('description'))
def get_all_release():
return requests.get('https://api.github.com/repos/triwinds/ns-emu-tools/releases').json()
def get_latest_release(prerelease=False):
data = get_all_release()
release_list = data if prerelease else [i for i in data if i['prerelease'] is False]
return release_list[0]
def main():
release_info = get_latest_release(True)
print(release_info)
message = message_template % (
release_info['tag_name'],
release_info['body'],
release_info['tag_name'],
release_info['html_url']
)
try:
send_message(message)
return
except:
pass
message = message_template2 % (
release_info['tag_name'],
release_info['body'],
release_info['tag_name'],
release_info['html_url']
)
send_message(message)
if __name__ == '__main__':
main()