fix(telegram): preserve topic metadata on overflow edits
This commit is contained in:
@@ -1839,6 +1839,7 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
content: str,
|
||||
*,
|
||||
finalize: bool = False,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
) -> SendResult:
|
||||
"""Edit a previously sent Telegram message.
|
||||
|
||||
@@ -1857,7 +1858,7 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
# without round-tripping a doomed edit.
|
||||
if utf16_len(content) > self.MAX_MESSAGE_LENGTH:
|
||||
return await self._edit_overflow_split(
|
||||
chat_id, message_id, content, finalize=finalize,
|
||||
chat_id, message_id, content, finalize=finalize, metadata=metadata,
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -1902,7 +1903,7 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
self.name, utf16_len(content), self.MAX_MESSAGE_LENGTH,
|
||||
)
|
||||
return await self._edit_overflow_split(
|
||||
chat_id, message_id, content, finalize=finalize,
|
||||
chat_id, message_id, content, finalize=finalize, metadata=metadata,
|
||||
)
|
||||
# Flood control / RetryAfter — short waits are retried inline,
|
||||
# long waits return a failure immediately so streaming can fall back
|
||||
@@ -1973,6 +1974,7 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
content: str,
|
||||
*,
|
||||
finalize: bool,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
) -> SendResult:
|
||||
"""Split an oversized edit across the existing message + continuations.
|
||||
|
||||
@@ -2044,8 +2046,16 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
# fallback, mirroring send().
|
||||
continuation_ids: list[str] = []
|
||||
prev_id = message_id
|
||||
thread_id = self._metadata_thread_id(metadata)
|
||||
for chunk in chunks[1:]:
|
||||
sent_msg = None
|
||||
reply_to_id = int(prev_id) if prev_id else None
|
||||
thread_kwargs = self._thread_kwargs_for_send(
|
||||
chat_id,
|
||||
thread_id,
|
||||
metadata,
|
||||
reply_to_message_id=reply_to_id,
|
||||
)
|
||||
for use_markdown in (True, False) if finalize else (False,):
|
||||
try:
|
||||
text = self.format_message(chunk) if use_markdown else chunk
|
||||
@@ -2053,16 +2063,31 @@ class TelegramAdapter(BasePlatformAdapter):
|
||||
chat_id=int(chat_id),
|
||||
text=text,
|
||||
parse_mode=ParseMode.MARKDOWN_V2 if use_markdown else None,
|
||||
reply_to_message_id=int(prev_id) if prev_id else None,
|
||||
reply_to_message_id=reply_to_id,
|
||||
**thread_kwargs,
|
||||
**self._link_preview_kwargs(),
|
||||
**self._notification_kwargs(metadata),
|
||||
)
|
||||
break
|
||||
except Exception as send_err:
|
||||
if "reply message not found" in str(send_err).lower():
|
||||
# Drop the reply anchor and try again.
|
||||
# Drop the reply anchor and try again. Private DM
|
||||
# topic fallback needs the anchor and topic id together;
|
||||
# forum topics can still safely keep message_thread_id.
|
||||
retry_thread_kwargs = (
|
||||
{}
|
||||
if metadata and metadata.get("telegram_dm_topic_reply_fallback")
|
||||
else self._thread_kwargs_for_send(
|
||||
chat_id, thread_id, metadata, reply_to_message_id=None
|
||||
)
|
||||
)
|
||||
try:
|
||||
sent_msg = await self._bot.send_message(
|
||||
chat_id=int(chat_id),
|
||||
text=chunk,
|
||||
**retry_thread_kwargs,
|
||||
**self._link_preview_kwargs(),
|
||||
**self._notification_kwargs(metadata),
|
||||
)
|
||||
break
|
||||
except Exception as _retry_err:
|
||||
|
||||
Reference in New Issue
Block a user