ReminderBot/bot/handlers/callback.py
leo 6901b0dd7b fix: fix interval reminder delete bug and simplify notification buttons
Remove pause/delete buttons from reminder notification popup (manage
via "我的提醒" list instead). Reorder delete operation to remove
scheduler job before DB record, preventing orphaned list entries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 17:14:15 +08:00

80 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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":
# Just acknowledge, don't stop the reminder
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
remove_reminder_job(reminder_id)
session.delete(reminder)
session.commit()
await query.edit_message_text(f"🗑 已删除提醒:{title}")
except Exception:
session.rollback()
await query.edit_message_text("操作失败,请稍后重试。")
finally:
Session.remove()