From 2dd46a86263f6499469565cd913a6a5237e290dd Mon Sep 17 00:00:00 2001 From: wangliang Date: Mon, 26 Jan 2026 13:15:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20typing=20status=20?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=8C=87=E7=A4=BA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在处理消息时自动显示"正在输入..."状态 - 处理完成后自动隐藏状态指示器 - 错误处理时确保状态指示器被关闭 - 提升用户体验,让用户知道 AI 正在处理请求 Co-Authored-By: Claude Sonnet 4.5 --- agent/webhooks/chatwoot_webhook.py | 55 +++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/agent/webhooks/chatwoot_webhook.py b/agent/webhooks/chatwoot_webhook.py index ed23d90..c9744c8 100644 --- a/agent/webhooks/chatwoot_webhook.py +++ b/agent/webhooks/chatwoot_webhook.py @@ -311,6 +311,27 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token: context["channel"] = message_channel context["is_email"] = is_email + # 创建 Chatwoot client(提前创建以便开启 typing status) + from integrations.chatwoot import ChatwootClient + chatwoot = ChatwootClient(account_id=int(account_id)) + + # 开启 typing status(显示"正在输入...") + try: + await chatwoot.toggle_typing_status( + conversation_id=conversation.id, + typing_status="on" + ) + logger.debug( + "Typing status enabled", + conversation_id=conversation_id + ) + except Exception as e: + logger.warning( + "Failed to enable typing status", + conversation_id=conversation_id, + error=str(e) + ) + try: # Process message through agent workflow final_state = await process_message( @@ -329,9 +350,8 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token: if response is None: response = "抱歉,我暂时无法处理您的请求。请稍后重试或联系人工客服。" - # Create Chatwoot client - from integrations.chatwoot import ChatwootClient - chatwoot = ChatwootClient(account_id=int(account_id)) + # Create Chatwoot client(已在前面创建,这里不需要再次创建) + # chatwoot 已在 try 块之前创建 # Send response to Chatwoot (skip if empty - agent may have already sent rich content) if response: @@ -340,6 +360,23 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token: content=response ) + # 关闭 typing status(隐藏"正在输入...") + try: + await chatwoot.toggle_typing_status( + conversation_id=conversation.id, + typing_status="off" + ) + logger.debug( + "Typing status disabled", + conversation_id=conversation_id + ) + except Exception as e: + logger.warning( + "Failed to disable typing status", + conversation_id=conversation_id, + error=str(e) + ) + # Handle human handoff if final_state.get("requires_human"): await chatwoot.update_conversation_status( @@ -376,9 +413,17 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token: conversation_id=conversation_id, error=str(e) ) - + + # 关闭 typing status(错误时也要关闭) + try: + await chatwoot.toggle_typing_status( + conversation_id=conversation.id, + typing_status="off" + ) + except Exception: + pass # 忽略关闭时的错误 + # Send error response - chatwoot = get_chatwoot_client(account_id=int(account_id)) await chatwoot.send_message( conversation_id=conversation.id, content="抱歉,处理您的消息时遇到了问题。我们的客服团队将尽快为您服务。"