-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram
executable file
·59 lines (43 loc) · 1.44 KB
/
telegram
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
#!/usr/bin/env python
# Notify via telegram
import os
import requests
import textwrap
def main():
token = os.environ['NOTIFY_PARAMETER_TOKEN']
chat_id = os.environ['NOTIFY_PARAMETER_CHAT_ID']
if os.environ['NOTIFY_WHAT'] == 'HOST':
message = host_message()
else:
message = service_message()
url = 'https://api.telegram.org/bot{token}/sendMessage'.format(token=token)
r = requests.get(url, params={
'chat_id': chat_id,
'text': message,
'parse_mode': 'Markdown',
})
r.raise_for_status()
def host_message():
return """
*{NOTIFY_NOTIFICATIONTYPE}* - [{NOTIFY_HOSTNAME}]({host_url}) is *{NOTIFY_HOSTSTATE}*
{NOTIFY_SHORTDATETIME}
{NOTIFY_HOSTOUTPUT}
""".strip().format(host_url=host_url(), **os.environ)
def service_message():
return """
*{NOTIFY_NOTIFICATIONTYPE}* - [{NOTIFY_HOSTNAME}]({host_url}) - [{NOTIFY_SERVICEDESC}]({service_url}) is *{NOTIFY_SERVICESTATE}*
{NOTIFY_SHORTDATETIME}
{NOTIFY_SERVICEOUTPUT}
""".strip().format(host_url=host_url(), service_url=service_url(), **os.environ)
def base_url():
url_prefix = os.environ['NOTIFY_PARAMETER_URL_PREFIX']
base_url = url_prefix.rstrip('/')
if base_url.endswith("/check_mk"):
base_url = base_url[:-9]
return base_url
def host_url():
return base_url() + os.environ['NOTIFY_HOSTURL']
def service_url():
return base_url() + os.environ['NOTIFY_SERVICEURL']
if __name__ == '__main__':
main()