Refactor session handling and configuration management

This commit is contained in:
vvzvlad
2025-02-10 05:09:29 +03:00
parent a5bcd12c95
commit 60b253c1c1
5 changed files with 97 additions and 98 deletions
+23 -19
View File
@@ -1,4 +1,5 @@
import logging
import os
from pyrogram import Client
from config import get_settings
@@ -7,27 +8,30 @@ settings = get_settings()
class TelegramClient:
def __init__(self):
if settings["session_string"]:
self.client = Client(
name="pyro_bridge",
session_string=settings["session_string"],
api_id=settings["tg_api_id"],
api_hash=settings["tg_api_hash"],
in_memory=True
)
else:
self.client = Client(
name="pyro_bridge",
api_id=settings["tg_api_id"],
api_hash=settings["tg_api_hash"],
workdir=settings["session_path"],
in_memory=True
)
self._ensure_session_directory()
self.client = Client(
name="pyro_bridge",
api_id=settings["tg_api_id"],
api_hash=settings["tg_api_hash"],
workdir=settings["session_path"],
)
def _ensure_session_directory(self):
try:
os.makedirs(settings["session_path"], exist_ok=True)
logger.debug(f'Session directory created/verified: {settings["session_path"]}')
except Exception as e:
logger.error(f'Failed to create session directory {settings["session_path"]}: {str(e)}')
raise
async def start(self):
if not self.client.is_connected:
await self.client.start()
logger.info("Telegram client connected")
try:
if not self.client.is_connected:
await self.client.start()
logger.info("Telegram client connected successfully")
except Exception as e:
logger.error(f"Failed to start Telegram client: {str(e)}")
raise
async def stop(self):
if self.client.is_connected: