Skip to content

Commit 3eb1508

Browse files
authored
fix(telegram): avoid duplicate message_thread_id in streaming (#5430)
1 parent c5b23d1 commit 3eb1508

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

astrbot/core/platform/sources/telegram/tg_event.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Plain,
1919
Record,
2020
Reply,
21+
Video,
2122
)
2223
from astrbot.api.platform import AstrBotMessage, MessageType, PlatformMetadata
2324

@@ -36,6 +37,7 @@ class TelegramPlatformEvent(AstrMessageEvent):
3637
# 消息类型到 chat action 的映射,用于优先级判断
3738
ACTION_BY_TYPE: dict[type, str] = {
3839
Record: ChatAction.UPLOAD_VOICE,
40+
Video: ChatAction.UPLOAD_VIDEO,
3941
File: ChatAction.UPLOAD_DOCUMENT,
4042
Image: ChatAction.UPLOAD_PHOTO,
4143
Plain: ChatAction.TYPING,
@@ -114,10 +116,18 @@ async def _send_media_with_action(
114116
**payload: Any,
115117
) -> None:
116118
"""发送媒体时显示 upload action,发送完成后恢复 typing"""
117-
await cls._send_chat_action(client, user_name, upload_action, message_thread_id)
118-
await send_coro(**payload)
119+
effective_thread_id = message_thread_id or cast(
120+
str | None, payload.get("message_thread_id")
121+
)
122+
await cls._send_chat_action(
123+
client, user_name, upload_action, effective_thread_id
124+
)
125+
send_payload = dict(payload)
126+
if effective_thread_id and "message_thread_id" not in send_payload:
127+
send_payload["message_thread_id"] = effective_thread_id
128+
await send_coro(**send_payload)
119129
await cls._send_chat_action(
120-
client, user_name, ChatAction.TYPING, message_thread_id
130+
client, user_name, ChatAction.TYPING, effective_thread_id
121131
)
122132

123133
@classmethod
@@ -141,14 +151,16 @@ async def _send_voice_with_fallback(
141151
"""
142152
try:
143153
if use_media_action:
154+
media_payload = dict(payload)
155+
if message_thread_id and "message_thread_id" not in media_payload:
156+
media_payload["message_thread_id"] = message_thread_id
144157
await cls._send_media_with_action(
145158
client,
146159
ChatAction.UPLOAD_VOICE,
147160
client.send_voice,
148161
user_name=user_name,
149-
message_thread_id=message_thread_id,
150162
voice=path,
151-
**cast(Any, payload),
163+
**cast(Any, media_payload),
152164
)
153165
else:
154166
await client.send_voice(voice=path, **cast(Any, payload))
@@ -162,15 +174,17 @@ async def _send_voice_with_fallback(
162174
"To enable voice messages, go to Telegram Settings → Privacy and Security → Voice Messages → set to 'Everyone'."
163175
)
164176
if use_media_action:
177+
media_payload = dict(payload)
178+
if message_thread_id and "message_thread_id" not in media_payload:
179+
media_payload["message_thread_id"] = message_thread_id
165180
await cls._send_media_with_action(
166181
client,
167182
ChatAction.UPLOAD_DOCUMENT,
168183
client.send_document,
169184
user_name=user_name,
170-
message_thread_id=message_thread_id,
171185
document=path,
172186
caption=caption,
173-
**cast(Any, payload),
187+
**cast(Any, media_payload),
174188
)
175189
else:
176190
await client.send_document(
@@ -278,6 +292,13 @@ async def send_with_client(
278292
caption=i.text or None,
279293
use_media_action=False,
280294
)
295+
elif isinstance(i, Video):
296+
path = await i.convert_to_file_path()
297+
await client.send_video(
298+
video=path,
299+
caption=getattr(i, "text", None) or None,
300+
**cast(Any, payload),
301+
)
281302

282303
async def send(self, message: MessageChain) -> None:
283304
if self.get_message_type() == MessageType.GROUP_MESSAGE:
@@ -333,7 +354,7 @@ async def send_streaming(self, generator, use_fallback: bool = False):
333354
"chat_id": user_name,
334355
}
335356
if message_thread_id:
336-
payload["reply_to_message_id"] = message_thread_id
357+
payload["message_thread_id"] = message_thread_id
337358

338359
delta = ""
339360
current_content = ""
@@ -375,7 +396,6 @@ async def send_streaming(self, generator, use_fallback: bool = False):
375396
ChatAction.UPLOAD_PHOTO,
376397
self.client.send_photo,
377398
user_name=user_name,
378-
message_thread_id=message_thread_id,
379399
photo=image_path,
380400
**cast(Any, payload),
381401
)
@@ -388,7 +408,6 @@ async def send_streaming(self, generator, use_fallback: bool = False):
388408
ChatAction.UPLOAD_DOCUMENT,
389409
self.client.send_document,
390410
user_name=user_name,
391-
message_thread_id=message_thread_id,
392411
document=path,
393412
filename=name,
394413
**cast(Any, payload),
@@ -406,6 +425,17 @@ async def send_streaming(self, generator, use_fallback: bool = False):
406425
use_media_action=True,
407426
)
408427
continue
428+
elif isinstance(i, Video):
429+
path = await i.convert_to_file_path()
430+
await self._send_media_with_action(
431+
self.client,
432+
ChatAction.UPLOAD_VIDEO,
433+
self.client.send_video,
434+
user_name=user_name,
435+
video=path,
436+
**cast(Any, payload),
437+
)
438+
continue
409439
else:
410440
logger.warning(f"不支持的消息类型: {type(i)}")
411441
continue

0 commit comments

Comments
 (0)