58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from aiogram import Bot, Dispatcher
|
|
from dotenv import load_dotenv
|
|
|
|
from .binding import BindingService, BotBindingRepository
|
|
from .bitrix import BitrixClient
|
|
from .config import BotConfig
|
|
from .crypto import TokenCipher
|
|
from .database import BotDatabase
|
|
from .deals import DealService
|
|
from .handlers import DealBotHandlers, StartBotHandlers
|
|
from .oauth import BotOAuthRepository
|
|
|
|
|
|
async def run() -> None:
|
|
load_dotenv()
|
|
logging.basicConfig(level=logging.INFO)
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
|
config = BotConfig.from_env()
|
|
|
|
database = BotDatabase(config.database_url)
|
|
await database.open()
|
|
|
|
bitrix = BitrixClient(
|
|
BotOAuthRepository(database),
|
|
TokenCipher(config.token_encryption_key),
|
|
config.bitrix_client_id,
|
|
config.bitrix_client_secret,
|
|
config.oauth_token_url,
|
|
)
|
|
|
|
bindings = BindingService(BotBindingRepository(database))
|
|
deals = DealService(
|
|
bitrix,
|
|
config.take_to_work_stage_id,
|
|
)
|
|
|
|
dispatcher = Dispatcher()
|
|
dispatcher.include_router(StartBotHandlers(bindings).router)
|
|
dispatcher.include_router(DealBotHandlers(deals, bindings).router)
|
|
|
|
try:
|
|
await dispatcher.start_polling(Bot(token=config.bot_token))
|
|
finally:
|
|
await bitrix.close()
|
|
await database.close()
|
|
|
|
|
|
def main() -> None:
|
|
asyncio.run(run())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|