Skip to content

The agent that grows with you

refactor(telegram): share the cold-boot queue decision and log it

kshitijk4poor6d8badcc6e5e
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 20723befead..16b96222526 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -2959,6 +2959,21 @@ class TelegramAdapter(BasePlatformAdapter):
with contextlib.suppress(Exception):
await _shutdown_abandoned_app(old_app)
+ def _cold_boot_drop_pending(self, *, is_reconnect: bool) -> bool:
+ """Whether THIS connection asks Telegram to discard its queued updates.
+
+ A watcher reconnect always preserves them (#46621); a cold boot follows
+ ``platforms.telegram.extra.drop_pending_on_cold_boot`` (default true). The decision is logged
+ on every cold boot — a command that never ran is otherwise invisible (#71811)."""
+ drop_pending = self._drop_pending_on_cold_boot if not is_reconnect else False
+ if not is_reconnect:
+ logger.info(
+ "[%s] Cold boot: %s Telegram updates queued while offline "
+ "(platforms.telegram.extra.drop_pending_on_cold_boot: %s)",
+ self.name, "dropping" if drop_pending else "preserving",
+ "true" if self._drop_pending_on_cold_boot else "false")
+ return drop_pending
+
async def _start_webhook_mode(self, webhook_url: str, *, is_reconnect: bool) -> None:
"""Start PTB's webhook server (Telegram pushes updates; lets cloud platforms auto-wake suspended
machines). SECURITY: TELEGRAM_WEBHOOK_SECRET is REQUIRED — without it the endpoint accepts forged
@@ -2979,7 +2994,7 @@ class TelegramAdapter(BasePlatformAdapter):
await self._app.updater.start_webhook(
listen=webhook_host, port=webhook_port, url_path=webhook_path, webhook_url=webhook_url,
secret_token=webhook_secret, allowed_updates=Update.ALL_TYPES,
- drop_pending_updates=self._drop_pending_on_cold_boot if not is_reconnect else False,
+ drop_pending_updates=self._cold_boot_drop_pending(is_reconnect=is_reconnect),
)
self._webhook_mode = True
self._polling_progress_accepting = False
@@ -3010,9 +3025,7 @@ class TelegramAdapter(BasePlatformAdapter):
logger.error("[%s] Telegram polling error: %s", self.name, _redact_telegram_error_text(error), exc_info=True)
self._polling_error_callback_ref = _polling_error_callback # reused by _handle_polling_conflict
- # Cold first boot drops the Bot API queue unless extra.drop_pending_on_cold_boot
- # is false (nightly-off hosts want the backlog); a watcher reconnect preserves it.
- drop_pending = self._drop_pending_on_cold_boot if not is_reconnect else False
+ drop_pending = self._cold_boot_drop_pending(is_reconnect=is_reconnect)
polling_started = await self._start_polling_resilient(
drop_pending_updates=drop_pending, error_callback=_polling_error_callback, require_progress=not is_reconnect)
if not polling_started: