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="抱歉,处理您的消息时遇到了问题。我们的客服团队将尽快为您服务。"