refactor: simplify markdown truncation
This commit is contained in:
@@ -68,7 +68,6 @@ def setup_logging(log_file: str | None) -> None:
|
|||||||
|
|
||||||
TELEGRAM_TEXT_LIMIT = TELEGRAM_HARD_LIMIT
|
TELEGRAM_TEXT_LIMIT = TELEGRAM_HARD_LIMIT
|
||||||
TELEGRAM_MARKDOWN_LIMIT = 3500
|
TELEGRAM_MARKDOWN_LIMIT = 3500
|
||||||
ELLIPSIS = "…"
|
|
||||||
|
|
||||||
|
|
||||||
def _clamp_tg_text(text: str, limit: int = TELEGRAM_TEXT_LIMIT) -> str:
|
def _clamp_tg_text(text: str, limit: int = TELEGRAM_TEXT_LIMIT) -> str:
|
||||||
@@ -85,18 +84,14 @@ def _send_markdown(
|
|||||||
reply_to_message_id: int | None = None,
|
reply_to_message_id: int | None = None,
|
||||||
disable_notification: bool = False,
|
disable_notification: bool = False,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
rendered_text, entities = render_markdown(text)
|
rendered, entities = render_markdown(text)
|
||||||
if len(rendered_text) > TELEGRAM_MARKDOWN_LIMIT:
|
if len(rendered) > TELEGRAM_MARKDOWN_LIMIT:
|
||||||
sep = "\n" + ELLIPSIS + "\n"
|
rendered = rendered[: TELEGRAM_MARKDOWN_LIMIT - 20] + "\n…(truncated)"
|
||||||
lines = rendered_text.splitlines()
|
|
||||||
tail = lines[-1] if lines else ""
|
|
||||||
max_head = max(0, TELEGRAM_MARKDOWN_LIMIT - len(sep) - len(tail))
|
|
||||||
rendered_text = rendered_text[:max_head] + sep + tail
|
|
||||||
entities = None
|
entities = None
|
||||||
return bot.send_message(
|
return bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
text=rendered_text,
|
text=rendered,
|
||||||
entities=entities or None,
|
entities=entities,
|
||||||
reply_to_message_id=reply_to_message_id,
|
reply_to_message_id=reply_to_message_id,
|
||||||
disable_notification=disable_notification,
|
disable_notification=disable_notification,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user