ForcePilot/backend/package/yuxi/channels/adapters/line/quick_reply.py
Kris f736a0ebb3 refactor(line): 重构Line适配器相关代码,优化功能与可读性
1.  抽离alt文本截断逻辑为公共工具函数
2.  重构快速回复构建函数,支持更多action类型
3.  新增 narrowcast 消息发送与进度查询能力
4.  新增用户粉丝数、好友人口统计API调用
5.  优化回复消息逻辑,支持quote token
6.  改进审批ID生成逻辑,避免重复
7.  调整部分配置与异常处理逻辑
8.  新增熔断器与相关指标统计
2026-05-13 16:11:04 +08:00

81 lines
2.7 KiB
Python

from __future__ import annotations
def build_quick_reply_items(options: list[str | dict]) -> list[dict]:
items = []
for opt in options[:13]:
if isinstance(opt, str):
label = opt.strip()
if not label:
continue
safe_label = label[:20]
items.append(
{
"type": "action",
"action": {"type": "message", "label": safe_label, "text": label},
}
)
elif isinstance(opt, dict):
label = opt.get("label", "").strip()
if not label:
continue
safe_label = label[:20]
action_type = opt.get("action_type", "message")
if action_type == "postback":
action = {
"type": "postback",
"label": safe_label,
"data": opt.get("data", ""),
"display_text": opt.get("display_text", safe_label),
}
if "input_option" in opt:
action["inputOption"] = opt["input_option"]
if "fill_in_text" in opt:
action["fillInText"] = opt["fill_in_text"]
elif action_type == "uri":
action = {
"type": "uri",
"label": safe_label,
"uri": opt.get("uri", ""),
}
if opt.get("alt_uri"):
alt_uri = opt["alt_uri"]
if isinstance(alt_uri, dict):
action["altUri"] = alt_uri
elif action_type == "camera":
action = {
"type": "camera",
"label": safe_label,
}
elif action_type == "camera_roll":
action = {
"type": "cameraRoll",
"label": safe_label,
}
elif action_type == "location":
action = {
"type": "location",
"label": safe_label,
}
else:
action = {
"type": "message",
"label": safe_label,
"text": opt.get("text", label),
}
items.append({"type": "action", "action": action})
return items
def build_text_with_quick_reply(text: str, options: list[str | dict]) -> dict:
if not options:
return {"type": "text", "text": text[:5000]}
items = build_quick_reply_items(options)
return {
"type": "text",
"text": text[:5000],
"quickReply": {"items": items},
}