@@ -2272,13 +2272,14 @@ def _recover_telegram_topic_thread_id(
22722272 ) -> Optional[str]:
22732273 """Pin DM-topic routing to the user's last-active topic.
22742274
2275- Telegram fragments topic-mode DMs two ways: a Reply on a message
2276- in another topic delivers ``message_thread_id`` for *that* topic,
2277- and ``_build_message_event`` strips the thread_id on plain replies
2278- (#3206 — needed for non-topic users). Both route the user to the
2279- wrong session. When topic mode is on, rewrite the thread_id to the
2280- user's most-recent binding if the inbound id is missing/General or
2281- not a known topic for this chat. Returns None to leave it alone.
2275+ Telegram can omit ``message_thread_id`` or surface General (``1``)
2276+ for some topic-mode DM replies. In those lobby-shaped cases, keep the
2277+ conversation attached to the user's most-recent bound topic.
2278+
2279+ Do not rewrite a non-lobby, previously-unbound thread id: a newly
2280+ created Telegram DM topic is also "unknown" until the first inbound
2281+ message is recorded, and rewriting it would send that brand-new topic's
2282+ answer into an older lane. Returns None to leave the source alone.
22822283 """
22832284 if (
22842285 source.platform != Platform.TELEGRAM
@@ -2288,6 +2289,14 @@ def _recover_telegram_topic_thread_id(
22882289 or not self._telegram_topic_mode_enabled(source)
22892290 ):
22902291 return None
2292+ inbound = str(source.thread_id or "")
2293+ is_lobby = not inbound or inbound in self._TELEGRAM_GENERAL_TOPIC_IDS
2294+ if not is_lobby:
2295+ # A non-lobby, unknown thread_id is most likely the first message in
2296+ # a brand-new Telegram DM topic. Preserve it so it can be recorded
2297+ # as a new independent lane below instead of hijacking the latest
2298+ # existing topic binding.
2299+ return None
22912300 session_db = getattr(self, "_session_db", None)
22922301 if session_db is None:
22932302 return None
@@ -2300,11 +2309,6 @@ def _recover_telegram_topic_thread_id(
23002309 return None
23012310 if not bindings:
23022311 return None
2303- inbound = str(source.thread_id or "")
2304- is_lobby = not inbound or inbound in self._TELEGRAM_GENERAL_TOPIC_IDS
2305- known = {str(b.get("thread_id") or "") for b in bindings}
2306- if not is_lobby and inbound in known:
2307- return None
23082312 user_id = str(source.user_id)
23092313 for b in bindings: # newest-first
23102314 if str(b.get("user_id") or "") == user_id:
0 commit comments