b2bcenter-parser/telegram_logs.py

38 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import json
import loguru
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,
# "reply_markup": json.dumps({"keyboard": [[{"text": "Остановить Бот"}]]})
})
if r.status_code != 200:
print(r.json())
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")