diff --git a/notifications/telegram.py b/notifications/telegram.py index 047ae9a..90c7813 100644 --- a/notifications/telegram.py +++ b/notifications/telegram.py @@ -2,6 +2,8 @@ from __future__ import annotations +import re + from notifications.events import NotificationPublisher, RenderedNotification try: @@ -12,6 +14,18 @@ _merge_strategy_plugin_i18n = None +_TELEGRAM_MARKET_SYMBOL_LINK_RE = re.compile(r"(? str: + text = str(value or "") + return _TELEGRAM_MARKET_SYMBOL_LINK_RE.sub( + lambda match: f"{match.group(1)}.{_TELEGRAM_MARKET_SYMBOL_LINK_JOINER}{match.group(2)}", + text, + ) + + SIGNAL_ICONS = { "hold": "💎", "entry": "🚀", @@ -428,7 +442,11 @@ def send_tg_message(message): url = f"https://api.telegram.org/bot{token}/sendMessage" try: prefixed = with_prefix_fn(message) - requests_module.post(url, json={"chat_id": chat_id, "text": prefixed}, timeout=10) + requests_module.post( + url, + json={"chat_id": chat_id, "text": _break_telegram_market_symbol_auto_links(prefixed)}, + timeout=10, + ) except Exception as exc: print(f"Telegram send failed: {type(exc).__name__}", flush=True) diff --git a/tests/test_notifications.py b/tests/test_notifications.py index 92fed9c..5aa6df3 100644 --- a/tests/test_notifications.py +++ b/tests/test_notifications.py @@ -404,12 +404,12 @@ def test_build_sender_posts_prefixed_message(self): with_prefix_fn=build_prefixer("HK", "longbridge-quant-semiconductor-rotation-income-hk"), requests_module=fake_requests, ) - sender("hello") + sender("SOXL.US and 00700.HK") self.assertEqual(len(fake_requests.calls), 1) url, payload, timeout = fake_requests.calls[0] self.assertIn("token-1", url) self.assertEqual(payload["chat_id"], "chat-1") - self.assertEqual(payload["text"], "[HK] hello") + self.assertEqual(payload["text"], "[HK] SOXL.\u2060US and 00700.\u2060HK") self.assertEqual(timeout, 10) def test_build_issue_notifier_logs_and_sends(self):