-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (58 loc) · 1.76 KB
/
main.py
File metadata and controls
81 lines (58 loc) · 1.76 KB
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
import logging
import asyncio
import schedule
from pyrogram import raw, __version__, idle
from tg import handlers, others
from db import repository
from data import config, clients
config.setup_logging()
_logger = logging.getLogger(__name__)
settings = config.get_settings()
def schedule_jobs():
"""
Schedule all the jobs that need to run periodically.
"""
# Schedule update users last message every hour
schedule.every(1).hour.do(
lambda: asyncio.create_task(
others.UserLastMessage.update_db_users_last_message()
)
)
async def schedule_jobs_async():
"""
Run the scheduled jobs in an asynchronous loop.
"""
schedule_jobs()
while True:
schedule.run_pending()
await asyncio.sleep(1)
async def start_telegram_bot():
logging.info(
f"The bot is up and running on Pyrogram v{__version__} (Layer {raw.all.layer})."
)
for handler in handlers.HANDLERS:
clients.bot_1.add_handler(handler)
await clients.bot_1.start()
await clients.bot_2.start()
await idle()
await clients.bot_1.stop()
await clients.bot_2.stop()
async def main():
# create admins
for admin in settings.admins:
if not (user := await repository.get_user(tg_id=admin)):
await repository.create_user(
tg_id=admin, name="admin", admin=True, language_code="he"
)
else:
if not user.admin:
await repository.update_user(tg_id=admin, admin=True)
asyncio.create_task(schedule_jobs_async())
await start_telegram_bot()
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
except KeyboardInterrupt:
pass