This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
from aiogram import Bot, Dispatcher, types
|
||||
from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from aiogram.utils import executor
|
||||
from aiogram.dispatcher.filters import Text
|
||||
from pomodoro_bot.user_manager import user_manager
|
||||
from pomodoro_bot.timer_manager import TimerManager
|
||||
import os
|
||||
|
||||
API_TOKEN = os.getenv("BOT_TOKEN")
|
||||
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
timer_manager = TimerManager(bot)
|
||||
|
||||
menu_kb = InlineKeyboardMarkup(row_width=2)
|
||||
menu_kb.add(
|
||||
InlineKeyboardButton("▶️ Pomodoro", callback_data="pomodoro"),
|
||||
InlineKeyboardButton("☕ Short Break", callback_data="shortbreak"),
|
||||
InlineKeyboardButton("😌 Long Break", callback_data="longbreak"),
|
||||
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()
|
||||
|
||||
def run_bot():
|
||||
executor.start_polling(dp, skip_updates=True)
|
||||
@@ -1,7 +0,0 @@
|
||||
import asyncio
|
||||
from pomodoro_bot.bot import run_bot
|
||||
from .redis_client import init_redis
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(init_redis())
|
||||
run_bot()
|
||||
@@ -1,6 +0,0 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class UserState:
|
||||
current_timer: str = None
|
||||
task: any = None
|
||||
@@ -1,10 +0,0 @@
|
||||
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
|
||||
)
|
||||
@@ -1,31 +0,0 @@
|
||||
import asyncio
|
||||
from .user_manager import user_manager
|
||||
from .models import UserState
|
||||
|
||||
class TimerManager:
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.tasks = {}
|
||||
|
||||
async def start_timer(self, user_id, duration, chat_id, label):
|
||||
await self.stop_timer(user_id)
|
||||
|
||||
async def timer():
|
||||
await asyncio.sleep(duration)
|
||||
if label == 'Pomodoro':
|
||||
await user_manager.increment_pomodoros(user_id)
|
||||
await self.bot.send_message(chat_id, f"⏰ {label} завершён!")
|
||||
|
||||
task = asyncio.create_task(timer())
|
||||
user = user_manager.get_user(user_id)
|
||||
user.current_timer = label
|
||||
user.task = task
|
||||
self.tasks[user_id] = task
|
||||
|
||||
async def stop_timer(self, user_id):
|
||||
user = user_manager.get_user(user_id)
|
||||
if user.task:
|
||||
user.task.cancel()
|
||||
user.task = None
|
||||
user.current_timer = None
|
||||
self.tasks.pop(user_id, None)
|
||||
@@ -1,20 +0,0 @@
|
||||
from .models import UserState
|
||||
from .redis_client import r as redis
|
||||
|
||||
class UserManager:
|
||||
def __init__(self):
|
||||
self.users = {}
|
||||
|
||||
def get_user(self, user_id):
|
||||
if user_id not in self.users:
|
||||
self.users[user_id] = UserState()
|
||||
return self.users[user_id]
|
||||
|
||||
async def increment_pomodoros(self, user_id):
|
||||
await redis.incr(f"user:{user_id}:pomodoros")
|
||||
|
||||
async def get_pomodoros(self, user_id):
|
||||
val = await redis.get(f"user:{user_id}:pomodoros")
|
||||
return int(val) if val else 0
|
||||
|
||||
user_manager = UserManager()
|
||||
Reference in New Issue
Block a user