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:
parent
370519a812
commit
a18b5ad1ce
1 changed files with 17 additions and 6 deletions
|
|
@ -42,7 +42,7 @@ async def register_user(chat_id: str):
|
||||||
await db.commit()
|
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:
|
async with AsyncSessionLocal() as db:
|
||||||
# Получаем tasks (разовые)
|
# Получаем tasks (разовые)
|
||||||
|
|
@ -65,7 +65,18 @@ async def get_schedule_for_date(date_str: str) -> str:
|
||||||
events = result.scalars().all()
|
events = result.scalars().all()
|
||||||
events = sorted(events, key=lambda e: e.start_time)
|
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
|
# Объединяем tasks
|
||||||
all_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))
|
await register_user(str(update.effective_chat.id))
|
||||||
today = datetime.now(TZ).date()
|
today = datetime.now(TZ).date()
|
||||||
date_str = today.strftime("%Y-%m-%d")
|
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)
|
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()
|
today = datetime.now(TZ).date()
|
||||||
tomorrow = today + timedelta(days=1)
|
tomorrow = today + timedelta(days=1)
|
||||||
date_str = tomorrow.strftime("%Y-%m-%d")
|
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)
|
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":
|
if query.data == "schedule_today":
|
||||||
# Расписание на сегодня (аналог /today) - отправляем новое сообщение
|
# Расписание на сегодня (аналог /today) - отправляем новое сообщение
|
||||||
date_str = today.strftime("%Y-%m-%d")
|
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)
|
await query.message.reply_text(text)
|
||||||
|
|
||||||
elif query.data == "schedule_tomorrow":
|
elif query.data == "schedule_tomorrow":
|
||||||
# Расписание на завтра (аналог /nextday) - отправляем новое сообщение
|
# Расписание на завтра (аналог /nextday) - отправляем новое сообщение
|
||||||
tomorrow = today + timedelta(days=1)
|
tomorrow = today + timedelta(days=1)
|
||||||
date_str = tomorrow.strftime("%Y-%m-%d")
|
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)
|
await query.message.reply_text(text)
|
||||||
|
|
||||||
elif query.data == "delete_start":
|
elif query.data == "delete_start":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue