TelegramBot/plugins/commands/setup.py

90 lines
2.9 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 import custom_filters
from modules.search_name import search_name
from modules.search_nearby import search_nearby
from modules.utils import from_utc
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["setup"] + i18n.sync.in_all_locales("start_configure", "buttons"), prefixes=["/", ""]) & ~custom_filters.context # 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(
app._("search_nearby", "buttons", locale=user.locale), request_location=True
),
ReplyButton(app._("search_name", "buttons", locale=user.locale)),
)
await message.reply_text(
app._("setup", "messages", locale=user.locale),
reply_markup=keyboard_type,
)
while True:
app.contexts.append(message.from_user.id)
answer_type = await listen_message(app, message.chat.id, 300)
app.contexts.remove(message.from_user.id)
if answer_type is None or answer_type.text == "/cancel":
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_type.location is None and answer_type.text not in app.in_all_locales(
"search_name", "buttons"
):
await answer_type.reply_text(
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
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(
app._("setup_retry", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
await user.update_location(location.id)
user_time = from_utc(
datetime(1970, 1, 1, user.time_hour, user.time_minute),
None if user.location is None else user.location.timezone.zone,
).strftime(app._("time", "formats", locale=user.locale))
await message.reply_text(
app._("setup_finished", "messages", locale=user.locale).format(
name=location.name,
offset=user.offset,
time=user_time,
),
reply_markup=ReplyKeyboardRemove(),
)