#151057 put a "native id" step in the private-topic retry ladders: after "message thread not found" the same topic was retried as direct_messages_topic_id. Bot API honours that field only in a channel's DM supergroup (Client::check_message_topic builds a direct-messages topic only for a supergroup with is_direct_messages; #55265 saw ok:true and the message in General). In a bot's private chat the native-id send lands in the root and returns success, so the ladder read it as "delivered in the topic": the dead binding was never pruned (topic mode never turned off, #95517), cron lost its thread_fallback note, and every later send paid the full ladder again. All three ladders (text send, media, control messages) now go from the same-thread retry straight to the chat root without the dead anchor, prune the binding and (text) report thread_fallback, as before #151057. direct_topic_retry_kwargs is gone. A channel DM supergroup still routes by direct_messages_topic_id first (_thread_kwargs_for_send, unchanged). The thread-fallback tests that pinned the native-id step now pin the root route; the three that only exercised failures of that step are removed. Fixes #151852. Found by @kshitijk4poor's post-merge review.
44 lines
2.4 KiB
Python
44 lines
2.4 KiB
Python
"""Telegram private-chat topic send ladder: what to do after Bot API answers "message thread not found".
|
|
|
|
Bot private-chat topics are addressed by ``message_thread_id`` (Bot API: "for forum supergroups and private
|
|
chats of bots with forum topic mode enabled"). When Telegram rejects that id twice the topic is gone: the user
|
|
deleted it, or forum mode was turned off (#95517). It is never retried as ``direct_messages_topic_id``: Bot API
|
|
honours that only in a channel's DM supergroup and sends a private chat's message to the root without saying so,
|
|
so the dead binding would never be pruned (#151852). Every retry removes exactly the constraint that failed
|
|
(#136808): the dead route goes, and so does the reply anchor, which points into the dead topic and would
|
|
otherwise steer the retry back there. Pruning the dead topic's binding (the chat's last one disables topic
|
|
mode, ``hermes_state_telegram.delete_telegram_topic_binding``) stops inbound recovery steering into it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def private_chat(chat_id: Any) -> bool:
|
|
try:
|
|
return int(str(chat_id).strip()) > 0
|
|
except (TypeError, ValueError):
|
|
return False
|
|
|
|
|
|
def topic_gone(chat_id: Any, error: Exception) -> bool:
|
|
"""Telegram's wordings for a private topic that no longer takes messages (origin/main's media retry matched the
|
|
same set); elsewhere only "thread not found". A parse, permission or flood error says nothing about the topic."""
|
|
markers = ("thread not found", "topic_deleted", "topic not found", "direct_messages_topic") if private_chat(chat_id) else ("thread not found",)
|
|
return any(m in str(error).lower() for m in markers)
|
|
|
|
|
|
def dead_topic_may_reroute(chat_id: Any, metadata: Any) -> bool:
|
|
"""A dead private topic may be rerouted to the chat root, unless the send targets a named topic
|
|
(``telegram_dm_topic_created_for_send``): that fails closed for text and media alike so DeliveryRouter recreates it."""
|
|
return private_chat(chat_id) and not (metadata or {}).get("telegram_dm_topic_created_for_send")
|
|
|
|
|
|
def dead_topic_retry_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
"""A control send's kwargs without the topic route, and without the anchor in a private chat."""
|
|
retry = {k: v for k, v in kwargs.items() if k not in ("message_thread_id", "direct_messages_topic_id")}
|
|
if private_chat(kwargs.get("chat_id")):
|
|
retry.pop("reply_to_message_id", None)
|
|
return retry
|