YouTubeLoader / bot /__main__.py
understanding's picture
Update bot/__main__.py
8c619c4 verified
# PATH: bot/__main__.py
import asyncio
from logging import getLogger
from logging.config import dictConfig
from uvicorn import Config as UvicornConfig, Server as UvicornServer
from bot.config import Server, LOGGER_CONFIG_JSON, Telegram
from bot.client import create_clients
from bot.startup_log import log_startup
from bot.handlers import setup_handlers
from bot.server import create_app
logger = getLogger("bot")
def _validate_required_secrets() -> None:
from bot.config import Telegram, Workers
missing = []
if not Telegram.API_ID: missing.append("API_ID")
if not Telegram.API_HASH: missing.append("API_HASH")
if not Telegram.OWNER_ID: missing.append("OWNER_ID")
# βœ… Need BOT_TOKEN or SESSION_STRING (bot session)
if not (Telegram.BOT_TOKEN or Telegram.SESSION_STRING):
missing.append("BOT_TOKEN or SESSION_STRING (bot session)")
if not Workers.WORKER1_URL: missing.append("WORKER1_URL")
if not Workers.WORKER2_URL: missing.append("WORKER2_URL")
if not Workers.BOT_BACKEND_KEY: missing.append("BOT_BACKEND_KEY")
if not Workers.HF_API_KEY: missing.append("HF_API_KEY")
if missing:
raise RuntimeError("Missing secrets: " + ", ".join(missing))
async def main():
dictConfig(LOGGER_CONFIG_JSON)
_validate_required_secrets()
quart_app = create_app()
uv_cfg = UvicornConfig(
app=quart_app,
host=Server.BIND_ADDRESS,
port=Server.PORT,
log_config=LOGGER_CONFIG_JSON
)
uv_server = UvicornServer(uv_cfg)
bot, user = create_clients()
# βœ… IMPORTANT: only user session can fetch restricted channel messages
setup_handlers(bot, user_app=user)
await bot.start()
if user:
await user.start()
asyncio.create_task(log_startup(bot))
asyncio.create_task(uv_server.serve())
logger.info("All services started βœ…")
try:
while True:
await asyncio.sleep(3600)
finally:
try:
if user:
await user.stop()
finally:
await bot.stop()
if __name__ == "__main__":
asyncio.run(main())