File size: 974 Bytes
a405e76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# PATH: bot/client.py
from logging import getLogger
from hydrogram import Client
from bot.config import Telegram

logger = getLogger("bot")

def create_client() -> Client:
    """
    Creates Hydrogram client in a safe way:
    - If SESSION_STRING is present -> user/bot session (no bot token needed)
    - Else BOT_TOKEN must be present
    """
    kwargs = dict(
        name="app",
        api_id=Telegram.API_ID,
        api_hash=Telegram.API_HASH,
        in_memory=True,
        sleep_threshold=-1,
        max_concurrent_transmissions=10,
    )

    if Telegram.SESSION_STRING.strip():
        kwargs["session_string"] = Telegram.SESSION_STRING.strip()
        logger.info("Using SESSION_STRING auth")
    else:
        if not Telegram.BOT_TOKEN.strip():
            raise RuntimeError("Missing BOT_TOKEN (and SESSION_STRING is empty).")
        kwargs["bot_token"] = Telegram.BOT_TOKEN.strip()
        logger.info("Using BOT_TOKEN auth")

    return Client(**kwargs)