新增 Telegram 渠道扩展,支持在 Yuxi 平台中集成 Telegram 即时通讯渠道。 包含以下功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - polling: 长轮询模式 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - actions: 动作处理 - inline_keyboard: 内联键盘 - native_commands: 原生指令 - chat: 聊天管理 - delivery: 消息送达确认 - media: 媒体资源处理 - profile: 用户资料 - reactions: 表情反应 - sticker: 贴纸处理 - types: 类型定义
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from enum import StrEnum
|
|
|
|
|
|
class TelegramErrorKind(StrEnum):
|
|
RETRYABLE = "retryable"
|
|
RATE_LIMITED = "rate_limited"
|
|
AUTH = "auth"
|
|
CHAT_NOT_FOUND = "chat_not_found"
|
|
BOT_BLOCKED = "bot_blocked"
|
|
PARSE_ERROR = "parse_error"
|
|
MEDIA_TOO_LARGE = "media_too_large"
|
|
FATAL = "fatal"
|
|
NETWORK = "network"
|
|
|
|
|
|
def classify_error(
|
|
status_code: int | None, error_body: dict | str | None,
|
|
) -> tuple[TelegramErrorKind, str, float | None]:
|
|
description = ""
|
|
if isinstance(error_body, dict):
|
|
description = error_body.get("description", "")
|
|
elif isinstance(error_body, str):
|
|
description = error_body
|
|
|
|
retry_after = None
|
|
if isinstance(error_body, dict):
|
|
params = error_body.get("parameters", {})
|
|
retry_after = params.get("retry_after")
|
|
|
|
if status_code == 429:
|
|
return TelegramErrorKind.RATE_LIMITED, description, float(retry_after) if retry_after else 30.0
|
|
|
|
if status_code == 401:
|
|
return TelegramErrorKind.AUTH, description, None
|
|
|
|
if status_code == 403:
|
|
if "bot was kicked" in description.lower() or "bot is not a member" in description.lower():
|
|
return TelegramErrorKind.BOT_BLOCKED, description, None
|
|
return TelegramErrorKind.AUTH, description, None
|
|
|
|
if status_code == 400:
|
|
if "chat not found" in description.lower():
|
|
return TelegramErrorKind.CHAT_NOT_FOUND, description, None
|
|
if "can't parse entities" in description.lower() or "parse" in description.lower():
|
|
return TelegramErrorKind.PARSE_ERROR, description, None
|
|
if "file is too big" in description.lower() or "request entity too large" in description.lower():
|
|
return TelegramErrorKind.MEDIA_TOO_LARGE, description, None
|
|
return TelegramErrorKind.FATAL, description, None
|
|
|
|
if status_code and status_code >= 500:
|
|
return TelegramErrorKind.RETRYABLE, description, None
|
|
|
|
if status_code is None:
|
|
return TelegramErrorKind.NETWORK, description, None
|
|
|
|
return TelegramErrorKind.FATAL, description, None
|
|
|
|
|
|
def is_retryable(kind: TelegramErrorKind) -> bool:
|
|
return kind in (TelegramErrorKind.RETRYABLE, TelegramErrorKind.RATE_LIMITED, TelegramErrorKind.NETWORK)
|
|
|
|
|
|
MAX_RETRIES = 3
|