import aiogram from aiogram.filters import Command from birthday_pool_bot.repositories import RepositoriesContainer from . import constants, handlers from .callback_data import ( ConfirmAnswerEnum, MenuCallbackData, NewSubscriptionCallbackData, NewSubscriptionConfirmCallbackData, NotifyNewPoolCallbackData, PoolActionEnum, PoolCallbackData, PoolsBackCallbackData, PoolsCallbackData, SubscribeToNewPoolAnswerEnum, SubscriptionActionEnum, SubscriptionCallbackData, SubscriptionsCallbackData, ) from .middlewares import ( AuthMiddleware, DependsMiddleware, ExceptionHandlerMiddleware, # TypingMiddleware, ) from .states import ( MenuState, NewSubscriptionPoolState, NewSubscriptionState, SetProfileBirthdayState, SetProfileGiftPaymentDataState, SetProfileNameState, SetProfilePhoneState, ) def setup_dispatcher( dispatcher: aiogram.Dispatcher, repositories_container: RepositoriesContainer, ): exception_handler_middleware = ExceptionHandlerMiddleware() # typing_middleware = TypingMiddleware() auth_middleware = AuthMiddleware(users_repository=repositories_container.users) users_repository_middleware = DependsMiddleware( name="users_repository", object_=repositories_container.users, ) pools_repository_middleware = DependsMiddleware( name="pools_repository", object_=repositories_container.pools, ) subscriptions_repository_middleware = DependsMiddleware( name="subscriptions_repository", object_=repositories_container.subscriptions, ) middlewares = [ exception_handler_middleware, # typing_middleware, auth_middleware, users_repository_middleware, pools_repository_middleware, subscriptions_repository_middleware, ] router = aiogram.Router() for middleware in middlewares: dispatcher.update.outer_middleware(middleware) router.callback_query.middleware(middleware) router.message.middleware(middleware) router = setup_router(router=router) dispatcher.include_router(router) def setup_router(router: aiogram.Router) -> aiogram.Router: # Menu router.message.register(handlers.menu_message_handler, Command("start")) router.message.register(handlers.menu_message_handler, Command("menu")) router.message.register( handlers.profile_message_handler, MenuState.MENU, aiogram.F.text == constants.PROFILE_BUTTON, ) router.message.register( handlers.subscriptions_message_handler, MenuState.MENU, aiogram.F.text == constants.SUBSCRIPTIONS_BUTTON, ) ## Profile router.message.register( handlers.ask_profile_name_message_handler, MenuState.PROFILE, aiogram.F.text == constants.SET_PROFILE_NAME, ) router.message.register( handlers.ask_profile_phone_message_handler, MenuState.PROFILE, aiogram.F.text == constants.SET_PROFILE_PHONE, ) router.message.register( handlers.ask_profile_birthdate_message_handler, MenuState.PROFILE, aiogram.F.text == constants.SET_PROFILE_BIRTHDATE_BUTTON, ) router.message.register( handlers.ask_profile_gift_payment_phone_message_handler, MenuState.PROFILE, aiogram.F.text == constants.SET_PROFILE_GIFT_PAYMENT_DATA_BUTTON, ) router.message.register( handlers.menu_message_handler, MenuState.PROFILE, aiogram.F.text == constants.BACK_BUTTON, ) ### Set profile name router.message.register( handlers.profile_message_handler, SetProfileNameState.WAITING_FOR_NAME, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_profile_name_message_handler, SetProfileNameState.WAITING_FOR_NAME, ) ### Set profile phone router.message.register( handlers.profile_message_handler, SetProfilePhoneState.WAITING_FOR_PHONE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_profile_phone_message_handler, SetProfilePhoneState.WAITING_FOR_PHONE, ) ### Set profile birthday router.message.register( handlers.profile_message_handler, SetProfileBirthdayState.WAITING_FOR_DATE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_profile_birthdate_message_handler, SetProfileBirthdayState.WAITING_FOR_DATE, ) ### Set profile gift payment data router.message.register( handlers.profile_message_handler, SetProfileGiftPaymentDataState.WAITING_FOR_PHONE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_profile_gift_payment_phone_message_handler, SetProfileGiftPaymentDataState.WAITING_FOR_PHONE, ) router.message.register( handlers.ask_profile_gift_payment_phone_message_handler, SetProfileGiftPaymentDataState.WAITING_FOR_BANK, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_profile_gift_payment_bank_message_handler, SetProfileGiftPaymentDataState.WAITING_FOR_BANK, aiogram.F.text.in_(constants.BANKS_MAP), ) ## Subscriptions router.callback_query.register( handlers.subscription_callback_handler, MenuState.SUBSCRIPTIONS, SubscriptionCallbackData.filter( aiogram.F.action == SubscriptionActionEnum.SHOW, ), ) router.callback_query.register( handlers.subscriptions_callback_handler, MenuState.SUBSCRIPTIONS, SubscriptionsCallbackData.filter(), ) router.callback_query.register( handlers.new_subscription_callback_handler, MenuState.SUBSCRIPTIONS, NewSubscriptionCallbackData.filter(), ) router.callback_query.register( handlers.menu_callback_handler, MenuState.SUBSCRIPTIONS, MenuCallbackData.filter(), ) ### Subscription interaction router.callback_query.register( handlers.delete_subscription_callback, MenuState.SUBSCRIPTIONS, SubscriptionCallbackData.filter( aiogram.F.action == SubscriptionActionEnum.DELETE, ), ) router.callback_query.register( handlers.subscriptions_callback_handler, MenuState.SUBSCRIPTIONS, SubscriptionCallbackData.filter( aiogram.F.action == SubscriptionActionEnum.BACK, ), ) ### Add new subscription router.message.register( handlers.new_subscription_message_handler, NewSubscriptionState.WAITING_FOR_PHONE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_user_message_handler, NewSubscriptionState.WAITING_FOR_PHONE, ) router.message.register( handlers.ask_new_subscription_user_message_handler, NewSubscriptionState.WAITING_FOR_DATE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_user_birthday_message_handler, NewSubscriptionState.WAITING_FOR_DATE, ) router.message.register( handlers.ask_new_subscription_user_message_handler, NewSubscriptionState.WAITING_FOR_NAME, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_name_message_handler, NewSubscriptionState.WAITING_FOR_NAME, ) router.message.register( handlers.ask_new_subscription_pool_description_message_handler, NewSubscriptionState.WAITING_FOR_POOL_DECISION, aiogram.F.text == constants.CREATE_POOL_BUTTON, ) router.message.register( handlers.show_new_subscription_choosing_pools_message_handler, NewSubscriptionState.WAITING_FOR_POOL_DECISION, aiogram.F.text == constants.JOIN_EXISTING_POOL_BUTTON, ) router.message.register( handlers.ask_new_subscription_confirmation_message_handler, NewSubscriptionState.WAITING_FOR_POOL_DECISION, aiogram.F.text == constants.DECLINE_POOL_BUTTON, ) router.message.register( handlers.ask_new_subscription_name_message_handler, NewSubscriptionState.WAITING_FOR_POOL_DECISION, aiogram.F.text == constants.BACK_BUTTON, ) router.callback_query.register( handlers.confirm_new_subscription_callback_handler, NewSubscriptionState.WAITING_FOR_CONFIRMATION, NewSubscriptionConfirmCallbackData.filter( aiogram.F.answer == ConfirmAnswerEnum.YES, ), ) router.callback_query.register( handlers.subscriptions_callback_handler, NewSubscriptionState.WAITING_FOR_CONFIRMATION, NewSubscriptionConfirmCallbackData.filter( aiogram.F.answer == ConfirmAnswerEnum.NO, ), ) #### Add new subscription pool router.message.register( handlers.ask_new_subscription_pool_payment_phone_message_handler, NewSubscriptionPoolState.WAITING_FOR_DESCRIPTION, aiogram.F.text == constants.SKIP_BUTTON, ) router.message.register( handlers.ask_new_subscription_pool_decision_message_handler, NewSubscriptionPoolState.WAITING_FOR_DESCRIPTION, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_pool_description_message_handler, NewSubscriptionPoolState.WAITING_FOR_DESCRIPTION, ) router.message.register( handlers.set_new_subscription_pool_payment_data_from_profile_message_handler, NewSubscriptionPoolState.WAITING_FOR_PAYMENT_PHONE, aiogram.F.text == constants.USE_PROFILE_GIFT_PAYMENT_DATA, ) router.message.register( handlers.ask_new_subscription_pool_description_message_handler, NewSubscriptionPoolState.WAITING_FOR_PAYMENT_PHONE, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_pool_payment_phone_message_handler, NewSubscriptionPoolState.WAITING_FOR_PAYMENT_PHONE, ) router.message.register( handlers.set_new_subscription_pool_payment_phone_message_handler, NewSubscriptionPoolState.WAITING_FOR_PAYMENT_BANK, aiogram.F.text == constants.BACK_BUTTON, ) router.message.register( handlers.set_new_subscription_pool_payment_bank_message_handler, NewSubscriptionPoolState.WAITING_FOR_PAYMENT_BANK, ) #### Choose pool for subscription router.callback_query.register( handlers.show_new_subscription_choosing_pools_callback_handler, NewSubscriptionState.WAITING_FOR_CHOOSE_POOL, PoolsCallbackData.filter(), ) router.callback_query.register( handlers.ask_new_subscription_pool_decision_callback_handler, NewSubscriptionState.WAITING_FOR_CHOOSE_POOL, PoolsBackCallbackData.filter(), ) router.callback_query.register( handlers.show_new_subscription_choosing_pool_callback_handler, NewSubscriptionState.WAITING_FOR_CHOOSE_POOL, PoolCallbackData.filter(aiogram.F.action == PoolActionEnum.SHOW), ) router.callback_query.register( handlers.choose_new_subscription_pool_callback_handler, NewSubscriptionState.WAITING_FOR_CHOOSE_POOL, PoolCallbackData.filter(aiogram.F.action == PoolActionEnum.CHOOSE), ) router.callback_query.register( handlers.show_new_subscription_choosing_pools_callback_handler, NewSubscriptionState.WAITING_FOR_CHOOSE_POOL, PoolsCallbackData.filter(), ) # Notification about new pool router.callback_query.register( handlers.subscribe_to_new_pool_callback_handler, NotifyNewPoolCallbackData.filter(aiogram.F.answer == SubscribeToNewPoolAnswerEnum.YES), ) router.callback_query.register( handlers.hide_new_pool_notification, NotifyNewPoolCallbackData.filter(aiogram.F.answer == SubscribeToNewPoolAnswerEnum.NO), ) # Fallback router.message.register(handlers.fallback_message_handler) return router