fix(telegram): support threads in DMs

This commit is contained in:
Ilya Semenov 2026-04-04 19:08:27 +07:00 committed by Xubin Ren
parent 4b4d8b506d
commit 0d6bc7fc11
2 changed files with 24 additions and 3 deletions

View File

@ -599,11 +599,15 @@ class TelegramChannel(BaseChannel):
return
now = time.monotonic()
thread_kwargs = {}
if message_thread_id := meta.get("message_thread_id"):
thread_kwargs["message_thread_id"] = message_thread_id
if buf.message_id is None:
try:
sent = await self._call_with_retry(
self._app.bot.send_message,
chat_id=int_chat_id, text=buf.text,
**thread_kwargs,
)
buf.message_id = sent.message_id
buf.last_edit = now
@ -651,9 +655,9 @@ class TelegramChannel(BaseChannel):
@staticmethod
def _derive_topic_session_key(message) -> str | None:
"""Derive topic-scoped session key for non-private Telegram chats."""
"""Derive topic-scoped session key for Telegram chats with threads."""
message_thread_id = getattr(message, "message_thread_id", None)
if message.chat.type == "private" or message_thread_id is None:
if message_thread_id is None:
return None
return f"telegram:{message.chat_id}:topic:{message_thread_id}"
@ -815,7 +819,7 @@ class TelegramChannel(BaseChannel):
return bool(bot_id and reply_user and reply_user.id == bot_id)
def _remember_thread_context(self, message) -> None:
"""Cache topic thread id by chat/message id for follow-up replies."""
"""Cache Telegram thread context by chat/message id for follow-up replies."""
message_thread_id = getattr(message, "message_thread_id", None)
if message_thread_id is None:
return

View File

@ -424,6 +424,23 @@ async def test_send_delta_incremental_edit_treats_not_modified_as_success() -> N
assert channel._stream_bufs["123"].last_edit > 0.0
@pytest.mark.asyncio
async def test_send_delta_initial_send_keeps_message_in_thread() -> None:
channel = TelegramChannel(
TelegramConfig(enabled=True, token="123:abc", allow_from=["*"]),
MessageBus(),
)
channel._app = _FakeApp(lambda: None)
await channel.send_delta(
"123",
"hello",
{"_stream_delta": True, "_stream_id": "s:0", "message_thread_id": 42},
)
assert channel._app.bot.sent_messages[0]["message_thread_id"] == 42
def test_derive_topic_session_key_uses_thread_id() -> None:
message = SimpleNamespace(
chat=SimpleNamespace(type="supergroup"),