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

80 lines
2.6 KiB
Python
Raw Normal View History

2023-08-27 23:43:16 +03:00
from convopyro import listen_message
from pyrogram import filters
2023-08-29 17:32:37 +03:00
from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
2023-08-27 23:43:16 +03:00
2023-10-29 19:18:43 +02:00
from classes.importer.csv import ImporterCSV
from classes.importer.json import ImporterJSON
2023-08-27 23:43:16 +03:00
from classes.pyroclient import PyroClient
from modules import custom_filters
@PyroClient.on_message(
2023-10-29 19:18:43 +02:00
~filters.scheduled & filters.private & custom_filters.owner & filters.command(["import"], prefixes=["/"]) # type: ignore
2023-08-27 23:43:16 +03:00
)
async def command_import(app: PyroClient, message: Message):
2023-08-29 17:32:37 +03:00
user = await app.find_user(message.from_user)
await message.reply_text(
app._("import", "messages", locale=user.locale),
2023-10-29 19:18:43 +02:00
reply_markup=ForceReply(placeholder=""),
2023-08-29 17:32:37 +03:00
)
2023-08-27 23:43:16 +03:00
while True:
answer = await listen_message(app, message.chat.id, 300)
if answer is None or answer.text == "/cancel":
2023-08-29 17:32:37 +03:00
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
2023-08-27 23:43:16 +03:00
return
2023-10-29 19:18:43 +02:00
if answer.document is None or answer.document.mime_type not in [
"application/json",
"text/csv",
]:
2023-08-27 23:43:16 +03:00
await answer.reply_text(
2023-08-29 17:32:37 +03:00
app._("import_invalid_filetype", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
2023-08-27 23:43:16 +03:00
)
continue
break
file = await app.download_media(answer, in_memory=True)
2023-10-29 19:18:43 +02:00
data: bytes = bytes(file.getbuffer()) # type: ignore
# I'd like to replace it with switch-case, but 3.9 compatibility
# is still more important to be there. Although refactor may be
# done in the near future as Python 3.9 EOL gets nearer.
if answer.document.mime_type == "application/json":
importer = ImporterJSON()
elif answer.document.mime_type == "text/csv":
importer = ImporterCSV()
else:
await answer.reply_text(
app._("import_invalid_filetype", "messages", locale=user.locale).format(
cancel_notice=""
),
reply_markup=ReplyKeyboardRemove(),
)
return
try:
import_result = await importer.import_data(data)
except ValueError:
await answer.reply_text(
app._("import_invalid", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
2023-08-27 23:43:16 +03:00
await answer.reply_text(
2023-08-29 17:32:37 +03:00
app._("import_finished", "messages", locale=user.locale).format(
2023-10-29 19:18:43 +02:00
count=len(import_result)
2023-08-29 17:32:37 +03:00
),
reply_markup=ReplyKeyboardRemove(),
2023-08-27 23:43:16 +03:00
)