File size: 2,115 Bytes
ed36936
 
 
 
 
 
20ab9d0
 
ed36936
278122e
ed36936
 
 
 
278122e
 
 
 
 
 
8c619c4
 
20ab9d0
8c619c4
 
278122e
 
 
 
 
20ab9d0
278122e
ed36936
 
278122e
ed36936
 
8c619c4
 
 
 
 
 
ed36936
 
20ab9d0
 
8c619c4
 
20ab9d0
 
8c619c4
20ab9d0
ed36936
20ab9d0
ed36936
 
 
 
 
 
 
 
20ab9d0
8c619c4
20ab9d0
 
 
ed36936
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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())