TelegramBot/plugins/commands/setup.py

72 lines
2.6 KiB
Python
Raw Normal View History

2023-08-27 23:43:16 +03:00
import logging
2023-08-28 16:41:20 +03:00
from datetime import datetime
2023-08-27 23:43:16 +03:00
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
2023-08-28 16:41:20 +03:00
from modules.search_name import search_name
from modules.search_nearby import search_nearby
2023-08-27 23:43:16 +03:00
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)
2023-08-28 16:41:20 +03:00
keyboard_type = ReplyKeyboard(resize_keyboard=True, row_width=1)
keyboard_type.add(
ReplyButton("Search nearby locations", request_location=True),
ReplyButton("Search by location name"),
2023-08-27 23:43:16 +03:00
)
2023-08-28 16:41:20 +03:00
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,
)
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
while True:
answer_type = await listen_message(app, message.chat.id, 300)
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
if answer_type is None or answer_type.text == "/cancel":
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
return
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
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
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
break
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
location = (
await search_name(app, answer_type)
if answer_type.location is None
else await search_nearby(app, answer_type)
)
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
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
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
await user.update_location(location.id)
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
user_time = datetime(1970, 1, 1, user.time_hour, user.time_minute)
2023-08-27 23:43:16 +03:00
2023-08-28 16:41:20 +03:00
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(),
)