为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通BOT,应用只负责拉群和解散群,并把几处销售已反馈的口径补齐。 群与身份: - 新增 group_bot 短协调(Redis 带环境前缀):记录 BOT 已接管群、当前关联工单、 @BOT 指令窗口与窗口外先到的附件,只做协调不当六态,六态仍在主账。 - 群里没有 @BOT 的文字不回;非 @ 文字断开当前单关联,之后必须重新 @BOT 带工单号。 - 先发文件再 @BOT 也认:窗口外附件先缓冲,等 @BOT 认领后补办。 - 会话存档只补附件、不重复办理 BOT 已办的同一句文字,避免同一条消息回两遍。 - 入站 from.userid 是加密 open_userid 时按官方接口换明文(带 Redis 缓存), 否则与主账 sales_wecom_id 永远比不上,航线本人会被判成「不是本人」。 - 桥侧只认本群 req_id,不再借用别的群 req_id,避免测服把话发进别的群。 报价单: - 建群后只发字段摘要和成交卡,Excel/PDF 等群里 @BOT 说「生成报价单」再出; 多段逐段出,没价出空白表;出表后补推成交跟进不涨 deal_version、不重置等待计时。 - 陆运转人工先选业务类型和报价场景再建群,按选定模板名直取,不再靠关键词猜。 - 空运舱位只回一条时直接出该条报价,不再发选择卡。 字段与金额: - 客户名称改为海/空/陆询价非必填,有值写回工单列,后台列表、报价单与 factsJson 一致。 - 空运新增计费重投影:体积重=CBM×166.7,计费重=max(毛重,体积重); TMS 无价、航线手工按 KG 报价时按计费重×单价算整票金额,群摘要与报价单同公式。 - 陆运报价附加条件统一到 land_quote_condition:未含税/含0税/未含燃油/含燃油费, 燃油只乘主运费,不乘提货送货报关等杂费;跨境整车无备注列时追加到底部备注事项。 - 海运拼柜不适用箱型箱量:不进必填、不进补问、不出站,改回整柜可再用。 - 海运提货地址/派送地址改为非必填,写了才出站(placeOfReceipt/deliveryAddress), 禁止从港口或贸易条款反推地址;TMS 查不到价时把 TMS 原话透给销售。 - 中港集拼与跨境集拼同口径:按体积+毛重,不采车型数量;修正被国内拼车别名抢单。 - 报关费区只收报关类费目;口岸海关监管费+接驳费按车型列回填固定行。 主账与后台: - 看板处理量按协同群处理人统计;下线「平均报价销售额」;处理人搜索先把姓名换回企微 id。 - 「转人工」筛选同时兼容旧六态与新 collab_mode=handoff,新版单不再漏。 - 报价单客户名称、提货/派送地址、空运计费重、拼柜空箱型按新口径填表。 - 新增协同成员查询支持与 AIR_NEED_WO v2 迁移脚本。 文档:04 补海运/空运/陆运必填与地址口径,10 补地址出站规则,12 补测服独立应用 1000015 的部署与切换记录。 Co-authored-by: Cursor <cursoragent@cursor.com>
173 lines
6.0 KiB
Python
173 lines
6.0 KiB
Python
"""
|
|
微盛群消息 → InboundMessage。
|
|
|
|
本文件职责:从管家 records 抽出 sender_id / chat_id / 正文 / 附件名。
|
|
禁止:把存档席「询价机器人」当成发言人入业务;禁止再向企微换 ID。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any, Mapping, Optional, Sequence
|
|
|
|
from agent.channel.wecom.models import InboundMessage
|
|
|
|
# 存档席在群里的显示名;from 偶发不是 userid 时也要丢掉。
|
|
# 2026-11-19 真机:群里显示「会话存档机器人」(无连字符),两种写法都排除。
|
|
SEAT_DISPLAY_NAMES = frozenset({"询价机器人", "会话存档机器人", "会话存档-机器人"})
|
|
_SKIP_TYPES = frozenset({"revoke", "recall", "event", "unknown"})
|
|
_FILE_TYPES = frozenset({"file", "image", "video", "emotion", "voice"})
|
|
|
|
|
|
def _as_mapping(raw: Any) -> dict[str, Any]:
|
|
if isinstance(raw, Mapping):
|
|
return dict(raw)
|
|
if isinstance(raw, str) and raw.strip().startswith("{"):
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except json.JSONDecodeError:
|
|
return {}
|
|
return dict(parsed) if isinstance(parsed, Mapping) else {}
|
|
return {}
|
|
|
|
|
|
def _create_time(msgtime: Any) -> int:
|
|
"""微盛 msgtime 常是毫秒;InboundMessage.create_time 用秒。"""
|
|
try:
|
|
n = int(msgtime or 0)
|
|
except (TypeError, ValueError):
|
|
return 0
|
|
if n > 10**12:
|
|
return n // 1000
|
|
return n
|
|
|
|
|
|
def _unwrap_text(raw: Any) -> str:
|
|
"""
|
|
从字符串或嵌套对象取出正文。
|
|
|
|
管家 content 经常是摘要;企微官方结构是 text.content。空、null 不当正文。
|
|
"""
|
|
if raw is None:
|
|
return ""
|
|
if isinstance(raw, str):
|
|
text = raw.strip()
|
|
if not text or text.lower() in {"null", "none"}:
|
|
return ""
|
|
if text.startswith("{") or text.startswith("["):
|
|
try:
|
|
return _unwrap_text(json.loads(text))
|
|
except json.JSONDecodeError:
|
|
return text
|
|
return text
|
|
if isinstance(raw, Mapping):
|
|
for key in ("content", "text", "msg"):
|
|
got = _unwrap_text(raw.get(key))
|
|
if got:
|
|
return got
|
|
return ""
|
|
return str(raw).strip()
|
|
|
|
|
|
def _best_text(record: Mapping[str, Any], payload: Mapping[str, Any]) -> str:
|
|
"""多处正文里取最长的一份,避免摘要盖掉带金额的原文。"""
|
|
candidates = [
|
|
_unwrap_text(record.get("content")),
|
|
_unwrap_text(payload.get("content")),
|
|
_unwrap_text(payload.get("text")),
|
|
_unwrap_text(payload.get("msg")),
|
|
]
|
|
texts = [x for x in candidates if x]
|
|
if not texts:
|
|
return ""
|
|
return max(texts, key=len)
|
|
|
|
|
|
def _filename(msg_type: str, content: str, payload: Mapping[str, Any]) -> str:
|
|
for key in ("filename", "file_name", "FileName", "title", "name"):
|
|
val = str(payload.get(key) or "").strip()
|
|
if val:
|
|
return val
|
|
if msg_type in _FILE_TYPES:
|
|
return str(content or "").strip()
|
|
return ""
|
|
|
|
|
|
def archive_record_to_inbound(
|
|
record: Mapping[str, Any],
|
|
*,
|
|
seat_userids: Sequence[str] = (),
|
|
) -> Optional[InboundMessage]:
|
|
"""
|
|
一条管家记录变成群入站。
|
|
|
|
返回 None:不是群、没有发言人、发言人是存档席、或撤回类消息。
|
|
message_id 带 wshoto: 前缀,避免和回调 MsgId 撞车。
|
|
"""
|
|
room_id = str(record.get("room_id") or record.get("roomid") or "").strip()
|
|
sender_id = str(record.get("from") or record.get("from_id") or "").strip()
|
|
if not room_id or not sender_id:
|
|
return None
|
|
seats = {str(x).strip() for x in seat_userids if str(x).strip()}
|
|
# 机器人自己的出站文字也会被存档听到:不能当业务入站,否则会把自己的
|
|
# 「报价单已发送」当成非 @ 文字打断当前工单关联。旧名/现名一起排除。
|
|
try:
|
|
from agent.policy.bot_name import bot_mention_names
|
|
|
|
bot_senders = set(bot_mention_names())
|
|
except Exception: # noqa: BLE001
|
|
bot_senders = set()
|
|
sender_name = str(record.get("from_name") or record.get("fromName") or "").strip()
|
|
if (
|
|
sender_id in seats
|
|
or sender_id in SEAT_DISPLAY_NAMES
|
|
or sender_id in bot_senders
|
|
or sender_name in bot_senders
|
|
):
|
|
return None
|
|
|
|
msg_type = str(record.get("msgtype") or record.get("msg_type") or "text").strip().lower()
|
|
if msg_type in _SKIP_TYPES:
|
|
return None
|
|
|
|
payload = _as_mapping(record.get("msg_content") or record.get("msgcontent"))
|
|
content = _best_text(record, payload)
|
|
msgid = str(record.get("msgid") or record.get("msg_id") or "").strip()
|
|
seq = record.get("seq")
|
|
if not msgid:
|
|
msgid = f"{room_id}:{seq or 0}:{sender_id}"
|
|
|
|
media: dict[str, Any] = {}
|
|
inbound_type = "text"
|
|
if msg_type in _FILE_TYPES:
|
|
inbound_type = "file" if msg_type == "file" else msg_type
|
|
media = {
|
|
"filename": _filename(msg_type, content, payload),
|
|
"media_id": str(payload.get("sdkfileid") or payload.get("sdk_file_id") or ""),
|
|
"file_ext": str(payload.get("fileext") or payload.get("file_ext") or ""),
|
|
"content_type": "",
|
|
"filesize": payload.get("filesize") or payload.get("file_size") or 0,
|
|
}
|
|
if not content:
|
|
content = str(media.get("filename") or "")
|
|
|
|
return InboundMessage(
|
|
sender_id=sender_id,
|
|
message_id=f"wshoto:{msgid}",
|
|
content=content,
|
|
msg_type=inbound_type,
|
|
chat_id=room_id,
|
|
chat_type="group",
|
|
create_time=_create_time(record.get("msgtime") or record.get("msg_time")),
|
|
media=media,
|
|
raw={
|
|
"source": "wshoto_archive",
|
|
# 存档是「公司级」数据:测服与正服共用同一个企微公司,测服也会捞到正服群。
|
|
# 打上 origin 让 dispatch 能区分「本机 AIBOT 被 @」与「存档旁听」再做环境隔离。
|
|
"wshoto_origin": "archive",
|
|
"seq": seq,
|
|
"msgtype": msg_type,
|
|
"direction": record.get("direction") or "",
|
|
},
|
|
)
|