mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-06 01:18:45 +00:00
feat(telegram): change to mid-stream split per review feedback(#2967 PR)
This commit is contained in:
parent
197ecb02ca
commit
1826bfd05a
@ -626,19 +626,59 @@ class TelegramChannel(BaseChannel):
|
|||||||
logger.warning("Stream initial send failed: {}", e)
|
logger.warning("Stream initial send failed: {}", e)
|
||||||
raise # Let ChannelManager handle retry
|
raise # Let ChannelManager handle retry
|
||||||
elif (now - buf.last_edit) >= self.config.stream_edit_interval:
|
elif (now - buf.last_edit) >= self.config.stream_edit_interval:
|
||||||
try:
|
if len(buf.text) > TELEGRAM_MAX_MESSAGE_LEN:
|
||||||
await self._call_with_retry(
|
# Finish current message
|
||||||
self._app.bot.edit_message_text,
|
current_text = buf.text[:TELEGRAM_MAX_MESSAGE_LEN]
|
||||||
chat_id=int_chat_id, message_id=buf.message_id,
|
try:
|
||||||
text=buf.text,
|
await self._call_with_retry(
|
||||||
)
|
self._app.bot.edit_message_text,
|
||||||
buf.last_edit = now
|
chat_id=int_chat_id,
|
||||||
except Exception as e:
|
message_id=buf.message_id,
|
||||||
if self._is_not_modified_error(e):
|
text=current_text,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("Failed to edit current message before splitting: {}", e)
|
||||||
|
raise # Let ChannelManager handle retry
|
||||||
|
|
||||||
|
# Prepare remaining content for a new message
|
||||||
|
remaining = buf.text[TELEGRAM_MAX_MESSAGE_LEN:]
|
||||||
|
logger.debug(f"[!] Splitting long message: {len(buf.text)} chars → new message with {len(remaining)} chars")
|
||||||
|
|
||||||
|
# Create new buffer for the next chunk
|
||||||
|
self._stream_bufs[chat_id] = _StreamBuf(stream_id=stream_id)
|
||||||
|
new_buf = self._stream_bufs[chat_id]
|
||||||
|
new_buf.text = remaining
|
||||||
|
new_buf.last_edit = now
|
||||||
|
|
||||||
|
# Immediately start the new message
|
||||||
|
if remaining.strip():
|
||||||
|
try:
|
||||||
|
sent = await self._call_with_retry(
|
||||||
|
self._app.bot.send_message,
|
||||||
|
chat_id=int_chat_id,
|
||||||
|
text=remaining[:TELEGRAM_MAX_MESSAGE_LEN],
|
||||||
|
**thread_kwargs
|
||||||
|
)
|
||||||
|
new_buf.message_id = sent.message_id
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed to send new message chunk after split: {}", e)
|
||||||
|
raise # Let ChannelManager handle retry
|
||||||
|
else:
|
||||||
|
# Normal edit (message is still under the limit)
|
||||||
|
try:
|
||||||
|
await self._call_with_retry(
|
||||||
|
self._app.bot.edit_message_text,
|
||||||
|
chat_id=int_chat_id,
|
||||||
|
message_id=buf.message_id,
|
||||||
|
text=buf.text,
|
||||||
|
)
|
||||||
buf.last_edit = now
|
buf.last_edit = now
|
||||||
return
|
except Exception as e:
|
||||||
logger.warning("Stream edit failed: {}", e)
|
if self._is_not_modified_error(e):
|
||||||
raise # Let ChannelManager handle retry
|
buf.last_edit = now
|
||||||
|
return
|
||||||
|
logger.warning("Stream edit failed: {}", e)
|
||||||
|
raise # Let ChannelManager handle retry
|
||||||
|
|
||||||
async def _on_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def _on_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
"""Handle /start command."""
|
"""Handle /start command."""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user