新增了Telegram适配器的全套基础模块,包括: 1. 核心适配器入口与会话工具 2. 账号管理、认证与配置系统 3. 连接相关的轮询、Webhook、更新偏移管理 4. 话题路由、管理与缓存系统 5. 消息反抖动、超时配置与工具类 6. 响应式UI与命令交互系统 7. 反应表情与通知系统 8. 审批与安全审计模块 9. 健康检查与状态监控 10. 贴纸缓存与视觉工具 11. 流式响应与协作功能 12. 群组迁移与目标归一化处理
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from .token import resolve_bot_token
|
|
|
|
|
|
class MultiAccountManager:
|
|
def __init__(self, configs: dict[str, dict[str, Any]] | None = None):
|
|
self._accounts: dict[str, dict[str, Any]] = configs or {}
|
|
self._active_accounts: dict[str, Any] = {}
|
|
|
|
def add_account(self, account_id: str, config: dict[str, Any]) -> None:
|
|
self._accounts[account_id] = config
|
|
|
|
def remove_account(self, account_id: str) -> bool:
|
|
if account_id in self._accounts:
|
|
del self._accounts[account_id]
|
|
self._active_accounts.pop(account_id, None)
|
|
return True
|
|
return False
|
|
|
|
def get_account_config(self, account_id: str) -> dict[str, Any] | None:
|
|
return self._accounts.get(account_id)
|
|
|
|
def list_accounts(self) -> list[dict[str, Any]]:
|
|
return [
|
|
{
|
|
"account_id": aid,
|
|
"active": aid in self._active_accounts,
|
|
"config": cfg,
|
|
}
|
|
for aid, cfg in self._accounts.items()
|
|
]
|
|
|
|
def list_enabled_accounts(self) -> list[dict[str, Any]]:
|
|
return [
|
|
{
|
|
"account_id": aid,
|
|
"active": aid in self._active_accounts,
|
|
"config": cfg,
|
|
}
|
|
for aid, cfg in self._accounts.items()
|
|
if cfg.get("enabled", True)
|
|
]
|
|
|
|
def resolve_default_account(self) -> dict[str, Any] | None:
|
|
default_id = None
|
|
for aid, cfg in self._accounts.items():
|
|
if cfg.get("default", False):
|
|
default_id = aid
|
|
break
|
|
if default_id is None and self._accounts:
|
|
default_id = next(iter(self._accounts))
|
|
if default_id:
|
|
return {
|
|
"account_id": default_id,
|
|
"active": default_id in self._active_accounts,
|
|
"config": self._accounts[default_id],
|
|
}
|
|
return None
|
|
|
|
def find_token_owner_account_id(self, token: str) -> str | None:
|
|
for aid, cfg in self._accounts.items():
|
|
account_token = resolve_bot_token(cfg, aid)
|
|
if account_token and account_token == token:
|
|
return aid
|
|
return None
|
|
|
|
def detect_token_conflicts(self) -> list[dict[str, Any]]:
|
|
token_map: dict[str, list[str]] = {}
|
|
for aid, cfg in self._accounts.items():
|
|
token = resolve_bot_token(cfg, aid)
|
|
if token:
|
|
token_map.setdefault(token, []).append(aid)
|
|
|
|
conflicts = []
|
|
for token, account_ids in token_map.items():
|
|
if len(account_ids) > 1:
|
|
conflicts.append(
|
|
{
|
|
"token_hash": token[:12] + "...",
|
|
"account_ids": account_ids,
|
|
}
|
|
)
|
|
return conflicts
|
|
|
|
def set_active(self, account_id: str, adapter: Any) -> None:
|
|
self._active_accounts[account_id] = adapter
|
|
|
|
def get_active(self, account_id: str) -> Any | None:
|
|
return self._active_accounts.get(account_id)
|
|
|
|
def get_all_active(self) -> dict[str, Any]:
|
|
return dict(self._active_accounts)
|
|
|
|
def deactivate(self, account_id: str) -> None:
|
|
self._active_accounts.pop(account_id, None)
|