Spaces:
Runtime error
Runtime error
Rename bot/config.py to bot/client.py
Browse files- bot/client.py +32 -0
- bot/config.py +0 -48
bot/client.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PATH: bot/client.py
|
| 2 |
+
from logging import getLogger
|
| 3 |
+
from hydrogram import Client
|
| 4 |
+
from bot.config import Telegram
|
| 5 |
+
|
| 6 |
+
logger = getLogger("bot")
|
| 7 |
+
|
| 8 |
+
def create_client() -> Client:
|
| 9 |
+
"""
|
| 10 |
+
Creates Hydrogram client in a safe way:
|
| 11 |
+
- If SESSION_STRING is present -> user/bot session (no bot token needed)
|
| 12 |
+
- Else BOT_TOKEN must be present
|
| 13 |
+
"""
|
| 14 |
+
kwargs = dict(
|
| 15 |
+
name="app",
|
| 16 |
+
api_id=Telegram.API_ID,
|
| 17 |
+
api_hash=Telegram.API_HASH,
|
| 18 |
+
in_memory=True,
|
| 19 |
+
sleep_threshold=-1,
|
| 20 |
+
max_concurrent_transmissions=10,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if Telegram.SESSION_STRING.strip():
|
| 24 |
+
kwargs["session_string"] = Telegram.SESSION_STRING.strip()
|
| 25 |
+
logger.info("Using SESSION_STRING auth")
|
| 26 |
+
else:
|
| 27 |
+
if not Telegram.BOT_TOKEN.strip():
|
| 28 |
+
raise RuntimeError("Missing BOT_TOKEN (and SESSION_STRING is empty).")
|
| 29 |
+
kwargs["bot_token"] = Telegram.BOT_TOKEN.strip()
|
| 30 |
+
logger.info("Using BOT_TOKEN auth")
|
| 31 |
+
|
| 32 |
+
return Client(**kwargs)
|
bot/config.py
DELETED
|
@@ -1,48 +0,0 @@
|
|
| 1 |
-
# PATH: bot/config.py
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
def _get_int(name: str, default: int) -> int:
|
| 5 |
-
try:
|
| 6 |
-
return int(os.environ.get(name, str(default)))
|
| 7 |
-
except Exception:
|
| 8 |
-
return default
|
| 9 |
-
|
| 10 |
-
def _get_str(name: str, default: str = "") -> str:
|
| 11 |
-
return str(os.environ.get(name, default))
|
| 12 |
-
|
| 13 |
-
class Telegram:
|
| 14 |
-
# REQUIRED
|
| 15 |
-
API_ID: int = _get_int("TELEGRAM_API_ID", 12345)
|
| 16 |
-
API_HASH: str = _get_str("TELEGRAM_API_HASH", "YOUR_API_HASH")
|
| 17 |
-
|
| 18 |
-
# Use either SESSION_STRING (preferred on HF if bot token blocked) OR BOT_TOKEN
|
| 19 |
-
SESSION_STRING: str = _get_str("SESSION_STRING", "")
|
| 20 |
-
BOT_TOKEN: str = _get_str("BOT_TOKEN", "")
|
| 21 |
-
|
| 22 |
-
OWNER_ID: int = _get_int("OWNER_ID", 0) # put your tg id
|
| 23 |
-
BOT_USERNAME: str = _get_str("TELEGRAM_BOT_USERNAME", "BotFather")
|
| 24 |
-
|
| 25 |
-
class Server:
|
| 26 |
-
BIND_ADDRESS: str = _get_str("BIND_ADDRESS", "0.0.0.0")
|
| 27 |
-
PORT: int = _get_int("PORT", 7860)
|
| 28 |
-
BASE_URL: str = _get_str("BASE_URL", "http://127.0.0.1:7860")
|
| 29 |
-
|
| 30 |
-
LOGGER_CONFIG_JSON = {
|
| 31 |
-
"version": 1,
|
| 32 |
-
"disable_existing_loggers": False,
|
| 33 |
-
"formatters": {
|
| 34 |
-
"default": {
|
| 35 |
-
"format": "[%(asctime)s][%(name)s][%(levelname)s] -> %(message)s",
|
| 36 |
-
"datefmt": "%d/%m/%Y %H:%M:%S",
|
| 37 |
-
}
|
| 38 |
-
},
|
| 39 |
-
"handlers": {
|
| 40 |
-
"stream": {"class": "logging.StreamHandler", "formatter": "default"}
|
| 41 |
-
},
|
| 42 |
-
"loggers": {
|
| 43 |
-
"uvicorn": {"level": "INFO", "handlers": ["stream"]},
|
| 44 |
-
"uvicorn.error": {"level": "INFO", "handlers": ["stream"]},
|
| 45 |
-
"bot": {"level": "INFO", "handlers": ["stream"]},
|
| 46 |
-
"hydrogram": {"level": "INFO", "handlers": ["stream"]},
|
| 47 |
-
},
|
| 48 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|