This repository has been archived on 2022-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
it-volunteers-for-peace/bot.py

50 lines
1.2 KiB
Python

#!/usr/bin/python
import logging
import os
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from asyncpg import create_pool
from middlewares.db import DbMiddleware
from handlers.commands import register_command_handlers
from handlers.projects import register_projects_handlers
async def main():
"""Configures and runs the bot, connects to the db"""
logging.basicConfig(level=logging.INFO)
API_TOKEN = os.getenv("API_TOKEN")
DB_HOST = os.getenv("DB_HOST")
DB_USER = os.getenv("DB_USER")
DB_PASS = os.getenv("DB_PASS")
DB_NAME = os.getenv("DB_NAME")
pool = await create_pool(
f"postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}/{DB_NAME}"
)
bot = Bot(API_TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())
# Register handlers
register_command_handlers(dp)
register_projects_handlers(dp)
# Register middlewares
dp.middleware.setup(DbMiddleware(pool))
try:
await dp.start_polling()
finally:
await dp.storage.close()
bot_session = await bot.get_session()
await bot_session.close()
if __name__ == "__main__":
asyncio.run(main())