ForcePilot/backend/package/yuxi/channels/adapters/telegram/dual_plugin.py
Kris 71fec609bb feat(telegram-adapter): 实现完整的Telegram适配器基础代码
新增了Telegram适配器的全套基础模块,包括:
1.  核心适配器入口与会话工具
2.  账号管理、认证与配置系统
3.  连接相关的轮询、Webhook、更新偏移管理
4.  话题路由、管理与缓存系统
5.  消息反抖动、超时配置与工具类
6.  响应式UI与命令交互系统
7.  反应表情与通知系统
8.  审批与安全审计模块
9.  健康检查与状态监控
10. 贴纸缓存与视觉工具
11. 流式响应与协作功能
12. 群组迁移与目标归一化处理
2026-05-12 00:49:52 +08:00

106 lines
3.1 KiB
Python

from __future__ import annotations
from typing import Any
from yuxi.utils.logging_config import logger
class TelegramAccountPlugin:
def __init__(self, config: dict[str, Any] | None = None):
self._config = config or {}
self._bot_id: str | None = None
self._bot_username: str | None = None
self._connected = False
self._connection_error: str | None = None
@property
def bot_id(self) -> str | None:
return self._bot_id
@property
def bot_username(self) -> str | None:
return self._bot_username
@property
def connected(self) -> bool:
return self._connected
@property
def connection_error(self) -> str | None:
return self._connection_error
async def login(self, bot) -> dict[str, Any] | None:
try:
me = await bot.get_me()
self._bot_id = str(me.id)
self._bot_username = me.username or ""
self._connected = True
self._connection_error = None
profile = {
"id": self._bot_id,
"username": self._bot_username,
"first_name": me.first_name,
"can_join_groups": getattr(me, "can_join_groups", None),
"can_read_all_group_messages": getattr(me, "can_read_all_group_messages", None),
"supports_inline_queries": getattr(me, "supports_inline_queries", None),
}
logger.info(f"[Telegram] Account logged in: @{self._bot_username} ({self._bot_id})")
return profile
except Exception as e:
self._connection_error = str(e)
logger.error(f"[Telegram] Account login failed: {e}")
return None
def configure(self, config: dict[str, Any]) -> None:
self._config = config
def get_account_info(self) -> dict[str, Any]:
return {
"bot_id": self._bot_id,
"bot_username": self._bot_username,
"connected": self._connected,
"error": self._connection_error,
}
class ReplyLoopPlugin:
def __init__(self, config: dict[str, Any] | None = None):
self._config = config or {}
self._running = False
self._processed_count = 0
self._error_count = 0
@property
def running(self) -> bool:
return self._running
@property
def processed_count(self) -> int:
return self._processed_count
@property
def error_count(self) -> int:
return self._error_count
def start(self) -> None:
self._running = True
logger.info("[Telegram] Reply loop started")
def stop(self) -> None:
self._running = False
logger.info(f"[Telegram] Reply loop stopped (processed={self._processed_count}, errors={self._error_count})")
def increment_processed(self) -> None:
self._processed_count += 1
def increment_error(self) -> None:
self._error_count += 1
def get_loop_info(self) -> dict[str, Any]:
return {
"running": self._running,
"processed_count": self._processed_count,
"error_count": self._error_count,
}