480 lines
17 KiB
Python
480 lines
17 KiB
Python
"""
|
||
陆运转人工:多项选择卡(业务类型 7 项 + 报价场景 5 项)与选择结果解析。
|
||
|
||
本文件职责:
|
||
1. 维护 `资料/陆运业务类型整理-终版.xlsx` 里产品确认的 7 个业务类型、5 个报价场景;
|
||
2. 把「业务类型 + 报价场景」翻成建单/拉群/出模板要用的标准项
|
||
(运输类型、线路类别、模板名、超时编码、时效小时);
|
||
3. 生成企微 `multiple_interaction` 卡片载荷,供销售一次选完;
|
||
4. 解析企微回调里的 `<SelectedItems>`,把选项 id 收回标准项。
|
||
|
||
为什么单独一个模块:7×5 的对照表属于业务合同,不能散落在话术和流程里;
|
||
产品改表只改这里,流程与话术都不用动。
|
||
|
||
禁止:在此改六态、发明价格、直连 TMS。
|
||
线程:纯函数 + 只读常量,无共享可变状态,可在 Worker 槽调用。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
from typing import Any, Iterable, Mapping, Optional
|
||
|
||
# 卡片问题键。企微 multiple_interaction 回调按 QuestionKey 回传,必须与出卡一致。
|
||
QUESTION_BIZ = "biz_type"
|
||
QUESTION_SCENARIO = "quote_scenario"
|
||
|
||
# 这张卡的卡种前缀:task_id 写成 handoff_land-<工单号或发送人>-<时间戳>,
|
||
# 点卡回调靠它认出「这是陆运的选卡」,不按残留书签去猜运输线。
|
||
CARD_KIND = "handoff_land"
|
||
|
||
# 卡片提交按钮的 EventKey 前缀。点击回来时正文=该键,交 card_action 分发。
|
||
BTN_HANDOFF_LAND_KEY = "handoff_land_submit"
|
||
BTN_HANDOFF_LAND_TEXT = "提交并转人工"
|
||
|
||
# 卡片标题上的说明文案。销售看这一句就知道点完会发生什么。
|
||
CARD_DESC = "请选择业务类型和报价场景;提交后自动建正式工单、拉产品建群"
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class LandBizType:
|
||
"""
|
||
陆运转人工卡片上的一个「业务类型」按钮。
|
||
|
||
字段含义(全部取自产品终版表):
|
||
key 卡片选项 id,回调按它找回本行,改名不影响历史选择;
|
||
label 销售看到的按钮文字;
|
||
transport_type 落到工单「运输类型」的标准值(与 land_options.TRANSPORT_TYPES 同口径);
|
||
route_category 落到工单「线路类别」的标准值,同时用于按线路匹配陆运产品;
|
||
template_name 该业务默认报价模板名(后台 inq_quote_template.name),
|
||
建群发空白模板时按名直取,不再靠关键词猜;
|
||
staff_hint 产品岗备注,仅日志排查用,不参与匹配。
|
||
"""
|
||
|
||
key: str
|
||
label: str
|
||
transport_type: str
|
||
route_category: str
|
||
template_name: str
|
||
staff_hint: str = ""
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class LandScenario:
|
||
"""
|
||
陆运转人工卡片上的一个「报价场景」按钮。
|
||
|
||
timeout_code 是后台 inq_response_timeout.timeout_code;
|
||
sla_hours 只用于群摘要里给产品看的报价时效,0 表示不监测(急件)。
|
||
"""
|
||
|
||
key: str
|
||
label: str
|
||
timeout_code: str
|
||
sla_hours: float
|
||
|
||
|
||
# 7 个业务类型:顺序与 Excel 一致,销售从上往下读。
|
||
# template_name 必须与后台 inq_quote_template.name 逐字一致:
|
||
# 2026-11 产品把陆运模板按「国内 / 东南亚 / 中港·亚·欧」重命名并拆出中港拼车,
|
||
# 现在 7 种业务对应 7 张表,一张不共用:
|
||
# 国内整车 / 国内拼车 / 国内城配·打包 / 东南亚整车 / 东南亚拼车
|
||
# / 中港·亚·欧整车 / 中港·亚·欧拼车
|
||
# 为什么坚持写后台全名:出单是按名直取(match.pick_by_name),写简称时一旦后台改名,
|
||
# 模糊匹配会掉到「中港」这种短词上,拼车单可能套走整车表。改名只改这里。
|
||
BUSINESS_TYPES: tuple[LandBizType, ...] = (
|
||
LandBizType(
|
||
key="dom_ftl",
|
||
label="国内整车",
|
||
transport_type="国内运输整车",
|
||
route_category="国内长途/零担",
|
||
template_name="陆运国内整车报价单",
|
||
),
|
||
LandBizType(
|
||
key="dom_ltl",
|
||
label="国内拼车",
|
||
transport_type="国内运输拼车",
|
||
route_category="国内长途/零担",
|
||
template_name="陆运国内拼车报价单",
|
||
),
|
||
LandBizType(
|
||
key="dom_city",
|
||
label="国内城配/打包",
|
||
transport_type="国内运输整车",
|
||
route_category="国内城配/拖车/打包",
|
||
template_name="陆运国内城配/打包报价单",
|
||
staff_hint="城配/打包走整车口径,产品按城配线路匹配",
|
||
),
|
||
LandBizType(
|
||
key="sea_ftl",
|
||
label="东南亚整车",
|
||
transport_type="跨境整车",
|
||
route_category="东南亚",
|
||
template_name="陆运东南亚整车报价单",
|
||
),
|
||
LandBizType(
|
||
key="sea_ltl",
|
||
label="东南亚拼车",
|
||
transport_type="跨境集拼",
|
||
route_category="东南亚",
|
||
template_name="陆运东南亚拼车报价单",
|
||
),
|
||
LandBizType(
|
||
key="hk_ftl",
|
||
label="中港/亚/欧整车",
|
||
transport_type="中港整车",
|
||
route_category="中港/中亚/中欧",
|
||
template_name="陆运中港/亚/欧整车报价单",
|
||
),
|
||
LandBizType(
|
||
key="hk_ltl",
|
||
label="中港/亚/欧拼车",
|
||
transport_type="中港零担/集拼",
|
||
route_category="中港/中亚/中欧",
|
||
# 后台这张表名尾字是「报价模板」而不是「报价单」,仍是同一张表,照抄不改写。
|
||
template_name="陆运中港/亚/欧拼车报价模板",
|
||
),
|
||
)
|
||
|
||
# 5 个报价场景:时效取自 Excel「拟调整」列(小时)。急件不设时效,0=不监测。
|
||
SCENARIOS: tuple[LandScenario, ...] = (
|
||
LandScenario(
|
||
key="common",
|
||
label="普件(常用线路)",
|
||
timeout_code="land_common",
|
||
sla_hours=2,
|
||
),
|
||
LandScenario(
|
||
key="cross_domestic",
|
||
label="普件(涉及国内+国外段)",
|
||
timeout_code="land_hk_cross",
|
||
sla_hours=8,
|
||
),
|
||
LandScenario(
|
||
key="ca_eu",
|
||
label="普件(涉及中欧、中亚)",
|
||
timeout_code="land_ca_eu",
|
||
sla_hours=24,
|
||
),
|
||
LandScenario(
|
||
key="custom",
|
||
label="定制化(涉及标案、境外段清派、打包、超限、特殊要求)",
|
||
timeout_code="land_hk_special",
|
||
sla_hours=48,
|
||
),
|
||
LandScenario(
|
||
key="express",
|
||
label="急件",
|
||
timeout_code="land_hk_express",
|
||
sla_hours=0,
|
||
),
|
||
)
|
||
|
||
_BIZ_BY_KEY: dict[str, LandBizType] = {b.key: b for b in BUSINESS_TYPES}
|
||
_BIZ_BY_LABEL: dict[str, LandBizType] = {b.label: b for b in BUSINESS_TYPES}
|
||
_SCENARIO_BY_KEY: dict[str, LandScenario] = {s.key: s for s in SCENARIOS}
|
||
_SCENARIO_BY_LABEL: dict[str, LandScenario] = {s.label: s for s in SCENARIOS}
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class LandSelection:
|
||
"""一次卡片提交的结论:业务类型 + 报价场景,以及由它们推出的标准项。"""
|
||
|
||
biz: LandBizType
|
||
scenario: LandScenario
|
||
|
||
@property
|
||
def biz_label(self) -> str:
|
||
return self.biz.label
|
||
|
||
@property
|
||
def scenario_label(self) -> str:
|
||
return self.scenario.label
|
||
|
||
@property
|
||
def transport_type(self) -> str:
|
||
return self.biz.transport_type
|
||
|
||
@property
|
||
def route_category(self) -> str:
|
||
return self.biz.route_category
|
||
|
||
@property
|
||
def template_name(self) -> str:
|
||
return self.biz.template_name
|
||
|
||
@property
|
||
def timeout_code(self) -> str:
|
||
return self.scenario.timeout_code
|
||
|
||
@property
|
||
def sla_hours(self) -> float:
|
||
return self.scenario.sla_hours
|
||
|
||
|
||
def biz_by_key(raw: str) -> Optional[LandBizType]:
|
||
"""按卡片选项 id 或按钮文字找业务类型;认不到返回 None。"""
|
||
text = (raw or "").strip()
|
||
if not text:
|
||
return None
|
||
return _BIZ_BY_KEY.get(text) or _BIZ_BY_LABEL.get(text)
|
||
|
||
|
||
def scenario_by_key(raw: str) -> Optional[LandScenario]:
|
||
"""按卡片选项 id 或按钮文字找报价场景;认不到返回 None。"""
|
||
text = (raw or "").strip()
|
||
if not text:
|
||
return None
|
||
return _SCENARIO_BY_KEY.get(text) or _SCENARIO_BY_LABEL.get(text)
|
||
|
||
|
||
def biz_for_pair(transport_type: str, route_category: str) -> Optional[LandBizType]:
|
||
"""
|
||
按工单里的「运输类型 + 线路类别」反查业务类型按钮。
|
||
|
||
为什么需要:合并成两项后,卡片只存业务类型;但历史工单、DeepSeek 抽到的
|
||
口语字段、旧卡回调里只有运输类型和线路类别,预勾选与文本回显都要能认回来。
|
||
城配/打包与国内整车共用运输类型,必须靠线路区分,不能只看运输类型。
|
||
副作用:无。
|
||
"""
|
||
t = (transport_type or "").strip()
|
||
r = (route_category or "").strip()
|
||
if not t or not r:
|
||
return None
|
||
for biz in BUSINESS_TYPES:
|
||
if biz.transport_type == t and biz.route_category == r:
|
||
return biz
|
||
return None
|
||
|
||
|
||
def _first_ids(raw: Any) -> list[str]:
|
||
"""回调既有列表也有单个字符串,统一成去空字符串列表。"""
|
||
if raw is None:
|
||
return []
|
||
if isinstance(raw, str):
|
||
text = raw.strip()
|
||
return [text] if text else []
|
||
if isinstance(raw, Iterable):
|
||
out: list[str] = []
|
||
for item in raw:
|
||
text = str(item or "").strip()
|
||
if text:
|
||
out.append(text)
|
||
return out
|
||
text = str(raw).strip()
|
||
return [text] if text else []
|
||
|
||
|
||
def pick_biz(selected: Mapping[str, Any] | None) -> Optional[LandBizType]:
|
||
"""
|
||
从 `{question_key: [option_id...]}` 里取业务类型。
|
||
|
||
单选卡正常只有一个 id;老卡/恶意回传多个时取第一个认得的,
|
||
避免一次点击建出两张不同业务的单。
|
||
"""
|
||
for opt in _first_ids((selected or {}).get(QUESTION_BIZ)):
|
||
hit = biz_by_key(opt)
|
||
if hit is not None:
|
||
return hit
|
||
return None
|
||
|
||
|
||
def pick_scenario(selected: Mapping[str, Any] | None) -> Optional[LandScenario]:
|
||
"""从回调里取报价场景;规则同 pick_biz。"""
|
||
for opt in _first_ids((selected or {}).get(QUESTION_SCENARIO)):
|
||
hit = scenario_by_key(opt)
|
||
if hit is not None:
|
||
return hit
|
||
return None
|
||
|
||
|
||
def resolve_selection(selected: Mapping[str, Any] | None) -> Optional[LandSelection]:
|
||
"""
|
||
把卡片回调收成建单要用的结论。
|
||
|
||
两项都选到才返回;缺一项返回 None,由流程重新发卡并提示,
|
||
不允许「只选了业务类型」就建群——没有报价场景就没法定超时时效。
|
||
"""
|
||
biz = pick_biz(selected)
|
||
scenario = pick_scenario(selected)
|
||
if biz is None or scenario is None:
|
||
return None
|
||
return LandSelection(biz=biz, scenario=scenario)
|
||
|
||
|
||
def selection_from_facts(facts: Mapping[str, Any] | None) -> Optional[LandSelection]:
|
||
"""
|
||
会话事实里已经有「业务类型 + 报价场景」时,直接合成选择结论。
|
||
|
||
为什么需要:销售在补问阶段/补问卡上已经选过这两项,之后再说「转人工」
|
||
不能再让他点一遍同样的卡(正服实测 WO20261170037 就这样重复问了)。
|
||
两项都在就跳过卡片直接建群;缺任何一项仍返回 None,由流程出卡。
|
||
|
||
取值顺序(都是同一套标准项,认不到就不猜):
|
||
业务类型:陆运业务类型 → 运输类型+线路类别配对反查
|
||
报价场景:报价场景 → 运输分类
|
||
返回 None 表示信息不完整。副作用:无,纯只读。
|
||
"""
|
||
src = dict(facts or {})
|
||
biz = biz_by_key(str(src.get("陆运业务类型") or "").strip())
|
||
if biz is None:
|
||
biz = biz_for_pair(
|
||
str(src.get("运输类型") or "").strip(),
|
||
str(src.get("线路类别") or "").strip(),
|
||
)
|
||
scenario = scenario_by_key(str(src.get("报价场景") or "").strip())
|
||
if scenario is None:
|
||
scenario = scenario_by_key(str(src.get("运输分类") or "").strip())
|
||
if biz is None or scenario is None:
|
||
return None
|
||
return LandSelection(biz=biz, scenario=scenario)
|
||
|
||
|
||
def _option_list(items: Iterable[Any], *, checked: str = "") -> list[dict[str, Any]]:
|
||
"""
|
||
生成企微 option_list。
|
||
|
||
checked 命中的那项预勾选:销售私聊里已经说过业务类型时不用再点一次。
|
||
没命中就都不预选,避免系统替销售做决定。
|
||
"""
|
||
out: list[dict[str, Any]] = []
|
||
for item in items:
|
||
row: dict[str, Any] = {"id": item.key, "text": item.label}
|
||
if checked and item.key == checked:
|
||
row["is_checked"] = True
|
||
out.append(row)
|
||
return out
|
||
|
||
|
||
def _preselect_biz(facts: Mapping[str, Any] | None) -> str:
|
||
"""按会话里已填的运输类型/线路类别预勾选业务类型;对不上就不勾。"""
|
||
src = dict(facts or {})
|
||
transport = str(src.get("运输类型") or "").strip()
|
||
route = str(src.get("线路类别") or "").strip()
|
||
for biz in BUSINESS_TYPES:
|
||
if biz.transport_type == transport and biz.route_category == route:
|
||
return biz.key
|
||
# 城配与国內整车同运输类型,靠线路区分;上面已经处理,这里不再兜底猜。
|
||
return ""
|
||
|
||
|
||
def _preselect_scenario(facts: Mapping[str, Any] | None) -> str:
|
||
"""按会话里已填的运输分类预勾选报价场景;对不上就不勾。"""
|
||
klass = str((facts or {}).get("运输分类") or "").strip()
|
||
for scenario in SCENARIOS:
|
||
if scenario.label == klass:
|
||
return scenario.key
|
||
return ""
|
||
|
||
|
||
def land_handoff_card_payload(
|
||
*,
|
||
task_token: str,
|
||
reason: str = "",
|
||
facts: Mapping[str, Any] | None = None,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
陆运转人工多项选择卡。
|
||
|
||
参数:
|
||
task_token 出卡令牌(一般是工单号或发送人),进 task_id 与按钮 key,
|
||
回调靠它找回会话;每次出卡 task_id 都加时间后缀,避免企微 42014;
|
||
reason 卡片主标题(转人工原因);
|
||
facts 当前会话已填字段,用于预勾选。
|
||
|
||
返回:企微 template_card 出站载荷。
|
||
副作用:无。
|
||
"""
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
token = (task_token or "handoff").strip() or "handoff"
|
||
why = (reason or "").strip() or copy.HANDOFF_INTENT_REASON
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "multiple_interaction",
|
||
"source": {"desc": "转人工", "desc_color": 0},
|
||
"task_id": copy.card_task_id(CARD_KIND, token),
|
||
"main_title": {"title": why, "desc": CARD_DESC},
|
||
"select_list": [
|
||
{
|
||
"question_key": QUESTION_BIZ,
|
||
"title": "业务类型(单选)",
|
||
"option_list": _option_list(
|
||
BUSINESS_TYPES, checked=_preselect_biz(facts)
|
||
),
|
||
"selected_type": 1,
|
||
},
|
||
{
|
||
"question_key": QUESTION_SCENARIO,
|
||
"title": "报价场景(单选)",
|
||
"option_list": _option_list(
|
||
SCENARIOS, checked=_preselect_scenario(facts)
|
||
),
|
||
"selected_type": 1,
|
||
},
|
||
],
|
||
"submit_button": {
|
||
"text": BTN_HANDOFF_LAND_TEXT,
|
||
"key": f"{BTN_HANDOFF_LAND_KEY}:{token}",
|
||
},
|
||
},
|
||
}
|
||
|
||
|
||
def deadline_text(hours: float) -> str:
|
||
"""群摘要里的报价时效:整数去小数点;0 表示急件不设时效。"""
|
||
try:
|
||
value = float(hours or 0)
|
||
except (TypeError, ValueError):
|
||
value = 0.0
|
||
if value <= 0:
|
||
return "急件,不设报价时效"
|
||
if value == int(value):
|
||
return f"{int(value)}小时"
|
||
return f"{value:g}小时"
|
||
|
||
|
||
def text_fallback(reason: str = "") -> str:
|
||
"""
|
||
卡片发不出去时的文字兜底。
|
||
|
||
企微模板卡偶发 42014/权限失败时只能发文字;把两组编号列清楚,
|
||
销售回「1 3」即可继续,不用去后台。
|
||
"""
|
||
why = (reason or "").strip() or "已识别您希望转人工处理"
|
||
lines = [why, "运输方式:陆运", "", "业务类型:"]
|
||
for i, biz in enumerate(BUSINESS_TYPES, start=1):
|
||
lines.append(f"{i}.{biz.label}")
|
||
lines.append("")
|
||
lines.append("报价场景:")
|
||
for i, scenario in enumerate(SCENARIOS, start=1):
|
||
lines.append(f"{i}.{scenario.label}")
|
||
lines.append("")
|
||
lines.append("请直接回复两个编号,例如「1 3」(业务类型 1,报价场景 3)。")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def parse_text_selection(text: str) -> Optional[LandSelection]:
|
||
"""
|
||
解析文字兜底的编号回复,例如「1 3」「1,3」「业务类型1 场景3」。
|
||
|
||
只认 1~7 与 1~5 的两个数字:第一个当业务类型、第二个当报价场景。
|
||
句子超过 12 个字符、或数字多于两个时不认——那是询价正文,
|
||
不能因为里面出现「2」就把单转人工。
|
||
"""
|
||
import re
|
||
|
||
raw = (text or "").strip()
|
||
if not raw or len(raw) > 12:
|
||
return None
|
||
numbers = re.findall(r"\d+", raw)
|
||
if len(numbers) != 2:
|
||
return None
|
||
biz_idx, scenario_idx = int(numbers[0]), int(numbers[1])
|
||
if not (1 <= biz_idx <= len(BUSINESS_TYPES)):
|
||
return None
|
||
if not (1 <= scenario_idx <= len(SCENARIOS)):
|
||
return None
|
||
return LandSelection(biz=BUSINESS_TYPES[biz_idx - 1], scenario=SCENARIOS[scenario_idx - 1])
|