Skip to content

Commit d70f8d2

Browse files
committed
feat: Add MTProto proxy configuration to settings and TelegramService
1 parent 1f7eef4 commit d70f8d2

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@ REDIS_URL=redis://localhost:6379/0
1414
MONGODB_URL=mongodb://localhost:27017
1515
MONGODB_DATABASE=telegram_service
1616

17+
# MTProto proxy (optional)
18+
USE_MTPROTO_PROXY=false
19+
MTPROTO_HOST=
20+
MTPROTO_PORT=
21+
MTPROTO_SECRET=
22+
1723
# Security
1824
API_SECRET_KEY=your-secret-key-for-api-auth

app/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Settings(BaseSettings):
2323
# MongoDB
2424
mongodb_url: str = "mongodb://localhost:27017"
2525
mongodb_database: str = "telegram_service"
26+
27+
# MTProto proxy (optional)
28+
use_mtproto_proxy: bool = False
29+
mtproto_host: Optional[str] = None
30+
mtproto_port: Optional[int] = None
31+
mtproto_secret: Optional[str] = None
2632

2733
# Security
2834
api_secret_key: str

app/services/telegram.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from telethon import TelegramClient, events
99
from telethon.errors import FloodWaitError, PasswordHashInvalidError, PhoneCodeInvalidError, SessionPasswordNeededError
10+
from telethon.network.connection import ConnectionTcpMTProxyRandomizedIntermediate
1011
from telethon.tl.types import User
1112

1213
from app.config import settings
@@ -22,6 +23,13 @@ class TelegramService:
2223
def __init__(self):
2324
self.clients: Dict[str, TelegramClient] = {}
2425
self._ensure_session_directory()
26+
self._proxy = self._build_proxy()
27+
28+
def _build_proxy(self):
29+
"""Return proxy config tuple for MTProto if enabled."""
30+
if settings.use_mtproto_proxy and settings.mtproto_host and settings.mtproto_port and settings.mtproto_secret:
31+
return (settings.mtproto_host, int(settings.mtproto_port), settings.mtproto_secret)
32+
return None
2533

2634
def _ensure_session_directory(self):
2735
"""Ensure the session directory exists."""
@@ -34,7 +42,19 @@ def _get_session_file_path(self, session_id: str) -> str:
3442
async def create_client(self, session_id: str) -> TelegramClient:
3543
"""Create a new Telegram client for a session."""
3644
session_file = self._get_session_file_path(session_id)
37-
client = TelegramClient(session_file, settings.telegram_api_id, settings.telegram_api_hash)
45+
client_kwargs = {}
46+
if self._proxy:
47+
client_kwargs.update(
48+
connection=ConnectionTcpMTProxyRandomizedIntermediate,
49+
proxy=self._proxy,
50+
)
51+
52+
client = TelegramClient(
53+
session_file,
54+
settings.telegram_api_id,
55+
settings.telegram_api_hash,
56+
**client_kwargs,
57+
)
3858
self.clients[session_id] = client
3959
logger.info(f"Created Telegram client for session: {session_id}")
4060
return client

0 commit comments

Comments
 (0)