Skip to content

Commit

Permalink
Put bot logic in one class (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Masynchin authored Jul 15, 2023
1 parent b129169 commit 6eea645
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
11 changes: 11 additions & 0 deletions app/bot/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ class ErrorRoute(Route):

def register(self, router):
router.errors(self.filter)(self.handler)


class Routes(Route):
"""Мультихендлер"""

def __init__(self, *routes):
self.routes = routes

def register(self, router):
for route in self.routes:
route.register(router)
31 changes: 30 additions & 1 deletion app/bot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from aiogram.filters.logic import and_f as And
from aiogram.fsm.state import State, StatesGroup

from app.bot.ext import CallbackRoute, ErrorRoute, MessageRoute
from app.bot.ext import CallbackRoute, ErrorRoute, MessageRoute, Routes
from app import keyboards
from app import mailing
from app import stickers
Expand All @@ -19,6 +19,35 @@
from app.logger import logger


# ВСЁ ВМЕСТЕ


class Logic(Routes):
"""Вся логика бота"""

def __init__(self, db, weather):
super().__init__(
Welcome(),
Info(),
CurrentWeather(weather),
HourForecast(weather),
ExactHourOptions(),
ExactHourForecast(weather),
DailyForecast(weather),
ExactDayOptions(),
ExactDayForecast(weather),
MailingInfo(db),
SubscribeToMailing(),
SetMailingHour(),
SetMailingMinute(db),
ChangeMailingTime(),
ChangeMailingHour(),
ChangeMailingMinute(db),
CancelMailing(db),
Errors(),
)


# START И ПОМОЩЬ


Expand Down
45 changes: 3 additions & 42 deletions app/bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,7 @@
import aiohttp

from app import config
from app.bot.handlers import (
CancelMailing,
ChangeMailingHour,
ChangeMailingMinute,
ChangeMailingTime,
CurrentWeather,
DailyForecast,
Errors,
ExactDayForecast,
ExactDayOptions,
ExactHourForecast,
ExactHourOptions,
HourForecast,
Info,
MailingInfo,
SetMailingHour,
SetMailingMinute,
SubscribeToMailing,
Welcome,
)
from app.bot.handlers import Logic
from app.bot.polling import Polling
from app.bot.webhook import Webhook
from app.bot.task import MailingTask
Expand All @@ -50,28 +31,8 @@ async def main():
db = Subscribers(db_session)
weather = OwmWeather.for_che(config.WEATHER_API_KEY, client_session)
task = MailingTask.default(db, weather)
routes = [
Welcome(),
Info(),
CurrentWeather(weather),
HourForecast(weather),
ExactHourOptions(),
ExactHourForecast(weather),
DailyForecast(weather),
ExactDayOptions(),
ExactDayForecast(weather),
MailingInfo(db),
SubscribeToMailing(),
SetMailingHour(),
SetMailingMinute(db),
ChangeMailingTime(),
ChangeMailingHour(),
ChangeMailingMinute(db),
CancelMailing(db),
Errors(),
]
for route in routes:
route.register(dp)
logic = Logic(db, weather)
logic.register(dp)

if config.RUN_TYPE == "polling":
await Polling(dp, tasks=[task]).run(bot)
Expand Down

0 comments on commit 6eea645

Please sign in to comment.