feat: render full agent lifecycle text from notification_body in Telegram
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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"<b>{esc.strip()}</b>")
|
||||||
|
else:
|
||||||
|
parts.append(esc)
|
||||||
|
return "\n".join(parts).strip()
|
||||||
|
|
||||||
|
|
||||||
def format_lifecycle_html(event: Event) -> str:
|
def format_lifecycle_html(event: Event) -> str:
|
||||||
details = _details_dict(event)
|
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()
|
lifecycle = _detail(details, "lifecycle", default="started").strip().lower()
|
||||||
trigger = _detail(details, "trigger", default="").strip().lower()
|
trigger = _detail(details, "trigger", default="").strip().lower()
|
||||||
header = _LIFECYCLE_HEADERS.get(lifecycle, f"📋 Lifecycle: {html_escape(lifecycle)}")
|
header = _LIFECYCLE_HEADERS.get(lifecycle, f"📋 Lifecycle: {html_escape(lifecycle)}")
|
||||||
|
|||||||
@@ -110,6 +110,29 @@ def test_lifecycle_started_template():
|
|||||||
assert "📡 Оповещение: SAC (Security Alert Center)" in text
|
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():
|
def test_lifecycle_footer_uses_telegram_via_agent_when_present():
|
||||||
host = Host(hostname="WIN01", display_name="H1", os_family="windows")
|
host = Host(hostname="WIN01", display_name="H1", os_family="windows")
|
||||||
event = Event(
|
event = Event(
|
||||||
|
|||||||
Reference in New Issue
Block a user