from datetime import datetime, timedelta, timezone import pytz from telegram import Update from telegram.ext import ContextTypes from bot.models.database import Session from bot.models.reminder import Reminder from bot.scheduler.job_manager import add_reminder_job, remove_reminder_job async def handle_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: query = update.callback_query await query.answer() data = query.data # Support compound actions like "cancel_snooze_123" if data.startswith("cancel_snooze_"): action = "cancel_snooze" reminder_id = int(data[len("cancel_snooze_"):]) else: action, reminder_id_str = data.split("_", 1) reminder_id = int(reminder_id_str) session = Session() try: reminder = session.get(Reminder, reminder_id) if reminder is None: await query.edit_message_text("提醒不存在或已被删除。") return if action == "done": reminder.is_active = False session.commit() remove_reminder_job(reminder_id) await query.edit_message_text(f"✅ 已完成提醒:{reminder.title}") elif action == "snooze": # Ask user for snooze minutes context.user_data["snooze_reminder_id"] = reminder_id context.user_data["snooze_chat_id"] = query.message.chat_id await query.edit_message_text( "⏰ 请输入延期的分钟数(例如:10):" ) elif action == "cancel_snooze": job_name = f"snooze_{reminder_id}" jobs = context.job_queue.get_jobs_by_name(job_name) if jobs: for job in jobs: job.schedule_removal() await query.edit_message_text(f"❌ 已关闭延期提醒:{reminder.title}") else: await query.edit_message_text("延期提醒不存在或已过期。") elif action == "pause": reminder.is_active = False session.commit() remove_reminder_job(reminder_id) await query.edit_message_text(f"⏸ 已暂停提醒:{reminder.title}") elif action == "resume": reminder.is_active = True session.commit() add_reminder_job(reminder_id) await query.edit_message_text(f"▶️ 已恢复提醒:{reminder.title}") elif action == "delete": title = reminder.title session.delete(reminder) session.commit() remove_reminder_job(reminder_id) await query.edit_message_text(f"🗑 已删除提醒:{title}") except Exception: session.rollback() await query.edit_message_text("操作失败,请稍后重试。") finally: Session.remove()