TelegramBot/plugins/commands/checkout.py

107 lines
3.4 KiB
Python

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
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["checkout"], prefixes=["/"]) # type: ignore
)
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(
ReplyButton("Yes, I want to delete it"),
ReplyButton("No, I don't want to delete it"),
)
await message.reply_text(
"Here's pretty much all the data bot has. Please, use these buttons to choose whether you want to delete your data from the bot.",
reply_markup=keyboard_delete,
)
while True:
answer_delete = await listen_message(app, message.chat.id, 300)
if answer_delete is None or answer_delete.text == "/cancel":
await message.reply_text(
"Cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_delete.text not in [
"Yes, I want to delete it",
"No, I don't want to delete it",
]:
await answer_delete.reply_text(
"Invalid answer provided. Use /cancel if you want to cancel this operation."
)
continue
if answer_delete.text in [
"No, I don't want to delete it",
]:
await answer_delete.reply_text(
"Alright, cancelled.", reply_markup=ReplyKeyboardRemove()
)
return
break
# Confirmation
keyboard_confirm = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_confirm.add(ReplyButton("I agree and want to proceed"))
await message.reply_text(
"Alright. Please, confirm that you want to delete your data from the bot.\n\nFollowing data will be deleted:\nSelected location, preferred language of the messages, notifications time and your notifications offset.",
reply_markup=keyboard_confirm,
)
while True:
answer_confirm = await listen_message(app, message.chat.id, 300)
if answer_confirm is None or answer_confirm.text == "/cancel":
await message.reply_text(
"Cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_confirm.text not in ["I agree and want to proceed"]:
await answer_confirm.reply_text(
"Invalid answer provided. Use /cancel if you want to cancel this operation."
)
continue
break
await user.delete()
await answer_confirm.reply_text(
"Your data has been deleted. If you want to start using this bot again, please use /setup command. Otherwise delete/block the bot and do not interact with it anymore.",
reply_markup=ReplyKeyboardRemove(),
)