This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (79 loc) · 3.01 KB
/
main.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import asyncio
from os import getenv
import aiohttp
import zmq
import zmq.asyncio as azmq
from loguru import logger
from src.bot import bot, dp
from src.config import DEBUG, PROFILE
from src.handlers.administration_handlers import register_administration_handlers
from src.handlers.debug_handlers import register_debug_handlers
from src.handlers.error_handlers import register_error_handlers
from src.handlers.find_handlers import register_find_handlers
from src.handlers.parent_handlers import register_parent_handlers
from src.handlers.registration_handlers import register_registration_handlers
from src.handlers.student_handers import register_student_handlers
from src.handlers.teacher_handers import register_teacher_handlers
from src.handlers.universal_handler import register_universal_handlers
async def web():
app = aiohttp.web.Application()
app.add_routes(
[
aiohttp.web.get("/", lambda req: aiohttp.web.Response(text="Healthy")),
]
)
runner = aiohttp.web.AppRunner(app)
await runner.setup()
await aiohttp.web.TCPSite(runner).start()
await asyncio.Event().wait()
async def run():
await register_error_handlers()
await register_parent_handlers()
await register_student_handlers()
await register_teacher_handlers()
await register_administration_handlers()
await register_find_handlers()
await register_registration_handlers()
await register_universal_handlers()
if PROFILE == DEBUG:
await register_debug_handlers()
logger.debug("Registered handlers")
# await aiogram.executor.start_polling(dp, skip_updates=True)
await dp.start_polling()
context = azmq.Context()
socket = context.socket(zmq.SUB)
async def zmq(socket):
port = getenv("ZMQ_PORT")
host = getenv("ZMQ_HOST")
socket.connect(f"tcp://{host}:{port}")
socket.subscribe("")
while True:
json = await socket.recv_json()
logger.debug(f"ZMQ receive json {json}")
text = json.get("text")
if text is None or not isinstance(text, str) or text.strip() == "":
continue
ids = json.get("telegram_ids", [])
args = json.get("args", {})
for id in ids:
logger.info(f"{id} | None | None | send_announcement | None | None")
try:
await bot.send_message(
chat_id=id, text=text, parse_mode="markdown", **args
)
except Exception as e:
logger.warning(f"{id} | None | None | user_block_bot | None | None")
await asyncio.sleep(0.05)
async def main():
bot_service = main_loop.create_task(run())
zmq_service = main_loop.create_task(zmq(socket))
web_service = main_loop.create_task(web())
await asyncio.wait([web_service, bot_service, zmq_service])
logger.debug("#" * 50)
logger.debug("BOT STARTED")
logger.debug("#" * 50)
if __name__ == "__main__":
# TODO: load env variables for zmq
main_loop = asyncio.get_event_loop()
main_loop.run_until_complete(main())
main_loop.close()