Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions gateway/platforms/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4799,14 +4799,19 @@ async def get_chat_info(self, chat_id: str) -> Dict[str, Any]:
)
return {"name": str(chat_id), "type": "dm", "error": str(e)}

def format_message(self, content: str) -> str:
def format_message(self, content: str, *, rewrite_tables: bool = True) -> str:
"""
Convert standard markdown to Telegram MarkdownV2 format.

Protected regions (code blocks, inline code) are extracted first so
their contents are never modified. Standard markdown constructs
(headers, bold, italic, links) are translated to MarkdownV2 syntax,
and all remaining special characters are escaped.

When *rewrite_tables* is True (the default), GFM pipe tables are
rewritten into Telegram-friendly bullet groups before escaping.
Set to False when the caller wants to preserve raw table syntax
(e.g. rich Telegram sends, standalone send_message tool).
"""
if not content:
return content
Expand All @@ -4825,7 +4830,8 @@ def _ph(value: str) -> str:

# 0) Rewrite GFM-style pipe tables into Telegram-friendly row groups
# before the normal MarkdownV2 conversions run.
text = _wrap_markdown_tables(text)
if rewrite_tables:
text = _wrap_markdown_tables(text)

# 1) Protect fenced code blocks (``` ... ```)
# Per MarkdownV2 spec, \ and ` inside pre/code must be escaped.
Expand Down
2 changes: 1 addition & 1 deletion tools/send_message_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
try:
from gateway.platforms.telegram import TelegramAdapter
_adapter = TelegramAdapter.__new__(TelegramAdapter)
formatted = _adapter.format_message(message)
formatted = _adapter.format_message(message, rewrite_tables=False)
except Exception:
# Fallback: send as-is if formatting unavailable
formatted = message
Expand Down
Loading