Refactor session handling and configuration management
This commit is contained in:
+23
-19
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user