fix(cron): route Telegram cron deliveries to a dedicated topic via TELEGRAM_CRON_THREAD_ID

When Telegram topic mode is enabled, cron messages delivered to the bot's
root DM (TELEGRAM_HOME_CHANNEL without a thread id) land in the system
lobby — replies there are rebuffed with the lobby reminder and
reply_to_message_id is dropped, so users cannot interact with the cron
output (#24409).

Add an optional TELEGRAM_CRON_THREAD_ID env var that overrides
TELEGRAM_HOME_CHANNEL_THREAD_ID for cron deliveries only. Operators can
create a "Cron" forum topic in the DM, point this var at its thread id,
and replies to cron messages will land in that topic's existing session
instead of the lobby. The home-channel thread id (used elsewhere, e.g.
restart notifications) is unchanged, and explicit
deliver="telegram:chat:thread" targets continue to win over the env var.

Per the reporter's clarification on 2026-05-13, option (a) (cron-side
route to a dedicated topic + config knob) was chosen.

Fixes #24409
This commit is contained in:
konsisumer
2026-05-13 06:13:35 +02:00
committed by Teknium
parent 032d4cafc4
commit a4fb0a3ac3
7 changed files with 85 additions and 1 deletions

View File

@ -292,10 +292,23 @@ def _get_home_target_chat_id(platform_name: str) -> str:
def _get_home_target_thread_id(platform_name: str) -> Optional[str]:
"""Return the optional thread/topic ID for a platform home target."""
"""Return the optional thread/topic ID for a platform home target.
Telegram-only override: ``TELEGRAM_CRON_THREAD_ID`` takes precedence over
``TELEGRAM_HOME_CHANNEL_THREAD_ID`` for cron delivery. When topic mode is
enabled, deliveries that land in the root DM (thread_id unset) end up in
the system-only lobby where the user cannot reply — the gateway returns
the lobby reminder and drops ``reply_to_message_id`` (#24409). Pointing
cron at a dedicated topic via this env var lets replies work as expected
without changing the lobby invariant.
"""
env_var = _resolve_home_env_var(platform_name)
if not env_var:
return None
if platform_name.lower() == "telegram":
cron_thread = os.getenv("TELEGRAM_CRON_THREAD_ID", "").strip()
if cron_thread:
return cron_thread
value = os.getenv(f"{env_var}_THREAD_ID", "").strip()
if not value:
legacy = _LEGACY_HOME_TARGET_ENV_VARS.get(env_var)