新增 LINE 官方账号对接的全套功能,包括: 1. 基础的 Bot 探测、会话解析、消息格式化能力 2. 富媒体消息模板、快速回复、卡片指令支持 3. Webhook 签名验证、重放防护、多账户路由管理 4. 消息发送、回复、分块传输、用户绑定管理 5. 交互式配置向导与诊断工具
114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def build_card_command(card_type: str, data: dict[str, Any]) -> dict:
|
|
card_builders = {
|
|
"receipt": _build_receipt_cmd,
|
|
"event": _build_event_cmd,
|
|
"agenda": _build_agenda_cmd,
|
|
"media_player": _build_media_player_cmd,
|
|
"device_control": _build_device_control_cmd,
|
|
"apple_tv_remote": _build_apple_tv_remote_cmd,
|
|
}
|
|
builder = card_builders.get(card_type)
|
|
if builder is None:
|
|
return {
|
|
"type": "text",
|
|
"text": f"Unknown card type: {card_type}",
|
|
}
|
|
return builder(data)
|
|
|
|
|
|
def _build_receipt_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.basic_cards import create_receipt_card
|
|
|
|
return create_receipt_card(
|
|
title=data.get("title", "Receipt"),
|
|
items=data.get("items", []),
|
|
total_label=data.get("total_label", "合计"),
|
|
currency=data.get("currency", "¥"),
|
|
alt_text=data.get("alt_text", "Receipt"),
|
|
)
|
|
|
|
|
|
def _build_event_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.basic_cards import create_event_card
|
|
|
|
return create_event_card(
|
|
title=data.get("title", ""),
|
|
date=data.get("date", ""),
|
|
time_range=data.get("time_range", ""),
|
|
location=data.get("location", ""),
|
|
description=data.get("description", ""),
|
|
hero_url=data.get("hero_url"),
|
|
alt_text=data.get("alt_text", "Event"),
|
|
)
|
|
|
|
|
|
def _build_agenda_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.basic_cards import create_agenda_card
|
|
|
|
return create_agenda_card(
|
|
title=data.get("title", "Agenda"),
|
|
events=data.get("events", []),
|
|
alt_text=data.get("alt_text", "Agenda"),
|
|
)
|
|
|
|
|
|
def _build_media_player_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.media_control_cards import (
|
|
create_media_player_card,
|
|
)
|
|
|
|
return create_media_player_card(
|
|
title=data.get("title", ""),
|
|
artist=data.get("artist", ""),
|
|
album_art_url=data.get("album_art_url"),
|
|
playback_state=data.get("playback_state", "paused"),
|
|
alt_text=data.get("alt_text", "Media Player"),
|
|
)
|
|
|
|
|
|
def _build_device_control_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.media_control_cards import (
|
|
create_device_control_card,
|
|
)
|
|
|
|
return create_device_control_card(
|
|
device_name=data.get("device_name", "Device"),
|
|
status=data.get("status", "offline"),
|
|
controls=data.get("controls"),
|
|
hero_url=data.get("hero_url"),
|
|
alt_text=data.get("alt_text", "Device Control"),
|
|
)
|
|
|
|
|
|
def _build_apple_tv_remote_cmd(data: dict) -> dict:
|
|
from yuxi.channels.adapters.line.flex_templates.media_control_cards import (
|
|
create_apple_tv_remote_card,
|
|
)
|
|
|
|
return create_apple_tv_remote_card(
|
|
device_name=data.get("device_name", "Apple TV"),
|
|
alt_text=data.get("alt_text", "Apple TV Remote"),
|
|
)
|
|
|
|
|
|
def resolve_card_command(content: str) -> dict | None:
|
|
import json
|
|
import re
|
|
|
|
match = re.search(r"\[\[card:(\w+):(.+?)\]\]", content)
|
|
if not match:
|
|
return None
|
|
|
|
card_type = match.group(1)
|
|
try:
|
|
data = json.loads(match.group(2))
|
|
except json.JSONDecodeError:
|
|
return None
|
|
|
|
return build_card_command(card_type, data)
|