TelegramBot/plugins/commands/start.py

116 lines
3.9 KiB
Python

from datetime import datetime
from convopyro import listen_message
from pyrogram import filters
from pyrogram.types import (
KeyboardButton,
Message,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
)
from classes.pyroclient import PyroClient
from modules import custom_filters
from modules.utils import from_utc
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) & ~custom_filters.context # type: ignore
)
async def command_start(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
await message.reply_text(app._("start", "messages", locale=user.locale))
join_code = None if len(message.command) == 1 else message.command[1]
if join_code is not None:
try:
location = await app.get_location(int(join_code))
except ValueError:
await message.reply_text(
app._("start_code_invalid", "messages", locale=user.locale)
)
return
keyboard = ReplyKeyboardMarkup(
[
[
KeyboardButton(
app._("start_code_yes", "buttons", locale=user.locale)
)
],
[KeyboardButton(app._("start_code_no", "buttons", locale=user.locale))],
],
resize_keyboard=True,
one_time_keyboard=True,
)
await message.reply_text(
app._("start_code", "messages", locale=user.locale).format(
name=location.name
),
reply_markup=keyboard,
)
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
if answer.text not in app.in_all_locales(
"start_code_yes", "buttons"
) + app.in_all_locales("start_code_no", "buttons"):
await answer.reply_text(
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
if answer.text in app.in_all_locales("start_code_no", "buttons"):
await answer.reply_text(
app._("start_selection_no", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
break
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 answer.reply_text(
app._("start_selection_yes", "messages", locale=user.locale).format(
name=location.name, offset=user.offset, time=user_time
),
reply_markup=ReplyKeyboardRemove(),
)
return
if user.location is None:
await message.reply_text(
app._("start_configure", "messages", locale=user.locale),
reply_markup=ReplyKeyboardMarkup(
[
[
KeyboardButton(
app._("start_configure", "buttons", locale=user.locale)
)
]
],
resize_keyboard=True,
one_time_keyboard=True,
),
)