/status and /ongoing

This commit is contained in:
Alex Tau 2025-06-06 15:38:48 +03:00
parent 62a25410cc
commit 6cc3966221
6 changed files with 121 additions and 47 deletions

View file

@ -1,62 +0,0 @@
from dataclasses import dataclass
from telethon import TelegramClient
from telethon.sessions import MemorySession
from ..core import cvars
from .alert import Alert
from .enum import SEVERITY_TO_EMOJI, AlertType, Severity
async def get_client() -> TelegramClient:
config = cvars.config.get()
api_id, api_hash, bot_token = config.telegram.creds.split(",")
client = await TelegramClient(MemorySession(), api_id, api_hash, connection_retries=None).start(bot_token=bot_token)
client.parse_mode = "html"
return client
def format_message(alert: Alert, note: str) -> str:
severity_emoji = SEVERITY_TO_EMOJI[alert.severity]
note_formatted = f" - <i>{note}</i>" if note else ""
message = f"{severity_emoji} {alert.alert_type} Alert{note_formatted}\n{alert.message}"
return message
async def send_alert(alert: Alert, note: str = "") -> None:
try:
client = cvars.tg_client.get()
except LookupError: # being called standalone
# cvars.config.set(get_config())
# temp_client = True
# client = await get_client()
# cvars.matrix_client.set(client)
raise NotImplementedError # TODO
else:
... # temp_client = False
room_id = cvars.config.get().telegram.room_id
message = format_message(alert, note)
await client.send_message(entity=room_id, message=message)
# if temp_client:
# await client.close()
async def send_start_alert() -> None:
config = cvars.config.get()
await send_alert(
Alert(
alert_type=AlertType.BOOT,
message=f"Service running with enabled checks: {', '.join(config.enabled_check_sets)}",
severity=Severity.INFO,
)
)
async def send_stop_alert() -> None:
await send_alert(
Alert(
alert_type=AlertType.BOOT,
message="Service stopping.",
severity=Severity.INFO,
)
)