[V10.321] 修正 Telegram HTML br 發送格式
All checks were successful
CD Pipeline / deploy (push) Successful in 1m6s

This commit is contained in:
OoO
2026-05-20 12:18:57 +08:00
parent ca27f2e4eb
commit 854e13201f
5 changed files with 54 additions and 6 deletions

View File

@@ -1,4 +1,41 @@
from services.telegram_templates import triaged_alert
from services import telegram_templates
from services.telegram_templates import _sanitize_telegram_html, triaged_alert
def test_telegram_html_sanitizer_converts_br_tags_to_newlines():
msg = _sanitize_telegram_html("第一行<br>第二行<br/>第三行<BR />第四行")
assert "<br" not in msg.lower()
assert msg == "第一行\n第二行\n第三行\n第四行"
assert _sanitize_telegram_html("第一行<br>第二行", parse_mode=None) == "第一行<br>第二行"
def test_send_telegram_with_result_sanitizes_html_payload(monkeypatch):
sent_payloads = []
class Response:
ok = True
status_code = 200
text = "ok"
def fake_post(url, json=None, timeout=None):
sent_payloads.append({"url": url, "json": json, "timeout": timeout})
return Response()
monkeypatch.setattr(telegram_templates, "_get_bot_token", lambda: "telegram-token")
monkeypatch.setattr("requests.post", fake_post)
result = telegram_templates.send_telegram_with_result(
"第一行<br>第二行<br/>第三行",
chat_ids=[101, 202],
parse_mode="HTML",
)
assert result["ok"] is True
assert result["sent"] == 2
assert [item["json"]["chat_id"] for item in sent_payloads] == [101, 202]
assert all(item["json"]["text"] == "第一行\n第二行\n第三行" for item in sent_payloads)
assert all(item["json"]["parse_mode"] == "HTML" for item in sent_payloads)
def test_ea_escalation_uses_structured_incident_brief():