新增 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: 类型定义
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from enum import StrEnum
|
|
|
|
|
|
class LineErrorCode(StrEnum):
|
|
INVALID_REPLY_TOKEN = "invalid_reply_token"
|
|
REPLY_TOKEN_EXPIRED = "reply_token_expired"
|
|
INVALID_CHANNEL_ACCESS_TOKEN = "invalid_channel_access_token"
|
|
CHANNEL_ACCESS_TOKEN_EXPIRED = "channel_access_token_expired"
|
|
RATE_LIMITED = "rate_limited"
|
|
INVALID_MESSAGE = "invalid_message"
|
|
USER_NOT_FOUND = "user_not_found"
|
|
GROUP_NOT_FOUND = "group_not_found"
|
|
NETWORK_ERROR = "network_error"
|
|
UNKNOWN = "unknown"
|
|
|
|
|
|
ERROR_PATTERN_MAP: dict[str, LineErrorCode] = {
|
|
"Invalid reply token": LineErrorCode.INVALID_REPLY_TOKEN,
|
|
"Reply token expired": LineErrorCode.REPLY_TOKEN_EXPIRED,
|
|
"Reply token has already been used": LineErrorCode.REPLY_TOKEN_EXPIRED,
|
|
"Invalid channel access token": LineErrorCode.INVALID_CHANNEL_ACCESS_TOKEN,
|
|
"The channel access token expired": LineErrorCode.CHANNEL_ACCESS_TOKEN_EXPIRED,
|
|
"Rate limit exceeded": LineErrorCode.RATE_LIMITED,
|
|
"Invalid message": LineErrorCode.INVALID_MESSAGE,
|
|
"user not found": LineErrorCode.USER_NOT_FOUND,
|
|
"group not found": LineErrorCode.GROUP_NOT_FOUND,
|
|
}
|
|
|
|
|
|
def classify_line_error(status_code: int, error_message: str) -> LineErrorCode:
|
|
msg_lower = error_message.lower()
|
|
|
|
for pattern, code in ERROR_PATTERN_MAP.items():
|
|
if pattern.lower() in msg_lower:
|
|
return code
|
|
|
|
if status_code == 401:
|
|
return LineErrorCode.INVALID_CHANNEL_ACCESS_TOKEN
|
|
if status_code == 429:
|
|
return LineErrorCode.RATE_LIMITED
|
|
if status_code >= 500:
|
|
return LineErrorCode.NETWORK_ERROR
|
|
|
|
return LineErrorCode.UNKNOWN
|
|
|
|
|
|
def is_reply_token_expired(error: LineErrorCode) -> bool:
|
|
return error in (LineErrorCode.REPLY_TOKEN_EXPIRED, LineErrorCode.INVALID_REPLY_TOKEN)
|
|
|
|
|
|
def is_retryable(error: LineErrorCode) -> bool:
|
|
return error in (
|
|
LineErrorCode.RATE_LIMITED,
|
|
LineErrorCode.NETWORK_ERROR,
|
|
) |