106 lines
3.1 KiB
Python
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,
|
||
|
|
}
|