TelegramBot/plugins/commands/setup.py

72 lines
2.6 KiB
Python

import logging
from datetime import datetime
from convopyro import listen_message
from libbot import i18n
from pykeyboard import ReplyButton, ReplyKeyboard
from pyrogram import filters
from pyrogram.types import Message, ReplyKeyboardRemove
from classes.pyroclient import PyroClient
from modules.search_name import search_name
from modules.search_nearby import search_nearby
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["setup"] + i18n.sync.in_all_locales("configure", "buttons"), prefixes=["/", ""]) # type: ignore
)
async def command_setup(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
keyboard_type = ReplyKeyboard(resize_keyboard=True, row_width=1)
keyboard_type.add(
ReplyButton("Search nearby locations", request_location=True),
ReplyButton("Search by location name"),
)
await message.reply_text(
"Let's begin configuration with the search for your location. Please, select whether you want to search among the locations near you or go straight to the search by location name.\n\nNote that the location you send to the bot will **NOT** be saved anywhere and is only used for location lookup in the database.",
reply_markup=keyboard_type,
)
while True:
answer_type = await listen_message(app, message.chat.id, 300)
if answer_type is None or answer_type.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
return
if answer_type.location is None and answer_type.text not in [
"Search by location name",
]:
await answer_type.reply_text(
"Please, select a valid option using keyboard provided. Use /cancel if you want to cancel this operation."
)
continue
break
location = (
await search_name(app, answer_type)
if answer_type.location is None
else await search_nearby(app, answer_type)
)
if location is None:
await answer_type.reply_text(
"If you want try selecting the location again, use the /setup command.",
reply_markup=ReplyKeyboardRemove(),
)
return
await user.update_location(location.id)
user_time = datetime(1970, 1, 1, user.time_hour, user.time_minute)
await message.reply_text(
f"You will now receive the notifications for **{location.name}** at {user_time.strftime(app._('time', 'formats', locale=user.locale))}, {user.offset} d. before collection.",
reply_markup=ReplyKeyboardRemove(),
)