fix: 统一消息 typing 生命周期

This commit is contained in:
jxxghp
2026-05-22 22:59:20 +08:00
parent 7e6cd47712
commit f7b78721c3
5 changed files with 242 additions and 116 deletions

View File

@@ -15,7 +15,6 @@ from app.schemas import (
CommandRegisterEventData,
NotificationConf,
MessageResponse,
NotificationType,
)
from app.schemas.types import ModuleType, ChainEventType
from app.utils.structures import DictUtils
@@ -452,7 +451,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
return
client: Telegram = self.get_instance(conf.name)
if client:
stop_typing = message.mtype != NotificationType.Agent
if message.file_path:
client.send_file(
file_path=message.file_path,
@@ -461,7 +459,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
text=message.text,
userid=userid,
original_chat_id=message.original_chat_id,
stop_typing=stop_typing,
)
elif message.voice_path:
client.send_voice(
@@ -469,7 +466,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
userid=userid,
caption=message.voice_caption,
original_chat_id=message.original_chat_id,
stop_typing=stop_typing,
)
else:
client.send_msg(
@@ -482,7 +478,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
original_message_id=message.original_message_id,
original_chat_id=message.original_chat_id,
disable_web_page_preview=message.disable_web_page_preview,
stop_typing=stop_typing,
)
def post_medias_message(
@@ -507,7 +502,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
buttons=message.buttons,
original_message_id=message.original_message_id,
original_chat_id=message.original_chat_id,
stop_typing=message.mtype != NotificationType.Agent,
)
def post_torrents_message(
@@ -532,7 +526,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
buttons=message.buttons,
original_message_id=message.original_message_id,
original_chat_id=message.original_chat_id,
stop_typing=message.mtype != NotificationType.Agent,
)
def delete_message(
@@ -616,11 +609,18 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
) -> Optional[dict]:
"""
标记 Telegram 消息正在处理。
入站侧已经启动 typing 任务,这里只返回可用于统一收口的上下文
Telegram typing 需要周期性续发,因此在模块接口中启动保活任务
"""
if channel != self._channel:
return None
if not text:
client_config = self.get_config(source)
if not client_config:
return None
client: Telegram = self.get_instance(client_config.name)
if not client:
return None
started = client.start_typing(chat_id=chat_id, userid=userid)
if not started:
return None
return {
"channel": channel.value,
@@ -674,14 +674,12 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
return None
client: Telegram = self.get_instance(conf.name)
if client:
agent_managed_typing = message.mtype == NotificationType.Agent
if message.voice_path:
result = client.send_voice(
voice_path=message.voice_path,
userid=userid,
caption=message.voice_caption,
original_chat_id=message.original_chat_id,
stop_typing=not agent_managed_typing,
)
else:
result = client.send_msg(
@@ -691,7 +689,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
userid=userid,
link=message.link,
disable_web_page_preview=message.disable_web_page_preview,
stop_typing=not agent_managed_typing,
)
if result and result.get("success"):
return MessageResponse(
@@ -699,9 +696,6 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
chat_id=result.get("chat_id"),
channel=MessageChannel.Telegram,
source=conf.name,
metadata={"agent_managed_typing": True}
if agent_managed_typing
else None,
success=True,
)
return None

View File

@@ -118,30 +118,15 @@ class Telegram:
# Check if we should process this message
if self._should_process_message(message):
# 启动持续发送正在输入状态
message_text = message.text or message.caption or ""
max_duration = (
self._typing_command_max_duration_seconds
if (
message_text.startswith("/")
and not message_text.lower().startswith("/ai")
)
else None
)
self._start_typing_task(
message.chat.id, max_duration_seconds=max_duration
)
payload = self._serialize_update_payload(message)
if not payload:
logger.warn("Telegram消息序列化失败跳过转发")
self._stop_typing_task(message.chat.id)
return
response = RequestUtils(timeout=15).post_res(
self._ds_url, json=payload
)
if not response or response.status_code >= 400:
logger.warn("Telegram消息转发失败停止typing状态")
self._stop_typing_task(message.chat.id)
logger.warn("Telegram消息转发失败")
@_bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
@@ -149,7 +134,6 @@ class Telegram:
处理按钮点击回调
"""
chat_id = None
typing_started = False
try:
# Update user-chat mapping for callbacks too
chat_id = call.message.chat.id
@@ -181,25 +165,15 @@ class Telegram:
# 先确认回调避免用户看到loading状态
_bot.answer_callback_query(call.id)
# 启动持续发送正在输入状态
self._start_typing_task(
chat_id,
max_duration_seconds=self._typing_callback_max_duration_seconds,
)
typing_started = True
# 发送给主程序处理
response = RequestUtils(timeout=15).post_res(
self._ds_url, json=callback_json
)
if not response or response.status_code >= 400:
logger.warn("Telegram按钮回调转发失败停止typing状态")
self._stop_typing_task(chat_id)
logger.warn("Telegram按钮回调转发失败")
except Exception as err:
logger.error(f"处理按钮回调失败:{str(err)}")
if typing_started and chat_id is not None:
self._stop_typing_task(chat_id)
_bot.answer_callback_query(call.id, "处理失败,请重试")
def run_polling():
@@ -427,11 +401,31 @@ class Telegram:
) -> None:
"""
按调用方要求停止 typing。
Agent 回复和流式编辑由 worker 统一收口,避免中途发送消息时误关后续排队消息的状态
typing 由消息处理状态统一收口,兼容显式要求立即停止的调用
"""
if stop_typing:
self._stop_typing_task(chat_id)
def start_typing(
self,
chat_id: Optional[Union[str, int]] = None,
userid: Optional[Union[str, int]] = None,
) -> bool:
"""
外部链路主动启动 typing 状态。
"""
if chat_id:
target_chat_id = chat_id
elif userid:
target_chat_id = self._get_user_chat_id(str(userid)) or str(userid)
else:
target_chat_id = None
target_chat_id = target_chat_id or (str(userid) if userid else None)
if not target_chat_id:
return False
self._start_typing_task(target_chat_id)
return True
def stop_typing(
self,
chat_id: Optional[Union[str, int]] = None,
@@ -463,7 +457,7 @@ class Telegram:
original_message_id: Optional[int] = None,
original_chat_id: Optional[str] = None,
disable_web_page_preview: Optional[bool] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[dict]:
"""
发送Telegram消息
@@ -559,7 +553,7 @@ class Telegram:
userid: Optional[str] = None,
caption: Optional[str] = None,
original_chat_id: Optional[str] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[dict]:
"""
发送Telegram语音消息。
@@ -608,7 +602,7 @@ class Telegram:
text: Optional[str] = None,
file_name: Optional[str] = None,
original_chat_id: Optional[str] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[dict]:
"""
发送本地图片或文件给 Telegram 用户。
@@ -699,7 +693,7 @@ class Telegram:
buttons: Optional[List[List[Dict]]] = None,
original_message_id: Optional[int] = None,
original_chat_id: Optional[str] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[bool]:
"""
发送媒体列表消息
@@ -779,7 +773,7 @@ class Telegram:
buttons: Optional[List[List[Dict]]] = None,
original_message_id: Optional[int] = None,
original_chat_id: Optional[str] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[bool]:
"""
发送种子列表消息
@@ -937,7 +931,7 @@ class Telegram:
text: str,
title: Optional[str] = None,
buttons: Optional[List[List[dict]]] = None,
stop_typing: bool = True,
stop_typing: bool = False,
) -> Optional[bool]:
"""
编辑Telegram消息公开方法