feat: 修复订单查询和物流查询功能
主要修改: 1. 订单数据解析修复 (agent/agents/order.py) - 修复 Mall API 返回数据的嵌套结构解析 - 更新字段映射:orderId→order_id, orderProduct→items, statusText→status_text - 支持多种商品图片字段:image, pic, thumb, productImg - 添加详细的调试日志 2. 物流查询修复 (mcp_servers/order_mcp/server.py) - 修复物流接口返回数据结构解析 (data[].trackingCode→tracking_number) - 添加 print() 日志用于调试 - 支持多种字段名映射 3. Chatwoot 集成优化 (agent/integrations/chatwoot.py) - 添加 json 模块导入 - 完善订单卡片和表单展示功能 4. API 请求头优化 (mcp_servers/shared/mall_client.py) - 更新 User-Agent 和 Accept 头 - 修正 Origin 和 Referer 大小写 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Chatwoot API Client for B2B Shopping AI Assistant
|
||||
"""
|
||||
import json
|
||||
from typing import Any, Optional
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
@@ -172,9 +173,122 @@ class ChatwootClient:
|
||||
self,
|
||||
conversation_id: int,
|
||||
order_data: dict[str, Any],
|
||||
actions: list[dict[str, Any]]
|
||||
actions: Optional[list[dict[str, Any]]] = None
|
||||
) -> dict[str, Any]:
|
||||
"""发送订单卡片消息(Markdown 文本 + 操作按钮)
|
||||
"""发送订单卡片消息(使用 Chatwoot cards 格式)
|
||||
|
||||
卡片结构:
|
||||
- 图片:第一件商品的图片
|
||||
- 标题:订单号 + 状态
|
||||
- 描述:汇总信息
|
||||
- 按钮:跳转到订单详情页面
|
||||
|
||||
Args:
|
||||
conversation_id: 会话 ID
|
||||
order_data: 订单数据,包含:
|
||||
- order_id: 订单号
|
||||
- status: 订单状态
|
||||
- status_text: 状态文本
|
||||
- items: 商品列表
|
||||
- total_amount: 总金额
|
||||
actions: 操作按钮配置列表(可选)
|
||||
|
||||
Returns:
|
||||
发送结果
|
||||
|
||||
Example:
|
||||
>>> order_data = {
|
||||
... "order_id": "202071324",
|
||||
... "status_text": "已发货",
|
||||
... "items": [{"name": "商品A", "quantity": 2}],
|
||||
... "total_amount": "599.00"
|
||||
... }
|
||||
>>> await chatwoot.send_order_card(123, order_data)
|
||||
"""
|
||||
order_id = order_data.get("order_id", "")
|
||||
status_text = order_data.get("status_text", order_data.get("status", ""))
|
||||
|
||||
# 获取第一件商品的图片
|
||||
items = order_data.get("items", [])
|
||||
media_url = None
|
||||
if items and len(items) > 0:
|
||||
first_item = items[0]
|
||||
media_url = first_item.get("image_url")
|
||||
|
||||
# 构建标题
|
||||
title = f"订单 #{order_id} {status_text}"
|
||||
|
||||
# 构建描述
|
||||
if items and len(items) > 0:
|
||||
if len(items) == 1:
|
||||
items_desc = items[0].get("name", "商品")
|
||||
else:
|
||||
items_desc = f"{items[0].get('name', '商品A')} 等共计 {len(items)} 件商品"
|
||||
description = f"包含 {items_desc},实付 ¥{order_data.get('total_amount', '0.00')}"
|
||||
else:
|
||||
description = f"实付 ¥{order_data.get('total_amount', '0.00')}"
|
||||
|
||||
# 构建操作按钮
|
||||
card_actions = []
|
||||
if actions:
|
||||
card_actions = actions
|
||||
else:
|
||||
# 默认按钮:跳转到订单详情页面
|
||||
card_actions = [
|
||||
{
|
||||
"type": "link",
|
||||
"text": "查看订单详情",
|
||||
"uri": f"https://www.qa1.gaia888.com/customer/order/detail?orderId={order_id}"
|
||||
}
|
||||
]
|
||||
|
||||
# 构建单个卡片
|
||||
card = {
|
||||
"title": title,
|
||||
"description": description,
|
||||
"actions": card_actions
|
||||
}
|
||||
|
||||
# 如果有图片,添加 media_url
|
||||
if media_url:
|
||||
card["media_url"] = media_url
|
||||
|
||||
# 构建 content_attributes
|
||||
content_attributes = {
|
||||
"items": [card]
|
||||
}
|
||||
|
||||
# 记录发送的数据(用于调试)
|
||||
logger.info(
|
||||
"Sending order card",
|
||||
conversation_id=conversation_id,
|
||||
order_id=order_id,
|
||||
has_media=bool(media_url),
|
||||
payload_preview=json.dumps({
|
||||
"content": "订单详情",
|
||||
"content_type": "cards",
|
||||
"content_attributes": content_attributes
|
||||
}, ensure_ascii=False, indent=2)[:1000]
|
||||
)
|
||||
|
||||
# 发送富媒体消息
|
||||
return await self.send_rich_message(
|
||||
conversation_id=conversation_id,
|
||||
content="订单详情",
|
||||
content_type="cards",
|
||||
content_attributes=content_attributes
|
||||
)
|
||||
|
||||
async def send_order_form(
|
||||
self,
|
||||
conversation_id: int,
|
||||
order_data: dict[str, Any],
|
||||
actions: Optional[list[dict[str, Any]]] = None
|
||||
) -> dict[str, Any]:
|
||||
"""发送订单详情表单消息(使用 content_type=form)
|
||||
|
||||
根据 Chatwoot API 文档实现的 form 格式订单详情展示。
|
||||
form 类型支持的字段类型:text, text_area, email, select
|
||||
|
||||
Args:
|
||||
conversation_id: 会话 ID
|
||||
@@ -188,11 +302,9 @@ class ChatwootClient:
|
||||
- shipping_fee: 运费(可选)
|
||||
- logistics: 物流信息(可选)
|
||||
- remark: 备注(可选)
|
||||
actions: 操作按钮配置列表,每个按钮包含:
|
||||
- type: "link" 或 "postback"
|
||||
- text: 按钮文字
|
||||
- uri: 链接地址(type=link 时必需)
|
||||
- payload: 回传数据(type=postback 时必需)
|
||||
actions: 操作按钮配置列表(可选),每个按钮包含:
|
||||
- label: 按钮文字(用于 select 选项的显示)
|
||||
- value: 按钮值(用于 select 选项的值)
|
||||
|
||||
Returns:
|
||||
发送结果
|
||||
@@ -202,27 +314,127 @@ class ChatwootClient:
|
||||
... "order_id": "123456789",
|
||||
... "status": "shipped",
|
||||
... "status_text": "已发货",
|
||||
... "created_at": "2023-10-27 14:30",
|
||||
... "total_amount": "1058.00",
|
||||
... "items": [...]
|
||||
... "items": [{"name": "商品A", "quantity": 2, "price": "100.00"}]
|
||||
... }
|
||||
>>> actions = [
|
||||
... {"type": "link", "text": "查看详情", "uri": "https://..."},
|
||||
... {"type": "postback", "text": "联系客服", "payload": "CONTACT_SUPPORT"}
|
||||
... {"label": "查看详情", "value": "VIEW_DETAILS"},
|
||||
... {"label": "联系客服", "value": "CONTACT_SUPPORT"}
|
||||
... ]
|
||||
>>> await chatwoot.send_order_card(123, order_data, actions)
|
||||
>>> await chatwoot.send_order_form(123, order_data, actions)
|
||||
"""
|
||||
# 生成 Markdown 内容
|
||||
markdown_content = format_order_card_markdown(order_data)
|
||||
# 构建表单字段
|
||||
form_items = []
|
||||
|
||||
# 生成按钮卡片
|
||||
buttons = create_action_buttons(actions)
|
||||
# 订单号(只读文本)
|
||||
form_items.append({
|
||||
"name": "order_id",
|
||||
"label": "订单号",
|
||||
"type": "text",
|
||||
"placeholder": "订单号",
|
||||
"default": order_data.get("order_id", "")
|
||||
})
|
||||
|
||||
# 发送富媒体消息
|
||||
# 订单状态(只读文本)
|
||||
status_text = order_data.get("status_text", order_data.get("status", "unknown"))
|
||||
form_items.append({
|
||||
"name": "status",
|
||||
"label": "订单状态",
|
||||
"type": "text",
|
||||
"placeholder": "订单状态",
|
||||
"default": status_text
|
||||
})
|
||||
|
||||
# 下单时间(只读文本)
|
||||
if order_data.get("created_at"):
|
||||
form_items.append({
|
||||
"name": "created_at",
|
||||
"label": "下单时间",
|
||||
"type": "text",
|
||||
"placeholder": "下单时间",
|
||||
"default": order_data["created_at"]
|
||||
})
|
||||
|
||||
# 商品列表(多行文本)
|
||||
items = order_data.get("items", [])
|
||||
if items:
|
||||
items_text = "\n".join([
|
||||
f"▫️ {item.get('name', '未知商品')} × {item.get('quantity', 1)} - ¥{item.get('price', '0.00')}"
|
||||
for item in items
|
||||
])
|
||||
form_items.append({
|
||||
"name": "items",
|
||||
"label": "商品详情",
|
||||
"type": "text_area",
|
||||
"placeholder": "商品列表",
|
||||
"default": items_text
|
||||
})
|
||||
|
||||
# 总金额(只读文本)
|
||||
form_items.append({
|
||||
"name": "total_amount",
|
||||
"label": "总金额",
|
||||
"type": "text",
|
||||
"placeholder": "总金额",
|
||||
"default": f"¥{order_data.get('total_amount', '0.00')}"
|
||||
})
|
||||
|
||||
# 运费(只读文本)
|
||||
if order_data.get("shipping_fee") is not None:
|
||||
form_items.append({
|
||||
"name": "shipping_fee",
|
||||
"label": "运费",
|
||||
"type": "text",
|
||||
"placeholder": "运费",
|
||||
"default": f"¥{order_data['shipping_fee']}"
|
||||
})
|
||||
|
||||
# 物流信息(多行文本)
|
||||
logistics = order_data.get("logistics")
|
||||
if logistics:
|
||||
logistics_text = (
|
||||
f"承运商: {logistics.get('carrier', '未知')}\n"
|
||||
f"单号: {logistics.get('tracking_number', '未知')}"
|
||||
)
|
||||
form_items.append({
|
||||
"name": "logistics",
|
||||
"label": "物流信息",
|
||||
"type": "text_area",
|
||||
"placeholder": "物流信息",
|
||||
"default": logistics_text
|
||||
})
|
||||
|
||||
# 备注(多行文本)
|
||||
if order_data.get("remark"):
|
||||
form_items.append({
|
||||
"name": "remark",
|
||||
"label": "备注",
|
||||
"type": "text_area",
|
||||
"placeholder": "备注",
|
||||
"default": order_data["remark"]
|
||||
})
|
||||
|
||||
# 操作选项(下拉选择,如果提供了 actions)
|
||||
if actions:
|
||||
form_items.append({
|
||||
"name": "action_select",
|
||||
"label": "操作",
|
||||
"type": "select",
|
||||
"options": actions
|
||||
})
|
||||
|
||||
# 构建 content_attributes
|
||||
content_attributes = {
|
||||
"items": form_items
|
||||
}
|
||||
|
||||
# 发送 form 类型消息
|
||||
return await self.send_rich_message(
|
||||
conversation_id=conversation_id,
|
||||
content=markdown_content,
|
||||
content_type="cards",
|
||||
content_attributes=buttons
|
||||
content="订单详情",
|
||||
content_type="form",
|
||||
content_attributes=content_attributes
|
||||
)
|
||||
|
||||
# ============ Conversations ============
|
||||
|
||||
Reference in New Issue
Block a user