TelegramBot/plugins/commands/set_time.py

59 lines
1.8 KiB
Python

import logging
from datetime import datetime
from convopyro import listen_message
from pyrogram import filters
from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
from classes.pyroclient import PyroClient
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["set_time"], prefixes=["/"]) # type: ignore
)
async def command_set_time(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
await message.reply_text(
"Alright. Please, send your desired time in HH:MM format.",
reply_markup=ForceReply(placeholder="Time as HH:MM"),
)
while True:
answer = await listen_message(app, message.chat.id, 300)
if answer is None or answer.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
return
try:
datetime.strptime(answer.text, "%H:%M")
except ValueError:
await answer.reply_text(
"Please, provide a valid time in HH:MM format. Use /cancel if you want to cancel this operation."
)
continue
break
user_time = datetime.strptime(answer.text, "%H:%M")
await user.update_time(hour=user_time.hour, minute=user_time.minute)
logger.info(
"User %s has selected notification time of %s",
user.id,
user_time.strftime("%H:%M"),
)
notice = "" if user.enabled else "Execute /toggle to enable notifications."
garbage_time = user_time.strftime(app._("time", "formats"))
await answer.reply_text(
f"Notifications time has been updated! You will now receive notification about collection {user.offset} d. before the collection at **{garbage_time}**. {notice}",
reply_markup=ReplyKeyboardRemove(),
)