Fix Telegram link preview suppression for bot sends

This commit is contained in:
Kovyrin Family Claw 2026-04-13 11:53:12 -04:00 committed by Teknium
parent 192ef00bb2
commit 00ff9a26cd
4 changed files with 43 additions and 2 deletions

View file

@ -638,6 +638,16 @@ def load_gateway_config() -> GatewayConfig:
os.environ["TELEGRAM_IGNORED_THREADS"] = str(ignored_threads)
if "reactions" in telegram_cfg and not os.getenv("TELEGRAM_REACTIONS"):
os.environ["TELEGRAM_REACTIONS"] = str(telegram_cfg["reactions"]).lower()
if "disable_link_previews" in telegram_cfg:
plat_data = platforms_data.setdefault(Platform.TELEGRAM.value, {})
if not isinstance(plat_data, dict):
plat_data = {}
platforms_data[Platform.TELEGRAM.value] = plat_data
extra = plat_data.setdefault("extra", {})
if not isinstance(extra, dict):
extra = {}
plat_data["extra"] = extra
extra["disable_link_previews"] = telegram_cfg["disable_link_previews"]
whatsapp_cfg = yaml_cfg.get("whatsapp", {})
if isinstance(whatsapp_cfg, dict):

View file

@ -284,6 +284,22 @@ class TestLoadGatewayConfig:
assert config.unauthorized_dm_behavior == "ignore"
assert config.platforms[Platform.WHATSAPP].extra["unauthorized_dm_behavior"] == "pair"
def test_bridges_telegram_disable_link_previews_from_config_yaml(self, tmp_path, monkeypatch):
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
config_path = hermes_home / "config.yaml"
config_path.write_text(
"telegram:\n"
" disable_link_previews: true\n",
encoding="utf-8",
)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
config = load_gateway_config()
assert config.platforms[Platform.TELEGRAM].extra["disable_link_previews"] is True
class TestHomeChannelEnvOverrides:
"""Home channel env vars should apply even when the platform was already

View file

@ -577,7 +577,7 @@ class TestSendToPlatformChunking:
sent_calls = []
async def fake_send(token, chat_id, message, media_files=None, thread_id=None):
async def fake_send(token, chat_id, message, media_files=None, thread_id=None, disable_link_previews=False):
sent_calls.append(media_files or [])
return {"success": True, "platform": "telegram", "chat_id": chat_id, "message_id": str(len(sent_calls))}
@ -756,6 +756,17 @@ class TestSendTelegramHtmlDetection:
kwargs = bot.send_message.await_args.kwargs
assert kwargs["parse_mode"] == "MarkdownV2"
def test_disable_link_previews_sets_disable_web_page_preview(self, monkeypatch):
bot = self._make_bot()
_install_telegram_mock(monkeypatch, bot)
asyncio.run(
_send_telegram("tok", "123", "https://example.com", disable_link_previews=True)
)
kwargs = bot.send_message.await_args.kwargs
assert kwargs["disable_web_page_preview"] is True
def test_html_with_code_and_pre_tags(self, monkeypatch):
bot = self._make_bot()
_install_telegram_mock(monkeypatch, bot)

View file

@ -375,6 +375,7 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
# --- Telegram: special handling for media attachments ---
if platform == Platform.TELEGRAM:
last_result = None
disable_link_previews = bool(getattr(pconfig, "extra", {}) and pconfig.extra.get("disable_link_previews"))
for i, chunk in enumerate(chunks):
is_last = (i == len(chunks) - 1)
result = await _send_telegram(
@ -383,6 +384,7 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
chunk,
media_files=media_files if is_last else [],
thread_id=thread_id,
disable_link_previews=disable_link_previews,
)
if isinstance(result, dict) and result.get("error"):
return result
@ -484,7 +486,7 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
return last_result
async def _send_telegram(token, chat_id, message, media_files=None, thread_id=None):
async def _send_telegram(token, chat_id, message, media_files=None, thread_id=None, disable_link_previews=False):
"""Send via Telegram Bot API (one-shot, no polling needed).
Applies markdownMarkdownV2 formatting (same as the gateway adapter)
@ -520,6 +522,8 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
thread_kwargs = {}
if thread_id is not None:
thread_kwargs["message_thread_id"] = int(thread_id)
if disable_link_previews:
thread_kwargs["disable_web_page_preview"] = True
last_msg = None
warnings = []