TelegramBot/plugins/commands/set_offset.py

61 lines
2.0 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_offset"], prefixes=["/"]) # type: ignore
)
async def command_set_offset(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
await message.reply_text(
"Alright. Please, send how many days in advance do you want to get a notification about the collection.",
reply_markup=ForceReply(placeholder="Number of days"),
)
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:
num = int(answer.text)
if num < 0 or num > 7:
raise ValueError(
"Offset bust not be less than 0 and greater than 7 days."
)
except (ValueError, TypeError):
await answer.reply_text(
"Please, provide a valid integer number of days in range 0 to 7 (inclusive). Use /cancel if you want to cancel this operation."
)
continue
break
offset = int(answer.text)
await user.update_offset(offset)
logger.info("User %s has set offset to %s", user.id, offset)
notice = "" if user.enabled else "Execute /toggle to enable notifications."
garbage_time = datetime(
1970, 1, 1, hour=user.time_hour, minute=user.time_minute
).strftime(app._("time", "formats"))
await answer.reply_text(
f"Notifications time has been updated! You will now receive notification about collection **{offset} d.** before the collection at {garbage_time}. {notice}",
reply_markup=ReplyKeyboardRemove(),
)