Enhance schedule retrieval in Telegram bot to include contextual prefixes for today and tomorrow. Updated get_schedule_for_date function to accept an add_prefix parameter, modifying the output text accordingly. Adjusted command handlers to utilize the new functionality for improved user experience.

This commit is contained in:
vrubel 2025-12-30 18:43:25 +03:00
parent 370519a812
commit a18b5ad1ce

View file

@ -42,7 +42,7 @@ async def register_user(chat_id: str):
await db.commit()
async def get_schedule_for_date(date_str: str) -> str:
async def get_schedule_for_date(date_str: str, add_prefix: bool = False) -> str:
"""Получить расписание на дату в текстовом формате"""
async with AsyncSessionLocal() as db:
# Получаем tasks (разовые)
@ -65,7 +65,18 @@ async def get_schedule_for_date(date_str: str) -> str:
events = result.scalars().all()
events = sorted(events, key=lambda e: e.start_time)
text = f"📅 {format_date_russian(date_str)}\n\n"
# Определяем префикс для даты
date_prefix = ""
if add_prefix:
today = datetime.now(TZ).date()
tomorrow = today + timedelta(days=1)
date_obj = datetime.strptime(date_str, "%Y-%m-%d").date()
if date_obj == today:
date_prefix = "Сегодня "
elif date_obj == tomorrow:
date_prefix = "Завтра "
text = f"📅 {date_prefix}{format_date_russian(date_str)}\n\n"
# Объединяем tasks
all_tasks = []
@ -128,7 +139,7 @@ async def today_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await register_user(str(update.effective_chat.id))
today = datetime.now(TZ).date()
date_str = today.strftime("%Y-%m-%d")
text = await get_schedule_for_date(date_str)
text = await get_schedule_for_date(date_str, add_prefix=True)
await update.message.reply_text(text)
@ -138,7 +149,7 @@ async def nextday_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
today = datetime.now(TZ).date()
tomorrow = today + timedelta(days=1)
date_str = tomorrow.strftime("%Y-%m-%d")
text = await get_schedule_for_date(date_str)
text = await get_schedule_for_date(date_str, add_prefix=True)
await update.message.reply_text(text)
@ -176,14 +187,14 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
if query.data == "schedule_today":
# Расписание на сегодня (аналог /today) - отправляем новое сообщение
date_str = today.strftime("%Y-%m-%d")
text = await get_schedule_for_date(date_str)
text = await get_schedule_for_date(date_str, add_prefix=True)
await query.message.reply_text(text)
elif query.data == "schedule_tomorrow":
# Расписание на завтра (аналог /nextday) - отправляем новое сообщение
tomorrow = today + timedelta(days=1)
date_str = tomorrow.strftime("%Y-%m-%d")
text = await get_schedule_for_date(date_str)
text = await get_schedule_for_date(date_str, add_prefix=True)
await query.message.reply_text(text)
elif query.data == "delete_start":