fix(notify): switch Telegram parse_mode from Markdown to HTML
Underscores in alert text (dark_green, FIRE_BUY) broke Telegram's legacy Markdown parser, causing ok:false → retries exhausted → failed. HTML parse_mode is more robust and doesn't treat _ as italic. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import html as _html
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ class TelegramNotifier:
|
|||||||
return _BASE.format(token=self._token, method=method)
|
return _BASE.format(token=self._token, method=method)
|
||||||
|
|
||||||
def send(self, alert: Alert) -> None:
|
def send(self, alert: Alert) -> None:
|
||||||
text = f"*{alert.title}*\n{alert.body}"
|
text = f"<b>{_html.escape(alert.title)}</b>\n{_html.escape(alert.body)}"
|
||||||
|
|
||||||
if alert.image_path and Path(alert.image_path).exists():
|
if alert.image_path and Path(alert.image_path).exists():
|
||||||
with open(alert.image_path, "rb") as fh:
|
with open(alert.image_path, "rb") as fh:
|
||||||
@@ -31,7 +32,7 @@ class TelegramNotifier:
|
|||||||
data={
|
data={
|
||||||
"chat_id": self._chat_id,
|
"chat_id": self._chat_id,
|
||||||
"caption": text,
|
"caption": text,
|
||||||
"parse_mode": "Markdown",
|
"parse_mode": "HTML",
|
||||||
},
|
},
|
||||||
files={"photo": fh},
|
files={"photo": fh},
|
||||||
timeout=10,
|
timeout=10,
|
||||||
@@ -42,7 +43,7 @@ class TelegramNotifier:
|
|||||||
json={
|
json={
|
||||||
"chat_id": self._chat_id,
|
"chat_id": self._chat_id,
|
||||||
"text": text,
|
"text": text,
|
||||||
"parse_mode": "Markdown",
|
"parse_mode": "HTML",
|
||||||
},
|
},
|
||||||
timeout=10,
|
timeout=10,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ def test_telegram_send_ok() -> None:
|
|||||||
n = TelegramNotifier("token", "chat123", session=session)
|
n = TelegramNotifier("token", "chat123", session=session)
|
||||||
n.send(_alert("Hi"))
|
n.send(_alert("Hi"))
|
||||||
assert len(session.calls) == 1
|
assert len(session.calls) == 1
|
||||||
assert "*Hi*" in session.calls[0]["json"]["text"]
|
assert "<b>Hi</b>" in session.calls[0]["json"]["text"]
|
||||||
|
|
||||||
|
|
||||||
def test_telegram_429_raises() -> None:
|
def test_telegram_429_raises() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user