ReminderBot/bot/handlers/callback.py
leo f453a7917e Initial commit: add reminderBot project structure
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 11:40:58 +08:00

69 lines
2.4 KiB
Python
Raw 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
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
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":
# Snooze for 10 minutes - send another reminder
from bot.scheduler.executor import execute_reminder
snooze_time = datetime.now(timezone.utc) + timedelta(minutes=10)
context.job_queue.run_once(
lambda ctx: execute_reminder(reminder_id, ctx.bot),
when=snooze_time,
)
await query.edit_message_text(
f"⏰ 已延期10分钟将在 {snooze_time.strftime('%H:%M')} 再次提醒。"
)
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()