This repository has been archived on 2024-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramBot/plugins/commands/start.py

121 lines
4.0 KiB
Python
Raw Permalink Normal View History

2023-08-27 23:43:16 +03:00
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
2023-08-30 15:41:34 +03:00
from modules import custom_filters
2023-09-25 00:47:09 +03:00
from modules.utils import from_utc
2023-08-27 23:43:16 +03:00
@PyroClient.on_message(
2023-08-30 15:41:34 +03:00
~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) & ~custom_filters.context # type: ignore
2023-08-27 23:43:16 +03:00
)
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).format(
terms_of_service="https://garbagebot.eu/community/public/terms-of-service",
privacy_policy="https://garbagebot.eu/community/public/privacy-policy",
)
)
2023-08-27 23:43:16 +03:00
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(
2023-08-29 17:32:37 +03:00
app._("start_code_invalid", "messages", locale=user.locale)
2023-08-27 23:43:16 +03:00
)
return
keyboard = ReplyKeyboardMarkup(
[
2023-08-29 17:32:37 +03:00
[
KeyboardButton(
app._("start_code_yes", "buttons", locale=user.locale)
)
],
[KeyboardButton(app._("start_code_no", "buttons", locale=user.locale))],
2023-08-27 23:43:16 +03:00
],
resize_keyboard=True,
one_time_keyboard=True,
)
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("start_code", "messages", locale=user.locale).format(
name=location.name
),
2023-08-27 23:43:16 +03:00
reply_markup=keyboard,
)
while True:
2023-08-30 15:41:34 +03:00
app.contexts.append(message.from_user.id)
2023-08-27 23:43:16 +03:00
answer = await listen_message(app, message.chat.id, 300)
2023-08-30 15:41:34 +03:00
app.contexts.remove(message.from_user.id)
2023-08-27 23:43:16 +03:00
if answer is None or answer.text == "/cancel":
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
2023-08-27 23:43:16 +03:00
)
return
2023-08-29 17:32:37 +03:00
if answer.text not in app.in_all_locales(
"start_code_yes", "buttons"
) + app.in_all_locales("start_code_no", "buttons"):
2023-08-27 23:43:16 +03:00
await answer.reply_text(
2023-08-29 17:32:37 +03:00
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
2023-08-27 23:43:16 +03:00
)
continue
2023-08-29 17:32:37 +03:00
if answer.text in app.in_all_locales("start_code_no", "buttons"):
2023-08-27 23:43:16 +03:00
await answer.reply_text(
2023-08-29 17:32:37 +03:00
app._("start_selection_no", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)
return
break
await user.update_location(location.id)
2023-09-25 00:47:09 +03:00
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))
2023-08-27 23:43:16 +03:00
await answer.reply_text(
2023-08-29 17:32:37 +03:00
app._("start_selection_yes", "messages", locale=user.locale).format(
name=location.name, offset=user.offset, time=user_time
),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)
return
if user.location is None:
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("start_configure", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardMarkup(
2023-08-29 17:32:37 +03:00
[
[
KeyboardButton(
app._("start_configure", "buttons", locale=user.locale)
)
]
],
2023-08-27 23:43:16 +03:00
resize_keyboard=True,
one_time_keyboard=True,
),
)