新增了Telegram适配器的全套基础模块,包括: 1. 核心适配器入口与会话工具 2. 账号管理、认证与配置系统 3. 连接相关的轮询、Webhook、更新偏移管理 4. 话题路由、管理与缓存系统 5. 消息反抖动、超时配置与工具类 6. 响应式UI与命令交互系统 7. 反应表情与通知系统 8. 审批与安全审计模块 9. 健康检查与状态监控 10. 贴纸缓存与视觉工具 11. 流式响应与协作功能 12. 群组迁移与目标归一化处理
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
class SetupContract:
|
|
EMPTY_GIF_CONTENT = b"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
|
|
RESPONSE_TIMEOUT_S = 3.0
|
|
PIPE_TERMINATE_ID = "telegram:pipe:terminate"
|
|
ACK_TIMEOUT_S = 1.0
|
|
|
|
def __init__(self, config: dict[str, Any] | None = None):
|
|
cfg = config or {}
|
|
self._bot_display_name: str | None = None
|
|
self._bot_username: str | None = None
|
|
self._setup_completed = False
|
|
|
|
async def initialize_setup(self, bot) -> bool:
|
|
try:
|
|
me = await bot.get_me()
|
|
self._bot_username = me.username or ""
|
|
self._bot_display_name = me.first_name or self._bot_username
|
|
logger.info(f"[Telegram] Setup initialized for @{self._bot_username}")
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"[Telegram] Setup initialization failed: {e}")
|
|
return False
|
|
|
|
@property
|
|
def bot_display_name(self) -> str:
|
|
return self._bot_display_name or "Telegram Bot"
|
|
|
|
@property
|
|
def bot_username(self) -> str | None:
|
|
return self._bot_username
|
|
|
|
@property
|
|
def setup_completed(self) -> bool:
|
|
return self._setup_completed
|
|
|
|
def mark_setup_completed(self) -> None:
|
|
self._setup_completed = True
|
|
|
|
def get_setup_status(self) -> dict[str, Any]:
|
|
return {
|
|
"initialized": self._setup_completed,
|
|
"bot_display_name": self._bot_display_name,
|
|
"bot_username": self._bot_username,
|
|
"response_timeout_s": self.RESPONSE_TIMEOUT_S,
|
|
"ack_timeout_s": self.ACK_TIMEOUT_S,
|
|
}
|
|
|
|
@staticmethod
|
|
def create_empty_gif() -> bytes:
|
|
import base64
|
|
|
|
return base64.b64decode(SetupContract.EMPTY_GIF_CONTENT)
|