-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_telegram.py
48 lines (37 loc) · 1.2 KB
/
send_telegram.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
import requests
import logging
import yaml
from load_config import *
config = load_config('config.yml')
with open('auth/auth.yml') as file:
try:
creds = yaml.load(file, Loader=yaml.FullLoader)
bot_token = creds['telegram_token']
bot_chatID = str(creds['telegram_chat_id'])
valid_auth = True
except KeyError:
valid_auth = False
pass
class TelegramLogFilter(logging.Filter):
# filter for logRecords with TELEGRAM extra
def filter(self, record):
return hasattr(record, 'TELEGRAM')
class TelegramHandler(logging.Handler):
# log to telegram if the TELEGRAM extra matches an enabled key
def emit(self, record):
if not valid_auth:
return
key = getattr(record, 'TELEGRAM')
# unknown message key
if not key in config['TELEGRAM']['NOTIFICATIONS']:
return
# message key disabled
if not config['TELEGRAM']['NOTIFICATIONS'][key]:
return
requests.get(
'https://api.telegram.org/bot'
+ bot_token
+ '/sendMessage?chat_id='
+ bot_chatID
+ '&parse_mode=Markdown&text='
+ record.message)