Skip to content

The agent that grows with you

refactor(tools): share _NO_DELIVERABLE, tighten telegram format helper

Teknium0a9bc0f3e66a
diff --git a/tools/send_message_senders.py b/tools/send_message_senders.py
index 91e9f270636..e67bb7ebb7a 100644
--- a/tools/send_message_senders.py
+++ b/tools/send_message_senders.py
@@ -258,8 +258,7 @@ def _telegram_format(message):
"""``(formatted, parse_mode, has_html)``: text already containing HTML tags is sent as
HTML; otherwise Markdown -> MarkdownV2 via the adapter's ``format_message``."""
from telegram.constants import ParseMode
- has_html = bool(re.search(r'<[a-zA-Z/][^>]*>', message))
- if has_html:
+ if re.search(r'<[a-zA-Z/][^>]*>', message):
return message, ParseMode.HTML, True
try:
from plugins.platforms.telegram.adapter import TelegramAdapter
@@ -276,7 +275,6 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
formatted, send_parse_mode, _has_html = _telegram_format(message)
bot = _telegram_bot(token)
from plugins.platforms.telegram.telegram_ids import normalize_telegram_chat_id
-
# Telegram accepts a numeric chat_id OR an @username string; never force-int.
int_chat_id = normalize_telegram_chat_id(chat_id)
media_files = media_files or []
@@ -285,15 +283,14 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
text_kwargs = {**thread_kwargs, **({"disable_web_page_preview": True} if disable_link_previews else {})}
last_msg, warnings = None, []
- # MEDIA caption: a single captionable file + short text rides on the bubble as
- # its *formatted* caption. Formatting can inflate a raw <1024 string past
- # Telegram's cap, so re-check in UTF-16 units and fall back to a separate body.
+ # MEDIA caption: a single captionable file + short text rides on the bubble as its
+ # *formatted* caption. Formatting can inflate a raw <1024 string past Telegram's
+ # cap, so re-check in UTF-16 units and fall back to a separate body.
_tg_caption = None
from gateway.platforms.base import BasePlatformAdapter, utf16_len
_cap, _ = _media_caption_split(message, media_files, max_caption_len=_TELEGRAM_CAPTION_LIMIT)
if _cap is not None and utf16_len(formatted) <= _TELEGRAM_CAPTION_LIMIT:
- _tg_caption = formatted
- formatted = "" # suppress the separate text send below
+ _tg_caption, formatted = formatted, "" # suppress the separate text send below
if formatted.strip():
# Chunk *after* formatting, in UTF-16 units: MarkdownV2/HTML escaping inflates
diff --git a/tools/send_message_tool.py b/tools/send_message_tool.py
index 10162638254..f80ab846f25 100644
--- a/tools/send_message_tool.py
+++ b/tools/send_message_tool.py
@@ -16,7 +16,7 @@ from tools.send_message_targets import ( # noqa: F401
_HOME_CHANNEL_ENV_OVERRIDES, _SLACK_USER_ID_RE, _parse_target_ref, resolve_send_target)
from tools.send_message_senders import ( # noqa: F401
_AUDIO_EXTS, _DEFAULT_CAPTION_LIMIT, _IMAGE_EXTS, _TELEGRAM_CAPTION_LIMIT, _VIDEO_EXTS,
- _VOICE_EXTS, _adapter_media_method, _error, _live_adapter, _media_caption_split, _plugin_standalone_sender,
+ _NO_DELIVERABLE, _VOICE_EXTS, _adapter_media_method, _error, _live_adapter, _media_caption_split, _plugin_standalone_sender,
_registry_standalone_send, _resolve_slack_user_target, _sanitize_error_text,
_send_bluebubbles, _send_matrix_via_adapter, _send_qqbot, _send_signal, _send_telegram,
_send_weixin, _send_yuanbao)
@@ -335,9 +335,9 @@ async def _send_live_adapter_media(
if adapter_method is None or adapter_method is getattr(BasePlatformAdapter, method_name):
return {"error": (f"Live adapter does not implement native {media_kind} delivery; "
f"media file {index + 1}/{total} was not sent")}
- kwargs = {"caption": caption if index == 0 else None, "reply_to": thread_id, "metadata": metadata}
try:
- last_result = await getattr(adapter, method_name)(chat_id, media_path, **kwargs)
+ last_result = await getattr(adapter, method_name)(
+ chat_id, media_path, caption=caption if index == 0 else None, reply_to=thread_id, metadata=metadata)
except asyncio.CancelledError:
raise
except Exception as exc:
@@ -349,7 +349,7 @@ async def _send_live_adapter_media(
return {"error": f"Adapter media send failed after {index}/{total} files: {detail}"}
if last_result is None:
- return {"error": "No deliverable text or media remained after processing MEDIA tags"}
+ return {"error": _NO_DELIVERABLE}
return {"success": True, "message_id": last_result.message_id, "media_delivered": True}
@@ -388,8 +388,7 @@ async def _send_via_adapter(
**({"publish_topic": chat_id} if platform_name == "ntfy" and chat_id else {})} or None
if media_files:
return await _dispatch_on_gateway_loop(
- runner,
- lambda: _send_live_adapter_media(
+ runner, lambda: _send_live_adapter_media(
adapter, chat_id, chunk, media_files,
thread_id=thread_id, metadata=metadata, force_document=force_document),
"send_message: failed to schedule media send on gateway loop")