新增 LINE 渠道扩展,支持在 Yuxi 平台中集成 LINE 即时通讯渠道。 包含以下功能模块: - bot: LINE Bot 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - token_manager: Token 管理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - flex_templates: Flex 模板消息 - card_command: 卡片指令处理 - template_messages: 模板消息 - rich_menu: 富菜单管理 - actions: 动作处理 - directives: 指令处理 - delivery: 消息送达确认 - loading: 加载动画 - media: 媒体资源处理 - types: 类型定义
373 lines
12 KiB
Python
373 lines
12 KiB
Python
from __future__ import annotations
|
|
|
|
LINE_GREEN = "#06C755"
|
|
LINE_LIGHT_GRAY = "#F5F5F5"
|
|
LINE_WHITE = "#FFFFFF"
|
|
LINE_FAFAFA = "#FAFAFA"
|
|
|
|
|
|
def create_receipt_card(
|
|
title: str,
|
|
items: list[tuple[str, str]],
|
|
*,
|
|
total: str | None = None,
|
|
footer: str | None = None,
|
|
) -> dict:
|
|
body_contents: list[dict] = []
|
|
for i, (label, value) in enumerate(items):
|
|
bg_color = LINE_WHITE if i % 2 == 0 else LINE_FAFAFA
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"backgroundColor": bg_color,
|
|
"contents": [
|
|
{"type": "text", "text": label, "flex": 1, "wrap": True, "size": "sm"},
|
|
{"type": "text", "text": value, "flex": 1, "wrap": True, "align": "end", "size": "sm"},
|
|
],
|
|
})
|
|
|
|
if total:
|
|
body_contents.append({"type": "separator", "margin": "md"})
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"contents": [
|
|
{"type": "text", "text": "Total", "flex": 1, "weight": "bold", "color": LINE_GREEN},
|
|
{"type": "text", "text": total, "flex": 1, "align": "end", "weight": "bold", "color": LINE_GREEN},
|
|
],
|
|
})
|
|
|
|
bubble: dict = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": [{"type": "text", "text": title, "weight": "bold", "size": "lg", "wrap": True}]
|
|
+ body_contents,
|
|
},
|
|
}
|
|
|
|
if footer:
|
|
attach_footer_text(bubble, footer)
|
|
|
|
return to_flex_message(title, bubble)
|
|
|
|
|
|
def create_event_card(
|
|
title: str,
|
|
date: str,
|
|
time: str,
|
|
*,
|
|
location: str = "",
|
|
description: str = "",
|
|
) -> dict:
|
|
body_contents: list[dict] = [
|
|
{
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"contents": [
|
|
{"type": "text", "text": date, "weight": "bold", "size": "xl", "color": "#555555"},
|
|
],
|
|
},
|
|
{"type": "text", "text": time, "size": "sm", "color": "#888888", "margin": "sm"},
|
|
]
|
|
|
|
if location:
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"contents": [
|
|
{
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"backgroundColor": LINE_LIGHT_GRAY,
|
|
"cornerRadius": "4px",
|
|
"paddingAll": "md",
|
|
"contents": [{"type": "text", "text": location, "size": "sm", "wrap": True}],
|
|
},
|
|
],
|
|
"margin": "md",
|
|
})
|
|
|
|
if description:
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"contents": [
|
|
{
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"backgroundColor": LINE_LIGHT_GRAY,
|
|
"cornerRadius": "4px",
|
|
"paddingAll": "md",
|
|
"contents": [{"type": "text", "text": description, "size": "sm", "wrap": True, "color": "#666666"}],
|
|
},
|
|
],
|
|
"margin": "md",
|
|
})
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": [{"type": "text", "text": title, "weight": "bold", "size": "lg", "wrap": True}]
|
|
+ body_contents,
|
|
},
|
|
}
|
|
|
|
return to_flex_message(title, bubble)
|
|
|
|
|
|
def create_agenda_card(title: str, events: list[dict]) -> dict:
|
|
body_contents: list[dict] = [{"type": "text", "text": title, "weight": "bold", "size": "lg", "wrap": True}]
|
|
|
|
for i, evt in enumerate(events):
|
|
dot_color = LINE_GREEN if i == 0 else "#CCCCCC"
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"spacing": "md",
|
|
"contents": [
|
|
{
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"flex": 0,
|
|
"width": "12px",
|
|
"contents": [
|
|
{"type": "text", "text": "●", "color": dot_color, "size": "xs", "align": "center"},
|
|
],
|
|
},
|
|
{
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"flex": 1,
|
|
"contents": [
|
|
{"type": "text", "text": evt.get("name", ""), "size": "sm", "wrap": True, "weight": "bold"},
|
|
{
|
|
"type": "text",
|
|
"text": evt.get("time", ""),
|
|
"size": "xs",
|
|
"color": "#888888",
|
|
"margin": "sm",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
"margin": "md",
|
|
})
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": body_contents,
|
|
},
|
|
}
|
|
|
|
return to_flex_message(title, bubble)
|
|
|
|
|
|
def create_media_player_card(
|
|
song: str,
|
|
artist: str,
|
|
source: str,
|
|
image_url: str,
|
|
state: str = "playing",
|
|
) -> dict:
|
|
body_contents: list[dict] = [
|
|
{
|
|
"type": "image",
|
|
"url": image_url,
|
|
"size": "full",
|
|
"aspectRatio": "1:1",
|
|
"aspectMode": "cover",
|
|
},
|
|
{"type": "text", "text": song, "weight": "bold", "size": "lg", "wrap": True, "margin": "lg"},
|
|
{"type": "text", "text": artist, "size": "sm", "color": "#888888", "wrap": True, "margin": "sm"},
|
|
{"type": "text", "text": source, "size": "xs", "color": "#AAAAAA", "wrap": True, "margin": "sm"},
|
|
]
|
|
|
|
controls = [
|
|
{"type": "button", "action": {"type": "message", "label": "⏮", "text": "/media prev"}, "flex": 1},
|
|
{"type": "button", "action": {"type": "message", "label": "▶", "text": "/media play"}, "flex": 1} if state != "playing" else
|
|
{"type": "button", "action": {"type": "message", "label": "⏸", "text": "/media pause"}, "flex": 1},
|
|
{"type": "button", "action": {"type": "message", "label": "⏭", "text": "/media next"}, "flex": 1},
|
|
]
|
|
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"spacing": "sm",
|
|
"contents": controls,
|
|
"margin": "lg",
|
|
})
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": body_contents,
|
|
},
|
|
}
|
|
|
|
return to_flex_message(song, bubble)
|
|
|
|
|
|
def create_device_control_card(
|
|
name: str,
|
|
device_type: str,
|
|
state: str,
|
|
controls: list[dict],
|
|
) -> dict:
|
|
state_color = LINE_GREEN if state.lower() in ("on", "online", "playing") else "#CC0000"
|
|
|
|
body_contents: list[dict] = [
|
|
{"type": "text", "text": name, "weight": "bold", "size": "lg", "wrap": True},
|
|
{
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"spacing": "sm",
|
|
"contents": [
|
|
{"type": "text", "text": "●", "color": state_color, "size": "sm", "flex": 0},
|
|
{"type": "text", "text": f"{device_type} · {state}", "size": "sm", "color": "#888888", "flex": 1},
|
|
],
|
|
"margin": "md",
|
|
},
|
|
]
|
|
|
|
if controls:
|
|
button_row_contents: list[dict] = []
|
|
for ctrl in controls:
|
|
action_type = ctrl.get("action_type", "message")
|
|
if action_type == "uri":
|
|
action = {"type": "uri", "label": ctrl["label"], "uri": ctrl["data"]}
|
|
elif action_type == "postback":
|
|
action = {"type": "postback", "label": ctrl["label"], "data": ctrl["data"]}
|
|
else:
|
|
action = {"type": "message", "label": ctrl["label"], "text": ctrl["data"]}
|
|
button_row_contents.append({"type": "button", "action": action, "flex": 1, "height": "sm"})
|
|
|
|
body_contents.append({
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"spacing": "sm",
|
|
"contents": button_row_contents,
|
|
"margin": "lg",
|
|
})
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": body_contents,
|
|
},
|
|
}
|
|
|
|
return to_flex_message(name, bubble)
|
|
|
|
|
|
def create_apple_tv_remote(name: str, state: str = "idle") -> dict:
|
|
dpad_contents: list[dict] = []
|
|
|
|
empty = {"type": "text", "text": " ", "size": "sm", "align": "center", "flex": 1}
|
|
up = {"type": "button", "action": {"type": "message", "label": "↑", "text": "/atv up"}, "flex": 1, "height": "sm"}
|
|
left = {"type": "button", "action": {"type": "message", "label": "←", "text": "/atv left"}, "flex": 1, "height": "sm"}
|
|
ok = {"type": "button", "action": {"type": "message", "label": "OK", "text": "/atv select"}, "flex": 1, "height": "sm",
|
|
"color": "#555555", "style": "primary"}
|
|
right = {"type": "button", "action": {"type": "message", "label": "→", "text": "/atv right"}, "flex": 1, "height": "sm"}
|
|
down = {"type": "button", "action": {"type": "message", "label": "↓", "text": "/atv down"}, "flex": 1, "height": "sm"}
|
|
|
|
dpad_contents = [
|
|
{"type": "box", "layout": "horizontal", "contents": [empty, up, empty], "spacing": "sm"},
|
|
{"type": "box", "layout": "horizontal", "contents": [left, ok, right], "spacing": "sm"},
|
|
{"type": "box", "layout": "horizontal", "contents": [empty, down, empty], "spacing": "sm"},
|
|
]
|
|
|
|
body_contents: list[dict] = [
|
|
{"type": "text", "text": name, "weight": "bold", "size": "lg", "wrap": True},
|
|
{"type": "text", "text": state, "size": "sm", "color": "#888888", "margin": "sm"},
|
|
]
|
|
|
|
body_contents.extend(dpad_contents)
|
|
|
|
media_controls = {
|
|
"type": "box",
|
|
"layout": "horizontal",
|
|
"spacing": "sm",
|
|
"contents": [
|
|
{"type": "button", "action": {"type": "message", "label": "⏮", "text": "/atv prev"}, "flex": 1,
|
|
"height": "sm"},
|
|
{"type": "button", "action": {"type": "message", "label": "▶", "text": "/atv play"}, "flex": 1,
|
|
"height": "sm"},
|
|
{"type": "button", "action": {"type": "message", "label": "⏭", "text": "/atv next"}, "flex": 1,
|
|
"height": "sm"},
|
|
],
|
|
"margin": "lg",
|
|
}
|
|
body_contents.append(media_controls)
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": body_contents,
|
|
},
|
|
}
|
|
|
|
return to_flex_message(name, bubble)
|
|
|
|
|
|
def create_code_block_card(code: str, language: str = "") -> dict:
|
|
truncated = code[:2000]
|
|
title = f"Code ({language})" if language else "Code"
|
|
|
|
bubble = {
|
|
"type": "bubble",
|
|
"body": {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"backgroundColor": LINE_LIGHT_GRAY,
|
|
"cornerRadius": "4px",
|
|
"paddingAll": "lg",
|
|
"contents": [
|
|
{"type": "text", "text": title, "weight": "bold", "size": "sm", "color": "#888888"},
|
|
{"type": "text", "text": truncated, "size": "xs", "wrap": True, "margin": "md"},
|
|
],
|
|
},
|
|
}
|
|
|
|
return to_flex_message(title, bubble)
|
|
|
|
|
|
def attach_footer_text(bubble: dict, footer: str) -> None:
|
|
footer_box = {
|
|
"type": "box",
|
|
"layout": "vertical",
|
|
"contents": [
|
|
{"type": "text", "text": footer, "size": "xs", "color": "#AAAAAA", "wrap": True},
|
|
],
|
|
}
|
|
if "footer" not in bubble or not bubble["footer"]:
|
|
bubble["footer"] = footer_box
|
|
else:
|
|
existing = bubble["footer"]
|
|
if isinstance(existing, dict) and existing.get("type") == "box":
|
|
existing.setdefault("contents", []).extend(
|
|
[
|
|
{"type": "separator", "margin": "sm"},
|
|
{"type": "text", "text": footer, "size": "xs", "color": "#AAAAAA", "wrap": True},
|
|
]
|
|
)
|
|
|
|
|
|
def to_flex_message(alt_text: str, contents: dict) -> dict:
|
|
return {
|
|
"type": "flex",
|
|
"altText": alt_text[:400],
|
|
"contents": contents,
|
|
} |