From 4baa2b426f388fa8c2a3096a07b699b8693b0496 Mon Sep 17 00:00:00 2001 From: PTah Date: Sun, 31 May 2026 18:11:17 +1000 Subject: [PATCH] feat: render full agent lifecycle text from notification_body in Telegram Co-authored-by: Cursor --- backend/app/services/telegram_templates.py | 18 +++++++++++++++++ backend/tests/test_telegram_templates.py | 23 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/backend/app/services/telegram_templates.py b/backend/app/services/telegram_templates.py index bb2f776..ea05880 100644 --- a/backend/app/services/telegram_templates.py +++ b/backend/app/services/telegram_templates.py @@ -309,8 +309,26 @@ _LIFECYCLE_TRIGGER_LABELS: dict[str, str] = { } +def _format_lifecycle_body_html(body: str) -> str: + text = body.replace("\r\n", "\n").strip() + if not text: + return "" + parts: list[str] = [] + for i, line in enumerate(text.split("\n")): + esc = html.escape(line) + if i == 0 and line.strip(): + parts.append(f"{esc.strip()}") + else: + parts.append(esc) + return "\n".join(parts).strip() + + def format_lifecycle_html(event: Event) -> str: details = _details_dict(event) + notification_body = details.get("notification_body") + if isinstance(notification_body, str) and notification_body.strip(): + return _format_lifecycle_body_html(notification_body) + lifecycle = _detail(details, "lifecycle", default="started").strip().lower() trigger = _detail(details, "trigger", default="").strip().lower() header = _LIFECYCLE_HEADERS.get(lifecycle, f"📋 Lifecycle: {html_escape(lifecycle)}") diff --git a/backend/tests/test_telegram_templates.py b/backend/tests/test_telegram_templates.py index b1884c3..6e84b59 100644 --- a/backend/tests/test_telegram_templates.py +++ b/backend/tests/test_telegram_templates.py @@ -110,6 +110,29 @@ def test_lifecycle_started_template(): assert "📡 Оповещение: SAC (Security Alert Center)" in text +def test_lifecycle_notification_body_renders_full_text(): + host = Host(hostname="WIN01", display_name="K6A-DC3", os_family="windows") + body = "✅ Мониторинг логинов ЗАПУЩЕН\n🖥️ Сервер: K6A-DC3 (10.0.0.1)\n🕐 Время запуска: 28.05.2026 12:00:00" + event = Event( + event_id="00000000-0000-4000-8000-000000000507", + host_id=1, + host=host, + occurred_at=datetime(2026, 5, 29, 9, 0, tzinfo=timezone.utc), + category="agent", + type="agent.lifecycle", + severity="info", + title="started", + summary="Мониторинг запущен", + details={"lifecycle": "started", "trigger": "boot", "notification_body": body, "telegram_via": "sac"}, + payload={"source": {"product": "rdp-login-monitor", "product_version": "1.2.31-SAC"}}, + ) + text = format_event_telegram_html(event) + assert "✅ Мониторинг логинов ЗАПУЩЕН" in text + assert "K6A-DC3 (10.0.0.1)" in text + assert "Агент запущен" not in text + assert "📡 Оповещение: SAC (Security Alert Center)" in text + + def test_lifecycle_footer_uses_telegram_via_agent_when_present(): host = Host(hostname="WIN01", display_name="H1", os_family="windows") event = Event(