2022-09-16 05:48:10 +00:00
|
|
|
#!/usr/bin/python
|
2022-10-15 14:52:45 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2022-10-15 15:51:27 +00:00
|
|
|
|
2022-10-16 12:39:03 +00:00
|
|
|
import asyncio
|
2022-10-15 14:52:45 +00:00
|
|
|
from aiogram import Bot, Dispatcher
|
2022-10-16 19:57:44 +00:00
|
|
|
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
2022-10-16 12:39:03 +00:00
|
|
|
import asyncpg
|
|
|
|
|
|
|
|
from middlewares.db import DbMiddleware
|
2022-10-15 15:51:27 +00:00
|
|
|
from handlers.commands import register_command_handlers
|
2022-10-16 19:57:44 +00:00
|
|
|
from handlers.projects import register_projects_handlers
|
2022-09-16 05:48:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2022-10-15 14:52:45 +00:00
|
|
|
"""Configures and runs the bot, connects to the db"""
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2022-10-16 12:39:03 +00:00
|
|
|
DB_URL = os.getenv("DB_URL")
|
2022-10-15 14:52:45 +00:00
|
|
|
API_TOKEN = os.getenv("API_TOKEN")
|
|
|
|
|
2022-10-16 12:39:03 +00:00
|
|
|
pool = await asyncpg.create_pool(DB_URL)
|
|
|
|
|
2022-10-15 14:52:45 +00:00
|
|
|
bot = Bot(API_TOKEN)
|
2022-10-16 19:57:44 +00:00
|
|
|
dp = Dispatcher(bot, storage=MemoryStorage())
|
2022-10-15 14:52:45 +00:00
|
|
|
|
2022-10-15 15:51:27 +00:00
|
|
|
# Register handlers
|
|
|
|
register_command_handlers(dp)
|
2022-10-16 19:57:44 +00:00
|
|
|
register_projects_handlers(dp)
|
2022-10-15 15:51:27 +00:00
|
|
|
|
2022-10-16 12:39:03 +00:00
|
|
|
# Register middlewares
|
|
|
|
dp.middleware.setup(DbMiddleware(pool))
|
|
|
|
|
2022-10-15 14:52:45 +00:00
|
|
|
try:
|
|
|
|
await dp.start_polling()
|
|
|
|
finally:
|
|
|
|
await dp.storage.close()
|
2022-10-15 16:24:54 +00:00
|
|
|
bot_session = await bot.get_session()
|
|
|
|
await bot_session.close()
|
2022-09-16 05:48:10 +00:00
|
|
|
|
|
|
|
|
2022-10-17 10:58:17 +00:00
|
|
|
if __name__ == "__main__":
|
2022-09-16 05:48:10 +00:00
|
|
|
asyncio.run(main())
|