diff --git a/bot_modules/backup.py b/bot_modules/backup.py index ebefc7f..f49eade 100644 --- a/bot_modules/backup.py +++ b/bot_modules/backup.py @@ -5,6 +5,7 @@ from bot_sys import bot_bd, log, config, keyboard, user_access from bot_modules import start, access, groups +from template import file_message from aiogram import Bot, types import sqlite3 @@ -68,32 +69,9 @@ async def BackupOpen(a_Message): await bot.send_message(user_id, backup_message, reply_markup = GetBackupKeyboardButtons(user_groups)) -def BackupFileTemplate(a_Path, a_CaptionMessage, a_AccessFunc, a_ButtonFunc, a_ErrorMessage): - async def BackupFile(a_Message): - user_id = str(a_Message.from_user.id) - user_groups= groups.GetUserGroupData(user_id) - if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.EDIT): - return await bot.send_message(user_id, access.access_denied_message, reply_markup = a_ButtonFunc(user_groups)) - - document = await GetFile(a_Path) - if document is None: - return await bot.send_message(user_id, error_backup_message, reply_markup = a_ButtonFunc(user_groups)) - - await bot.send_document(user_id, document, caption = a_CaptionMessage.replace('@time', log.GetTime()), reply_markup = a_ButtonFunc(user_groups)) - return BackupFile - # --------------------------------------------------------- # Работа с базой данных пользователей -async def GetFile(a_Path): - try: - document = open(a_Path, 'rb') - log.Success(f'Загружен файл {a_Path}') - return document - except Exception as e: - log.Error(f'Не удалось загрузить файл {a_Path}. Ошибка {str(e)}') - return None - # --------------------------------------------------------- # API @@ -111,5 +89,5 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): dp.register_message_handler(BackupOpen, text = backup_button_name) - dp.register_message_handler(BackupFileTemplate(bot_bd.GetBDFileName(), backup_bd_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_bd_button_name) - dp.register_message_handler(BackupFileTemplate(log.g_log_file_name, backup_log_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_log_button_name) + dp.register_message_handler(file_message.BackupFileTemplate(bot_bd.GetBDFileName(), backup_bd_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_bd_button_name) + dp.register_message_handler(file_message.BackupFileTemplate(log.g_log_file_name, backup_log_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_log_button_name) diff --git a/template/file_message.py b/template/file_message.py new file mode 100644 index 0000000..2f10b0f --- /dev/null +++ b/template/file_message.py @@ -0,0 +1,37 @@ +# -*- coding: utf8 -*- +# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) + +# Сообщения для работы с файлами + +from bot_sys import log, config, user_access +from bot_modules import access, groups +from aiogram import Bot, types + +import sqlite3 + +from aiogram.dispatcher import Dispatcher + +bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) + +def BackupFileTemplate(a_Path, a_CaptionMessage, a_AccessFunc, a_ButtonFunc, a_ErrorMessage): + async def BackupFile(a_Message): + user_id = str(a_Message.from_user.id) + user_groups= groups.GetUserGroupData(user_id) + if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.EDIT): + return await a_Message.answer(access.access_denied_message, reply_markup = a_ButtonFunc(user_groups)) + + document = await GetFile(a_Path) + if document is None: + return await a_Message.answer(user_id, error_backup_message, reply_markup = a_ButtonFunc(user_groups)) + + await bot.send_document(user_id, document, caption = a_CaptionMessage.replace('@time', log.GetTime()), reply_markup = a_ButtonFunc(user_groups)) + return BackupFile + +async def GetFile(a_Path): + try: + document = open(a_Path, 'rb') + log.Success(f'Загружен файл {a_Path}') + return document + except Exception as e: + log.Error(f'Не удалось загрузить файл {a_Path}. Ошибка {str(e)}') + return None