From 3e1d92b48ea304908d630bcb69b5f1c6ffa53b49 Mon Sep 17 00:00:00 2001 From: Alexei Date: Tue, 25 Apr 2023 13:29:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D1=81=D1=82=D1=8B=D1=85=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9,=20?= =?UTF-8?q?=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/backup.py | 14 ++++---------- bot_modules/profile.py | 20 +++++--------------- bot_modules/start.py | 13 +++++-------- template/simple_message.py | 15 ++++++++++++--- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/bot_modules/backup.py b/bot_modules/backup.py index 05a4d08..4826461 100644 --- a/bot_modules/backup.py +++ b/bot_modules/backup.py @@ -5,7 +5,8 @@ from bot_sys import bot_bd, log, config, keyboard, user_access from bot_modules import start, access, groups -from template import file_message +from template import file_message, simple_message + from aiogram import Bot, types import sqlite3 @@ -61,14 +62,6 @@ def GetBackupKeyboardButtons(a_UserGroups): # --------------------------------------------------------- # Обработка сообщений -async def BackupOpen(a_Message): - user_id = str(a_Message.from_user.id) - user_groups= groups.GetUserGroupData(user_id) - if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): - return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetBackupKeyboardButtons(user_groups)) - - await bot.send_message(user_id, backup_message, reply_markup = GetBackupKeyboardButtons(user_groups)) - # --------------------------------------------------------- # Работа с базой данных пользователей @@ -88,6 +81,7 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(BackupOpen, text = backup_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplate(backup_message, GetBackupKeyboardButtons, GetAccess), text = backup_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/bot_modules/profile.py b/bot_modules/profile.py index 1bac0df..0eb269c 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -5,14 +5,11 @@ from bot_sys import bot_bd, log, config, keyboard, user_access from bot_modules import start, access, groups -from aiogram import Bot, types - -import sqlite3 +from template import simple_message +from aiogram import types from aiogram.dispatcher import Dispatcher -bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) - # --------------------------------------------------------- # БД module_name = 'profile' @@ -47,18 +44,12 @@ def GetStartKeyboardButtons(a_UserGroups): # --------------------------------------------------------- # Обработка сообщений -# Отображение профиля пользователя async def ProfileOpen(a_Message): - user_id = str(a_Message.from_user.id) - user_groups= groups.GetUserGroupData(user_id) - if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): - return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups)) - - user_info = GetUserInfo(user_id) + user_info = GetUserInfo(a_Message.from_user.id) msg = profile_message if not user_info is None: msg = msg.replace('@user_id', str(user_info[0])).replace('@user_name', str(user_info[1])) - await bot.send_message(user_id, msg, reply_markup = GetStartKeyboardButtons(user_groups)) + return msg # --------------------------------------------------------- # Работа с базой данных пользователей @@ -67,7 +58,6 @@ async def ProfileOpen(a_Message): def AddUser(a_UserID, a_UserName): bot_bd.SQLRequestToBD("INSERT OR IGNORE INTO users (user_id, userName) VALUES (?, ?);", commit=True, param = (a_UserID, a_UserName)) -# Добавление пользователя, если он уже есть, то игнорируем def GetUserInfo(a_UserID): user_info = bot_bd.SQLRequestToBD('SELECT * FROM users WHERE user_id = ?', param = [a_UserID]) print(user_info, str(user_info)) @@ -91,4 +81,4 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(ProfileOpen, text = user_profile_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplate(ProfileOpen, GetStartKeyboardButtons, GetAccess), text = user_profile_button_name) diff --git a/bot_modules/start.py b/bot_modules/start.py index 7f167bb..e688d52 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -5,6 +5,7 @@ from bot_sys import log, config, keyboard, user_access from bot_modules import profile, projects, groups, access, backup +from template import simple_message from aiogram.dispatcher import Dispatcher @@ -38,15 +39,11 @@ def GetStartKeyboardButtons(a_UserGroups): # Первичное привестивие async def StartMenu(a_Message): - user_id = int(a_Message.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): - return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups)) - + user_id = str(a_Message.from_user.id) user_name = str(a_Message.from_user.username) profile.AddUser(user_id, user_name) log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте') - await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(user_groups), parse_mode='HTML') + return start_message # --------------------------------------------------------- # API @@ -64,7 +61,7 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(StartMenu, commands = ['start']) - dp.register_message_handler(StartMenu, text = start_menu_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplate(StartMenu, GetStartKeyboardButtons, GetAccess), commands = ['start']) + dp.register_message_handler(simple_message.SimpleMessageTemplate(StartMenu, GetStartKeyboardButtons, GetAccess), text = start_menu_button_name) diff --git a/template/simple_message.py b/template/simple_message.py index ece6e1f..38945f9 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -8,10 +8,19 @@ from bot_modules import access, groups from aiogram import types def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): - async def Help(a_Message : types.message): + async def GetMessage(a_Message : types.message): + return a_HelpMessage + + return SimpleMessageTemplate(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode) + +def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): + async def SimpleMessage(a_Message : types.message): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) if not user_access.CheckAccessString(a_AccessFunc(), user_groups, access_mode): return await a_Message.answer(access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups)) - await a_Message.answer(a_HelpMessage, reply_markup = a_GetButtonsFunc(user_groups)) - return Help + + msg = await a_WorkFunc(a_Message) + if not msg is None: + await a_Message.answer(msg, reply_markup = a_GetButtonsFunc(user_groups)) + return SimpleMessage