feat: HTML Telegram templates for RDP/SSH events (notif-30)
Agent-style messages from event details, parse_mode HTML, templates for login/sudo/RDG and problems; unit tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from sqlalchemy.orm import Session
|
||||
from app.models import Event, Problem
|
||||
from app.services.notification_severity import severity_meets_minimum
|
||||
from app.services.notification_settings import TelegramConfig, get_effective_telegram_config
|
||||
from app.services.telegram_templates import format_event_telegram_html, format_problem_telegram_html
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -20,7 +21,13 @@ class TelegramSendError(Exception):
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
def send_telegram_text(message: str, *, config: TelegramConfig | None = None, force: bool = False) -> None:
|
||||
def send_telegram_text(
|
||||
message: str,
|
||||
*,
|
||||
config: TelegramConfig | None = None,
|
||||
force: bool = False,
|
||||
parse_mode: str | None = "HTML",
|
||||
) -> None:
|
||||
cfg = config or get_effective_telegram_config()
|
||||
if not force and not cfg.active:
|
||||
return
|
||||
@@ -30,14 +37,23 @@ def send_telegram_text(message: str, *, config: TelegramConfig | None = None, fo
|
||||
return
|
||||
|
||||
url = f"https://api.telegram.org/bot{cfg.bot_token}/sendMessage"
|
||||
payload = {"chat_id": cfg.chat_id, "text": message, "disable_web_page_preview": True}
|
||||
payload: dict[str, str | bool] = {
|
||||
"chat_id": cfg.chat_id,
|
||||
"text": message,
|
||||
"disable_web_page_preview": True,
|
||||
}
|
||||
if parse_mode:
|
||||
payload["parse_mode"] = parse_mode
|
||||
try:
|
||||
with httpx.Client(timeout=8.0) as client:
|
||||
response = client.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
except httpx.HTTPStatusError as exc:
|
||||
detail = exc.response.text[:200] if exc.response is not None else str(exc)
|
||||
raise TelegramSendError(f"Telegram API HTTP {exc.response.status_code}: {detail}", status_code=exc.response.status_code) from exc
|
||||
raise TelegramSendError(
|
||||
f"Telegram API HTTP {exc.response.status_code}: {detail}",
|
||||
status_code=exc.response.status_code,
|
||||
) from exc
|
||||
except Exception as exc:
|
||||
logger.exception("telegram send failed")
|
||||
raise TelegramSendError(str(exc)) from exc
|
||||
@@ -50,7 +66,7 @@ def send_telegram_test_message(*, config: TelegramConfig | None = None) -> None:
|
||||
if not cfg.configured:
|
||||
raise TelegramNotConfiguredError("Не задан bot token или chat_id")
|
||||
send_telegram_text(
|
||||
"✅ SAC: тестовое сообщение\nКанал Telegram для оповещений работает.",
|
||||
"✅ <b>SAC: тестовое сообщение</b>\nКанал Telegram для оповещений работает.",
|
||||
config=cfg,
|
||||
force=True,
|
||||
)
|
||||
@@ -62,39 +78,26 @@ def notify_event(event: Event, *, db: Session | None = None, apply_policy_gate:
|
||||
return
|
||||
if apply_policy_gate and not severity_meets_minimum(event.severity, cfg.min_severity):
|
||||
return
|
||||
host = event.host.hostname if event.host else "unknown"
|
||||
message = (
|
||||
f"🚨 SAC событие\n"
|
||||
f"Host: {host}\n"
|
||||
f"Severity: {event.severity}\n"
|
||||
f"Type: {event.type}\n"
|
||||
f"Title: {event.title}\n"
|
||||
f"Summary: {event.summary}"
|
||||
)
|
||||
message = format_event_telegram_html(event)
|
||||
try:
|
||||
send_telegram_text(message, config=cfg)
|
||||
except TelegramSendError:
|
||||
logger.exception("notify_event telegram failed event_id=%s", event.event_id)
|
||||
|
||||
|
||||
def notify_problem(problem: Problem, event: Event | None = None, *, db: Session | None = None, apply_policy_gate: bool = True) -> None:
|
||||
def notify_problem(
|
||||
problem: Problem,
|
||||
event: Event | None = None,
|
||||
*,
|
||||
db: Session | None = None,
|
||||
apply_policy_gate: bool = True,
|
||||
) -> None:
|
||||
cfg = get_effective_telegram_config(db)
|
||||
if not cfg.active:
|
||||
return
|
||||
if apply_policy_gate and not severity_meets_minimum(problem.severity, cfg.min_severity):
|
||||
return
|
||||
host = problem.host.hostname if problem.host else "unknown"
|
||||
related = ""
|
||||
if event is not None:
|
||||
related = f"\nEvent: {event.type} ({event.severity})"
|
||||
message = (
|
||||
f"🔥 SAC Problem opened\n"
|
||||
f"Host: {host}\n"
|
||||
f"Severity: {problem.severity}\n"
|
||||
f"Rule: {problem.rule_id or '-'}\n"
|
||||
f"Title: {problem.title}\n"
|
||||
f"Summary: {problem.summary}{related}"
|
||||
)
|
||||
message = format_problem_telegram_html(problem, event)
|
||||
try:
|
||||
send_telegram_text(message, config=cfg)
|
||||
except TelegramSendError:
|
||||
|
||||
Reference in New Issue
Block a user