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/checkout.py

116 lines
3.7 KiB
Python
Raw Permalink Normal View History

2023-08-27 23:43:16 +03:00
import logging
from io import BytesIO
from convopyro import listen_message
from pykeyboard import ReplyButton, ReplyKeyboard
from pyrogram import filters
from pyrogram.types import Message, ReplyKeyboardRemove
from ujson import dumps
from classes.pyroclient import PyroClient
2023-08-30 15:41:34 +03:00
from modules import custom_filters
2023-08-27 23:43:16 +03:00
logger = logging.getLogger(__name__)
@PyroClient.on_message(
2023-08-30 15:41:34 +03:00
~filters.scheduled & filters.private & filters.command(["checkout"], prefixes=["/"]) & ~custom_filters.context # type: ignore
2023-08-27 23:43:16 +03:00
)
async def command_checkout(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
user_data = BytesIO(
dumps(await user.checkout(), escape_forward_slashes=False).encode()
)
await message.reply_document(
user_data,
file_name="user_data.json",
)
# Data deletion request
keyboard_delete = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_delete.add(
2023-08-29 17:32:37 +03:00
ReplyButton(app._("delete_yes", "buttons", locale=user.locale)),
ReplyButton(app._("delete_no", "buttons", locale=user.locale)),
2023-08-27 23:43:16 +03:00
)
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("checkout", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=keyboard_delete,
)
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_delete = 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_delete is None or answer_delete.text == "/cancel":
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("cancelled", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)
return
2023-08-29 17:32:37 +03:00
if answer_delete.text not in app._(
"delete_yes", "buttons", locale=user.locale
) + app._("delete_no", "buttons", locale=user.locale):
2023-08-27 23:43:16 +03:00
await answer_delete.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_delete.text in app._("delete_no", "buttons", locale=user.locale):
2023-08-27 23:43:16 +03:00
await answer_delete.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
break
# Confirmation
keyboard_confirm = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
2023-08-29 17:32:37 +03:00
keyboard_confirm.add(
ReplyButton(app._("delete_confirm", "buttons", locale=user.locale))
)
2023-08-27 23:43:16 +03:00
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("checkout_deletion", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=keyboard_confirm,
)
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_confirm = 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_confirm is None or answer_confirm.text == "/cancel":
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("cancelled", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)
return
2023-08-29 17:32:37 +03:00
if answer_confirm.text not in app.in_all_locales("delete_confirm", "buttons"):
2023-08-27 23:43:16 +03:00
await answer_confirm.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
break
await user.delete()
await answer_confirm.reply_text(
2023-08-29 17:32:37 +03:00
app._("checkout_deleted", "messages", locale=user.locale),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)