35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
|
import requests
|
|||
|
from loguru import logger
|
|||
|
from storage import Storage
|
|||
|
|
|||
|
s = Storage()
|
|||
|
|
|||
|
_TOKEN = '6767909836:AAFpsqtWeBNIBgSSi2_19rltEHOF0mrvTg0'
|
|||
|
_URL = 'https://api.telegram.org/bot'
|
|||
|
|
|||
|
|
|||
|
def _log(message):
|
|||
|
if message.record.get('level').name == "ERROR":
|
|||
|
icon = "❌ "
|
|||
|
elif message.record.get('level').name == "SUCCESS":
|
|||
|
icon = "✅ "
|
|||
|
else:
|
|||
|
icon = "ℹ️ "
|
|||
|
for chat_id in s.get_users():
|
|||
|
r = requests.post("{0}{1}/sendMessage".format(_URL, _TOKEN), {
|
|||
|
"chat_id": int(chat_id),
|
|||
|
"disable_notification": True,
|
|||
|
"text": icon + message
|
|||
|
})
|
|||
|
|
|||
|
if r.status_code >= 400:
|
|||
|
logger.error("Failed to send message: {0} {1}".format(r.status_code, r.text))
|
|||
|
|
|||
|
|
|||
|
def _filter_info_only(record):
|
|||
|
return record.get('level').name == "INFO" or record.get('level').name == "SUCCESS"
|
|||
|
|
|||
|
|
|||
|
logger.add(_log, format="{message}", filter=_filter_info_only)
|
|||
|
logger.add(_log, format=" {message}", filter=lambda record: record.get('level').name == "ERROR")
|