feat: 添加 typing status 状态指示器

- 在处理消息时自动显示"正在输入..."状态
- 处理完成后自动隐藏状态指示器
- 错误处理时确保状态指示器被关闭
- 提升用户体验,让用户知道 AI 正在处理请求

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
wangliang
2026-01-26 13:15:59 +08:00
parent 9fe29ff3fe
commit 2dd46a8626

View File

@@ -311,6 +311,27 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token:
context["channel"] = message_channel context["channel"] = message_channel
context["is_email"] = is_email 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: try:
# Process message through agent workflow # Process message through agent workflow
final_state = await process_message( final_state = await process_message(
@@ -329,9 +350,8 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token:
if response is None: if response is None:
response = "抱歉,我暂时无法处理您的请求。请稍后重试或联系人工客服。" response = "抱歉,我暂时无法处理您的请求。请稍后重试或联系人工客服。"
# Create Chatwoot client # Create Chatwoot client(已在前面创建,这里不需要再次创建)
from integrations.chatwoot import ChatwootClient # chatwoot 已在 try 块之前创建
chatwoot = ChatwootClient(account_id=int(account_id))
# Send response to Chatwoot (skip if empty - agent may have already sent rich content) # Send response to Chatwoot (skip if empty - agent may have already sent rich content)
if response: if response:
@@ -340,6 +360,23 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token:
content=response 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 # Handle human handoff
if final_state.get("requires_human"): if final_state.get("requires_human"):
await chatwoot.update_conversation_status( await chatwoot.update_conversation_status(
@@ -377,8 +414,16 @@ async def handle_incoming_message(payload: ChatwootWebhookPayload, cookie_token:
error=str(e) error=str(e)
) )
# 关闭 typing status错误时也要关闭
try:
await chatwoot.toggle_typing_status(
conversation_id=conversation.id,
typing_status="off"
)
except Exception:
pass # 忽略关闭时的错误
# Send error response # Send error response
chatwoot = get_chatwoot_client(account_id=int(account_id))
await chatwoot.send_message( await chatwoot.send_message(
conversation_id=conversation.id, conversation_id=conversation.id,
content="抱歉,处理您的消息时遇到了问题。我们的客服团队将尽快为您服务。" content="抱歉,处理您的消息时遇到了问题。我们的客服团队将尽快为您服务。"