diff --git a/pomodoro_bot/bot.py b/pomodoro_bot/bot.py index f21b0bd..0da5b80 100644 --- a/pomodoro_bot/bot.py +++ b/pomodoro_bot/bot.py @@ -10,7 +10,26 @@ API_TOKEN = os.getenv("BOT_TOKEN") bot = Bot(token=API_TOKEN) dp = Dispatcher(bot) -timer_manager = None + + @dp.message_handler(commands=['start', 'help']) + async def send_welcome(message: Message): + await message.reply("Привет! Я твой личный Pomodoro-бот. Выбирай действие:", reply_markup=menu_kb) + + @dp.callback_query_handler(Text(equals=["pomodoro", "shortbreak", "longbreak", "stop"])) + async def handle_callback(call: types.CallbackQuery): + if call.data == "pomodoro": + await timer_manager.start_timer(call.from_user.id, 25*60, call.message.chat.id, 'Pomodoro') + await call.message.answer("Начался 25-минутный Pomodoro! 🔥") + elif call.data == "shortbreak": + await timer_manager.start_timer(call.from_user.id, 5*60, call.message.chat.id, 'Short Break') + await call.message.answer("Начался 5-минутный перерыв ☕") + elif call.data == "longbreak": + await timer_manager.start_timer(call.from_user.id, 15*60, call.message.chat.id, 'Long Break') + await call.message.answer("Начался длинный перерыв 😌") + elif call.data == "stop": + await timer_manager.stop_timer(call.from_user.id) + await call.message.answer("Таймер остановлен ⏹") + await call.answer() menu_kb = InlineKeyboardMarkup(row_width=2) menu_kb.add( @@ -20,32 +39,35 @@ menu_kb.add( InlineKeyboardButton("⏹ Stop", callback_data="stop") ) -@dp.message_handler(commands=['start', 'help']) -async def send_welcome(message: Message): - await message.reply("Привет! Я твой личный Pomodoro-бот. Выбирай действие:", reply_markup=menu_kb) -@dp.callback_query_handler(Text(equals=["pomodoro", "shortbreak", "longbreak", "stop"])) -async def handle_callback(call: types.CallbackQuery): - if call.data == "pomodoro": - await timer_manager.start_timer(call.from_user.id, 25*60, call.message.chat.id, 'Pomodoro') - await call.message.answer("Начался 25-минутный Pomodoro! 🔥") - elif call.data == "shortbreak": - await timer_manager.start_timer(call.from_user.id, 5*60, call.message.chat.id, 'Short Break') - await call.message.answer("Начался 5-минутный перерыв ☕") - elif call.data == "longbreak": - await timer_manager.start_timer(call.from_user.id, 15*60, call.message.chat.id, 'Long Break') - await call.message.answer("Начался длинный перерыв 😌") - elif call.data == "stop": - await timer_manager.stop_timer(call.from_user.id) - await call.message.answer("Таймер остановлен ⏹") - await call.answer() from pomodoro_bot.redis_client import init_redis async def on_startup(dispatcher): - global timer_manager await init_redis() - timer_manager = TimerManager(bot, dispatcher.loop) + dispatcher['timer_manager'] = TimerManager(bot, dispatcher.loop) + + @dispatcher.message_handler(commands=['start', 'help']) + async def send_welcome(message: Message): + await message.reply("Привет! Я твой личный Pomodoro-бот. Выбирай действие:", reply_markup=menu_kb) + + @dispatcher.callback_query_handler(Text(equals=["pomodoro", "shortbreak", "longbreak", "stop"])) + async def handle_callback(call: types.CallbackQuery): + timer_manager = dispatcher['timer_manager'] + + if call.data == "pomodoro": + await timer_manager.start_timer(call.from_user.id, 25*60, call.message.chat.id, 'Pomodoro') + await call.message.answer("Начался 25-минутный Pomodoro! 🔥") + elif call.data == "shortbreak": + await timer_manager.start_timer(call.from_user.id, 5*60, call.message.chat.id, 'Short Break') + await call.message.answer("Начался 5-минутный перерыв ☕") + elif call.data == "longbreak": + await timer_manager.start_timer(call.from_user.id, 15*60, call.message.chat.id, 'Long Break') + await call.message.answer("Начался длинный перерыв 😌") + elif call.data == "stop": + await timer_manager.stop_timer(call.from_user.id) + await call.message.answer("Таймер остановлен ⏹") + await call.answer() def run_bot(): executor.start_polling(dp, skip_updates=True, on_startup=on_startup) \ No newline at end of file diff --git a/pomodoro_bot/timer_manager.py b/pomodoro_bot/timer_manager.py index 313c446..484bd9d 100644 --- a/pomodoro_bot/timer_manager.py +++ b/pomodoro_bot/timer_manager.py @@ -29,16 +29,4 @@ class TimerManager: user.task.cancel() user.task = None user.current_timer = None - self.tasks.pop(user_id, None) - -# pomodoro_bot/redis_client.py -import redis.asyncio as redis -import os - -r = None - -async def init_redis(): - global r - r = redis.Redis.from_url( - os.getenv("REDIS_URL", "redis://localhost"), decode_responses=True - ) \ No newline at end of file + self.tasks.pop(user_id, None) \ No newline at end of file