import logging from datetime import datetime import pytz from convopyro import listen_message from pyrogram import filters from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient from modules import custom_filters logger = logging.getLogger(__name__) @PyroClient.on_message( ~filters.scheduled & filters.private & filters.command(["set_time"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_set_time(app: PyroClient, message: Message): user = await app.find_user(message.from_user) if user.location is None: await message.reply_text( app._("location_empty", "messages", locale=user.locale) ) return await message.reply_text( app._("set_time", "messages", locale=user.locale), reply_markup=ForceReply( placeholder=app._("set_time", "force_replies", locale=user.locale) ), ) while True: app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( app._("cancelled", "messages", locale=user.locale), reply_markup=ReplyKeyboardRemove(), ) return try: datetime.strptime(answer.text, "%H:%M") except ValueError: await answer.reply_text( app._("set_time_invalid", "messages", locale=user.locale).format( cancel_notice=app._("cancel", "messages", locale=user.locale) ) ) continue break # Time we got from the user parsed_time = datetime.strptime(answer.text, "%H:%M") # Datetime user means in their timezone user_time = datetime.now(user.location.timezone).replace( hour=parsed_time.hour, minute=parsed_time.minute, second=0, microsecond=0 ) # Datetime in user's timezone moved to UTC timezone utc_time = user_time.astimezone(pytz.utc) await user.update_time(hour=utc_time.hour, minute=utc_time.minute) logger.info( "User %s has selected notification time of %s (%s UTC)", user.id, user_time.strftime("%H:%M"), utc_time.strftime("%H:%M"), ) await answer.reply_text( app._("set_time_finished", "messages", locale=user.locale).format( offset=user.offset, time=user_time.strftime(app._("time", "formats", locale=user.locale)), toggle_notice=( "" if user.enabled else app._("toggle", "messages", locale=user.locale) ), ), reply_markup=ReplyKeyboardRemove(), )