understanding commited on
Commit
20ab9d0
·
verified ·
1 Parent(s): 7cd62b7

Update bot/__main__.py

Browse files
Files changed (1) hide show
  1. bot/__main__.py +20 -21
bot/__main__.py CHANGED
@@ -4,8 +4,8 @@ from logging import getLogger
4
  from logging.config import dictConfig
5
  from uvicorn import Config as UvicornConfig, Server as UvicornServer
6
 
7
- from bot.config import Server, LOGGER_CONFIG_JSON
8
- from bot.client import create_client
9
  from bot.startup_log import log_startup
10
  from bot.handlers import setup_handlers
11
  from bot.server import create_app
@@ -14,42 +14,37 @@ logger = getLogger("bot")
14
 
15
  def _validate_required_secrets() -> None:
16
  from bot.config import Telegram, Workers
17
-
18
  missing = []
19
  if not Telegram.API_ID: missing.append("API_ID")
20
  if not Telegram.API_HASH: missing.append("API_HASH")
21
  if not Telegram.OWNER_ID: missing.append("OWNER_ID")
22
- if not (Telegram.SESSION_STRING or Telegram.BOT_TOKEN):
23
- missing.append("SESSION_STRING (preferred) or BOT_TOKEN")
24
-
25
  if not Workers.WORKER1_URL: missing.append("WORKER1_URL")
26
  if not Workers.WORKER2_URL: missing.append("WORKER2_URL")
27
  if not Workers.BOT_BACKEND_KEY: missing.append("BOT_BACKEND_KEY")
28
  if not Workers.HF_API_KEY: missing.append("HF_API_KEY")
29
-
30
  if missing:
31
- raise RuntimeError("Missing required secrets: " + ", ".join(missing))
32
 
33
  async def main():
34
  dictConfig(LOGGER_CONFIG_JSON)
35
  _validate_required_secrets()
36
 
37
- # web server
38
  quart_app = create_app()
39
- uv_cfg = UvicornConfig(
40
- app=quart_app,
41
- host=Server.BIND_ADDRESS,
42
- port=Server.PORT,
43
- log_config=LOGGER_CONFIG_JSON,
44
- )
45
  uv_server = UvicornServer(uv_cfg)
46
 
47
- # telegram
48
- app = create_client()
49
- setup_handlers(app)
 
 
 
 
 
50
 
51
- await app.start()
52
- asyncio.create_task(log_startup(app))
53
  asyncio.create_task(uv_server.serve())
54
 
55
  logger.info("All services started ✅")
@@ -58,7 +53,11 @@ async def main():
58
  while True:
59
  await asyncio.sleep(3600)
60
  finally:
61
- await app.stop()
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  asyncio.run(main())
 
4
  from logging.config import dictConfig
5
  from uvicorn import Config as UvicornConfig, Server as UvicornServer
6
 
7
+ from bot.config import Server, LOGGER_CONFIG_JSON, Telegram
8
+ from bot.client import create_clients
9
  from bot.startup_log import log_startup
10
  from bot.handlers import setup_handlers
11
  from bot.server import create_app
 
14
 
15
  def _validate_required_secrets() -> None:
16
  from bot.config import Telegram, Workers
 
17
  missing = []
18
  if not Telegram.API_ID: missing.append("API_ID")
19
  if not Telegram.API_HASH: missing.append("API_HASH")
20
  if not Telegram.OWNER_ID: missing.append("OWNER_ID")
21
+ if not (Telegram.BOT_TOKEN or Telegram.SESSION_STRING):
22
+ missing.append("BOT_TOKEN (preferred) or SESSION_STRING (legacy)")
 
23
  if not Workers.WORKER1_URL: missing.append("WORKER1_URL")
24
  if not Workers.WORKER2_URL: missing.append("WORKER2_URL")
25
  if not Workers.BOT_BACKEND_KEY: missing.append("BOT_BACKEND_KEY")
26
  if not Workers.HF_API_KEY: missing.append("HF_API_KEY")
 
27
  if missing:
28
+ raise RuntimeError("Missing secrets: " + ", ".join(missing))
29
 
30
  async def main():
31
  dictConfig(LOGGER_CONFIG_JSON)
32
  _validate_required_secrets()
33
 
 
34
  quart_app = create_app()
35
+ uv_cfg = UvicornConfig(app=quart_app, host=Server.BIND_ADDRESS, port=Server.PORT, log_config=LOGGER_CONFIG_JSON)
 
 
 
 
 
36
  uv_server = UvicornServer(uv_cfg)
37
 
38
+ bot, user = create_clients()
39
+ user_app = user or (bot if not Telegram.BOT_TOKEN else None)
40
+
41
+ setup_handlers(bot, user_app=user_app)
42
+
43
+ await bot.start()
44
+ if user and user is not bot:
45
+ await user.start()
46
 
47
+ asyncio.create_task(log_startup(bot))
 
48
  asyncio.create_task(uv_server.serve())
49
 
50
  logger.info("All services started ✅")
 
53
  while True:
54
  await asyncio.sleep(3600)
55
  finally:
56
+ try:
57
+ if user and user is not bot:
58
+ await user.stop()
59
+ finally:
60
+ await bot.stop()
61
 
62
  if __name__ == "__main__":
63
  asyncio.run(main())