From 0aa24beefc9b1fd2ba52e9875b64d6137ac37799 Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 13 May 2023 09:26:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D1=8B=20=D1=81=D0=B0=D0=BC=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=20=D0=B8=20=D0=BD=D0=B0=D1=87=D0=B0?= =?UTF-8?q?=D0=BB=D0=BE=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=BA=D0=B8=20=D1=81=D0=B8=D0=BC=D0=BF=D0=BB?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=81=D1=81=D0=B0=D0=B4=D0=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/access.py | 6 +++--- bot_modules/backup.py | 2 +- bot_modules/comments.py | 10 +++++----- bot_modules/groups.py | 4 ++-- bot_modules/languages.py | 16 ++++++++-------- bot_modules/messages.py | 16 ++++++++-------- bot_modules/needs.py | 16 ++++++++-------- bot_modules/profile.py | 2 +- bot_modules/projects.py | 16 ++++++++-------- bot_modules/start.py | 4 ++-- bot_modules/tasks.py | 14 +++++++------- bot_sys/aiogram_bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ bot_sys/bot_bd.py | 5 ++++- bot_sys/interfaces.py | 21 +++++++++++++++++++++ bot_sys/keyboard.py | 35 ++++++++++++++++++++++++++++------- template/bd_item_add.py | 4 ++-- template/bd_item_delete.py | 2 +- template/bd_item_select.py | 4 ++-- template/simple_message.py | 6 +++--- 19 files changed, 160 insertions(+), 69 deletions(-) create mode 100644 bot_sys/aiogram_bot.py create mode 100644 bot_sys/interfaces.py diff --git a/bot_modules/access.py b/bot_modules/access.py index a03c512..51fd0e6 100644 --- a/bot_modules/access.py +++ b/bot_modules/access.py @@ -136,7 +136,7 @@ def ShowMessageTemplate(a_StringMessage): return simple_message.WorkFuncResult(msg, item_access = a_Item[1]) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -178,8 +178,8 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): defaul_keyboard_func = GetEditAccessKeyboardButtons - dp.register_message_handler(simple_message.InfoMessageTemplate(access_start_message, defaul_keyboard_func, GetAccess), text = access_button_name) - dp.register_message_handler(simple_message.InfoMessageTemplate(help_message, defaul_keyboard_func, GetAccess), text = help_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(access_start_message, defaul_keyboard_func, GetAccess), text = access_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(help_message, defaul_keyboard_func, GetAccess), text = help_button_name) sql_request.RequestToBDRegisterHandlers(dp, sql_request_button_name, request_start_message, FSMRequestToBDAccess, defaul_keyboard_func, user_access.AccessMode.ACCEES_EDIT, GetAccess) diff --git a/bot_modules/backup.py b/bot_modules/backup.py index 0fe5e61..800025d 100644 --- a/bot_modules/backup.py +++ b/bot_modules/backup.py @@ -81,7 +81,7 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(simple_message.InfoMessageTemplate(backup_message, GetBackupKeyboardButtons, GetAccess), text = backup_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(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/comments.py b/bot_modules/comments.py index 14c14cc..8b884cf 100644 --- a/bot_modules/comments.py +++ b/bot_modules/comments.py @@ -166,7 +166,7 @@ def ShowMessageTemplate(a_StringMessage): return simple_message.WorkFuncResult(msg, item_access = a_Item[2]) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -252,7 +252,7 @@ def RegisterHandlers(dp : Dispatcher): # Стартовое сообщение - dp.register_message_handler(simple_message.SimpleMessageTemplate(CommentsOpen, defaul_keyboard_func, GetAccess), text = comments_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(CommentsOpen, defaul_keyboard_func, GetAccess), text = comments_button_name) # Список комментариев a_Prefix = RegisterSelectParent(list_comment_button_name, user_access.AccessMode.VIEW) @@ -291,8 +291,8 @@ def RegisterHandlers(dp : Dispatcher): bd_item.GetCheckForPrefixFunc(a_Prefix), \ FSMCreateComment, \ AddBDItemFunc, \ - SimpleMessageTemplate(comment_create_desc_message), \ - SimpleMessageTemplate(comment_success_create_message), \ + SimpleMessageTemplateLegacy(comment_create_desc_message), \ + SimpleMessageTemplateLegacy(comment_success_create_message), \ a_Prefix,\ needs.table_name, \ needs.key_name, \ @@ -325,7 +325,7 @@ def RegisterHandlers(dp : Dispatcher): # Редактирование комментариев edit_keyboard_func = GetEditCommentKeyboardButtons - dp.register_message_handler(simple_message.InfoMessageTemplate(comment_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_comment_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(comment_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_comment_button_name) RegisterEdit(edit_comment_desc_button_name, FSMEditCommentDescItem, comment_edit_desc_message, desc_field, bd_item.FieldType.text) RegisterEdit(edit_comment_access_button_name, FSMEditCommentAccessItem, comment_edit_access_message, access_field, bd_item.FieldType.text) diff --git a/bot_modules/groups.py b/bot_modules/groups.py index 68e386e..d010d02 100644 --- a/bot_modules/groups.py +++ b/bot_modules/groups.py @@ -124,7 +124,7 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(simple_message.InfoMessageTemplate(group_start_message, GetEditGroupKeyboardButtons, GetAccess), text = user_group_button_name) - dp.register_message_handler(simple_message.InfoMessageTemplate(help_message, GetEditGroupKeyboardButtons, GetAccess), text = help_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(group_start_message, GetEditGroupKeyboardButtons, GetAccess), text = user_group_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(help_message, GetEditGroupKeyboardButtons, GetAccess), text = help_button_name) sql_request.RequestToBDRegisterHandlers(dp, sql_request_button_name, request_start_message, FSMRequestToBD, GetEditGroupKeyboardButtons, user_access.AccessMode.EDIT, GetAccess) diff --git a/bot_modules/languages.py b/bot_modules/languages.py index 250111f..a7062a3 100644 --- a/bot_modules/languages.py +++ b/bot_modules/languages.py @@ -185,7 +185,7 @@ def GetStartLanguageKeyboardButtons(a_Language, a_UserGroups): def GetViewItemInlineKeyboardTemplate(a_ItemID): def GetViewItemInlineKeyboard(a_Message, a_UserGroups): cur_buttons = [ - keyboard.InlineButton(messages.list_message_button_name, messages.select_messages_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + keyboard.InlineButtonWithAccess(messages.list_message_button_name, messages.select_messages_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), ] return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) return GetViewItemInlineKeyboard @@ -216,7 +216,7 @@ def ShowMessageTemplate(a_StringLanguage, keyboard_template_func = None): return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowLanguage -def SimpleMessageTemplate(a_StringLanguage): +def SimpleMessageTemplateLegacy(a_StringLanguage): async def ShowLanguage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringLanguage) return ShowLanguage @@ -298,7 +298,7 @@ def RegisterHandlers(dp : Dispatcher): defaul_keyboard_func = GetStartLanguageKeyboardButtons # Список языков - dp.register_message_handler(simple_message.SimpleMessageTemplate(LanguagesOpen, defaul_keyboard_func, GetAccess), text = languages_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(LanguagesOpen, defaul_keyboard_func, GetAccess), text = languages_button_name) bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, \ list_language_button_name, \ table_name, \ @@ -333,10 +333,10 @@ def RegisterHandlers(dp : Dispatcher): FSMCreateLanguage.desc, \ FSMCreateLanguage.photo,\ AddBDItemFunc, \ - SimpleMessageTemplate(language_create_name_message), \ - SimpleMessageTemplate(language_create_desc_message), \ - SimpleMessageTemplate(language_create_photo_message), \ - SimpleMessageTemplate(language_success_create_message), \ + SimpleMessageTemplateLegacy(language_create_name_message), \ + SimpleMessageTemplateLegacy(language_create_desc_message), \ + SimpleMessageTemplateLegacy(language_create_photo_message), \ + SimpleMessageTemplateLegacy(language_success_create_message), \ None,\ None, \ None, \ @@ -370,7 +370,7 @@ def RegisterHandlers(dp : Dispatcher): field_type = a_FieldType\ ) - dp.register_message_handler(simple_message.InfoMessageTemplate(language_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_language_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(language_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_language_button_name) RegisterEdit(edit_language_photo_button_name, FSMEditLanguagePhotoItem, language_edit_photo_message, photo_field, bd_item.FieldType.photo) RegisterEdit(edit_language_name_button_name, FSMEditLanguageNameItem, language_edit_name_message, name_field, bd_item.FieldType.text) RegisterEdit(edit_language_desc_button_name, FSMEditLanguageDeskItem, language_edit_desc_message, desc_field, bd_item.FieldType.text) diff --git a/bot_modules/messages.py b/bot_modules/messages.py index ccf9fad..715f94d 100644 --- a/bot_modules/messages.py +++ b/bot_modules/messages.py @@ -193,7 +193,7 @@ def GetStartMessageKeyboardButtons(a_Message, a_UserGroups): def GetViewItemInlineKeyboardTemplate(a_ItemID): def GetViewItemInlineKeyboard(a_Message, a_UserGroups): cur_buttons = [ - #keyboard.InlineButton(needs.list_need_button_name, needs.select_needs_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + #keyboard.InlineButtonWithAccess(needs.list_need_button_name, needs.select_needs_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), ] return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) return GetViewItemInlineKeyboard @@ -228,7 +228,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -314,7 +314,7 @@ def RegisterHandlers(dp : Dispatcher): ) # Стартовое сообщение - dp.register_message_handler(simple_message.SimpleMessageTemplate(MessagesOpen, defaul_keyboard_func, GetAccess), text = messages_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(MessagesOpen, defaul_keyboard_func, GetAccess), text = messages_button_name) # Список сообщений a_Prefix = RegisterSelectParent(list_message_button_name, user_access.AccessMode.VIEW) @@ -356,10 +356,10 @@ def RegisterHandlers(dp : Dispatcher): FSMCreateMessage.desc, \ FSMCreateMessage.photo,\ AddBDItemFunc, \ - SimpleMessageTemplate(message_create_name_message), \ - SimpleMessageTemplate(message_create_desc_message), \ - SimpleMessageTemplate(message_create_photo_message), \ - SimpleMessageTemplate(message_success_create_message), \ + SimpleMessageTemplateLegacy(message_create_name_message), \ + SimpleMessageTemplateLegacy(message_create_desc_message), \ + SimpleMessageTemplateLegacy(message_create_photo_message), \ + SimpleMessageTemplateLegacy(message_success_create_message), \ a_Prefix,\ languages.table_name, \ languages.key_name, \ @@ -393,7 +393,7 @@ def RegisterHandlers(dp : Dispatcher): # Редактирование сообщения edit_keyboard_func = GetEditMessageKeyboardButtons - dp.register_message_handler(simple_message.InfoMessageTemplate(message_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_message_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(message_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_message_button_name) RegisterEdit(edit_message_photo_button_name, FSMEditMessagePhotoItem, message_edit_photo_message, photo_field, bd_item.FieldType.photo) RegisterEdit(edit_message_name_button_name, FSMEditMessageNameItem, message_edit_name_message, name_field, bd_item.FieldType.text) diff --git a/bot_modules/needs.py b/bot_modules/needs.py index e1245e9..b69ad28 100644 --- a/bot_modules/needs.py +++ b/bot_modules/needs.py @@ -192,7 +192,7 @@ def GetStartNeedKeyboardButtons(a_Message, a_UserGroups): def GetViewItemInlineKeyboardTemplate(a_ItemID): def GetViewItemInlineKeyboard(a_Message, a_UserGroups): cur_buttons = [ - keyboard.InlineButton(comments.list_comment_button_name, comments.select_comments_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + keyboard.InlineButtonWithAccess(comments.list_comment_button_name, comments.select_comments_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), ] return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) return GetViewItemInlineKeyboard @@ -223,7 +223,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -299,7 +299,7 @@ def RegisterHandlers(dp : Dispatcher): # Стартовое сообщение - dp.register_message_handler(simple_message.SimpleMessageTemplate(NeedsOpen, defaul_keyboard_func, GetAccess), text = needs_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(NeedsOpen, defaul_keyboard_func, GetAccess), text = needs_button_name) # Список потребностей a_Prefix = RegisterSelectParent(list_need_button_name, user_access.AccessMode.VIEW) @@ -341,10 +341,10 @@ def RegisterHandlers(dp : Dispatcher): FSMCreateNeed.desc, \ FSMCreateNeed.photo,\ AddBDItemFunc, \ - SimpleMessageTemplate(need_create_name_message), \ - SimpleMessageTemplate(need_create_desc_message), \ - SimpleMessageTemplate(need_create_photo_message), \ - SimpleMessageTemplate(need_success_create_message), \ + SimpleMessageTemplateLegacy(need_create_name_message), \ + SimpleMessageTemplateLegacy(need_create_desc_message), \ + SimpleMessageTemplateLegacy(need_create_photo_message), \ + SimpleMessageTemplateLegacy(need_success_create_message), \ a_Prefix,\ tasks.table_name, \ tasks.key_name, \ @@ -378,7 +378,7 @@ def RegisterHandlers(dp : Dispatcher): # Редактирование потребностей edit_keyboard_func = GetEditNeedKeyboardButtons - dp.register_message_handler(simple_message.InfoMessageTemplate(need_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_need_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(need_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_need_button_name) RegisterEdit(edit_need_photo_button_name, FSMEditNeedPhotoItem, need_edit_photo_message, photo_field, bd_item.FieldType.photo) RegisterEdit(edit_need_name_button_name, FSMEditNeedNameItem, need_edit_name_message, name_field, bd_item.FieldType.text) diff --git a/bot_modules/profile.py b/bot_modules/profile.py index 0b027f2..b910900 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -125,4 +125,4 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - dp.register_message_handler(simple_message.SimpleMessageTemplate(ProfileOpen, GetStartKeyboardButtons, GetAccess), text = user_profile_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(ProfileOpen, GetStartKeyboardButtons, GetAccess), text = user_profile_button_name) diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 74c5135..799311f 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -190,7 +190,7 @@ def GetStartProjectKeyboardButtons(a_Message, a_UserGroups): def GetViewItemInlineKeyboardTemplate(a_ItemID): def GetViewItemInlineKeyboard(a_Message, a_UserGroups): cur_buttons = [ - keyboard.InlineButton(tasks.list_task_button_name, tasks.select_tasks_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), + keyboard.InlineButtonWithAccess(tasks.list_task_button_name, tasks.select_tasks_prefix, a_ItemID, GetAccess(), user_access.AccessMode.VIEW), ] return keyboard.MakeInlineKeyboard(cur_buttons, a_UserGroups) return GetViewItemInlineKeyboard @@ -222,7 +222,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -276,7 +276,7 @@ def RegisterHandlers(dp : Dispatcher): defaul_keyboard_func = GetStartProjectKeyboardButtons # Список проектов - dp.register_message_handler(simple_message.SimpleMessageTemplate(ProjectsOpen, defaul_keyboard_func, GetAccess), text = projects_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(ProjectsOpen, defaul_keyboard_func, GetAccess), text = projects_button_name) bd_item_view.FirstSelectAndShowBDItemRegisterHandlers(dp, \ list_project_button_name, \ table_name, \ @@ -311,10 +311,10 @@ def RegisterHandlers(dp : Dispatcher): FSMCreateProject.desc, \ FSMCreateProject.photo,\ AddBDItemFunc, \ - SimpleMessageTemplate(project_create_name_message), \ - SimpleMessageTemplate(project_create_desc_message), \ - SimpleMessageTemplate(project_create_photo_message), \ - SimpleMessageTemplate(project_success_create_message), \ + SimpleMessageTemplateLegacy(project_create_name_message), \ + SimpleMessageTemplateLegacy(project_create_desc_message), \ + SimpleMessageTemplateLegacy(project_create_photo_message), \ + SimpleMessageTemplateLegacy(project_success_create_message), \ None,\ None, \ None, \ @@ -348,7 +348,7 @@ def RegisterHandlers(dp : Dispatcher): field_type = a_FieldType\ ) - dp.register_message_handler(simple_message.InfoMessageTemplate(project_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(project_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_project_button_name) RegisterEdit(edit_project_photo_button_name, FSMEditProjectPhotoItem, project_edit_photo_message, photo_field, bd_item.FieldType.photo) RegisterEdit(edit_project_name_button_name, FSMEditProjectNameItem, project_edit_name_message, name_field, bd_item.FieldType.text) RegisterEdit(edit_project_desc_button_name, FSMEditProjectDeskItem, project_edit_desc_message, desc_field, bd_item.FieldType.text) diff --git a/bot_modules/start.py b/bot_modules/start.py index 52e77fe..5760b43 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -72,7 +72,7 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): - 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) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(StartMenu, GetStartKeyboardButtons, GetAccess), commands = ['start']) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(StartMenu, GetStartKeyboardButtons, GetAccess), text = start_menu_button_name) diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py index 505aca4..386f472 100644 --- a/bot_modules/tasks.py +++ b/bot_modules/tasks.py @@ -224,7 +224,7 @@ def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None): return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func) return ShowMessage -def SimpleMessageTemplate(a_StringMessage): +def SimpleMessageTemplateLegacy(a_StringMessage): async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item): return simple_message.WorkFuncResult(a_StringMessage) return ShowMessage @@ -286,7 +286,7 @@ def RegisterHandlers(dp : Dispatcher): ) # Стартовое сообщение - dp.register_message_handler(simple_message.SimpleMessageTemplate(TasksOpen, defaul_keyboard_func, GetAccess), text = tasks_button_name) + dp.register_message_handler(simple_message.SimpleMessageTemplateLegacy(TasksOpen, defaul_keyboard_func, GetAccess), text = tasks_button_name) # Список задач a_Prefix = RegisterSelectParent(list_task_button_name, user_access.AccessMode.VIEW) @@ -328,10 +328,10 @@ def RegisterHandlers(dp : Dispatcher): FSMCreateTask.desc, \ FSMCreateTask.photo,\ AddBDItemFunc, \ - SimpleMessageTemplate(task_create_name_message), \ - SimpleMessageTemplate(task_create_desc_message), \ - SimpleMessageTemplate(task_create_photo_message), \ - SimpleMessageTemplate(task_success_create_message), \ + SimpleMessageTemplateLegacy(task_create_name_message), \ + SimpleMessageTemplateLegacy(task_create_desc_message), \ + SimpleMessageTemplateLegacy(task_create_photo_message), \ + SimpleMessageTemplateLegacy(task_success_create_message), \ a_Prefix,\ projects.table_name, \ projects.key_name, \ @@ -365,7 +365,7 @@ def RegisterHandlers(dp : Dispatcher): # Редактирование задачи edit_keyboard_func = GetEditTaskKeyboardButtons - dp.register_message_handler(simple_message.InfoMessageTemplate(task_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_task_button_name) + dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(task_start_edit_message, edit_keyboard_func, GetAccess, access_mode = user_access.AccessMode.EDIT), text = edit_task_button_name) RegisterEdit(edit_task_photo_button_name, FSMEditTaskPhotoItem, task_edit_photo_message, photo_field, bd_item.FieldType.photo) RegisterEdit(edit_task_name_button_name, FSMEditTaskNameItem, task_edit_name_message, name_field, bd_item.FieldType.text) diff --git a/bot_sys/aiogram_bot.py b/bot_sys/aiogram_bot.py new file mode 100644 index 0000000..e134868 --- /dev/null +++ b/bot_sys/aiogram_bot.py @@ -0,0 +1,46 @@ +#-*-coding utf-8-*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +from bot_sys import interfaces, bot_bd, keyboard + +from aiogram import types +from aiogram import Bot +from aiogram.dispatcher import Dispatcher +from aiogram.contrib.fsm_storage.memory import MemoryStorage + +class AiogramBot(IBot): + def __init__(self, a_TelegramBotApiToken, a_BDFileName): + self.m_TelegramBotApiToken = a_TelegramBotApiToken + self.m_BDFileName = a_BDFileName + self.m_TBot = Bot(token=self.m_TelegramBotApiToken, parse_mode = types.ParseMode.HTML) + self.m_Storage = MemoryStorage() + self.m_Dispatcher = Dispatcher(self.m_TBot, storage = storage) + + def SQLRequest(self, a_Request : str, commit = False, return_error = False, param = None): + return bot_bd.SQLRequest(self.m_BDFileName, a_Request, commit = commit, return_error = return_error, param = param) + + async def SendMessage(self, a_UserID, a_Message, a_PhotoIDs, a_InlineKeyboardButtons, a_KeyboardButtons): + inline_keyboard = keyboard.MakeAiogramInlineKeyboard(a_InlineKeyboardButtons) + keyboard = keyboard.MakeAiogramKeyboard(a_KeyboardButtons) + if not keyboard: + keyboard = inline_keyboard + if a_PhotoIDs: + self.m_TBot.send_photo( + a_UserID, + a_PhotoIDs, + a_Message, + reply_markup = keyboard + ) + else: + self.m_TBot.send_message( + a_UserID, + a_Message, + reply_markup = keyboard + ) + + def RegisterMessageHandler(self, a_MessageHandler, a_CheckFunc): + self.m_Dispatcher.register_message_handler(a_MessageHandler, a_CheckFunc) + + def RegisterCallbackHandler(self, a_CallbackHandler, a_CheckFunc): + self.m_Dispatcher.register_callback_query_handler(a_CallbackHandler, a_CheckFunc) + diff --git a/bot_sys/bot_bd.py b/bot_sys/bot_bd.py index 1b59a51..368ed57 100644 --- a/bot_sys/bot_bd.py +++ b/bot_sys/bot_bd.py @@ -29,7 +29,10 @@ def SelectBDTemplate(a_TableName): return SelectBD def SQLRequestToBD(a_Request : str, commit = False, return_error = False, param = None): - db = sqlite3.connect(GetBDFileName()) + return SQLRequest(GetBDFileName(), a_Request, commit = commit, return_error = return_error, param = param) + +def SQLRequest(a_BDFileName, a_Request : str, commit = False, return_error = False, param = None): + db = sqlite3.connect(a_BDFileName) cursor = db.cursor() result = [] error = None diff --git a/bot_sys/interfaces.py b/bot_sys/interfaces.py new file mode 100644 index 0000000..e3012f7 --- /dev/null +++ b/bot_sys/interfaces.py @@ -0,0 +1,21 @@ +#-*-coding utf-8-*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +from abc import ABC, abstractmethod + +class IBot(ABC): + @abstractmethod + def SQLRequest(self, a_Request : str, commit = False, return_error = False, param = None): + pass + + @abstractmethod + async def SendMessage(self, a_UserID, a_Message, a_PhotoIDs, a_InlineKeyboardButtons, a_KeyboardButtons): + pass + + @abstractmethod + def RegisterMessageHandler(self, a_MessageHandler, a_CheckFunc): + pass + + @abstractmethod + def RegisterCallbackHandler(self, a_CallbackHandler, a_CheckFunc): + pass diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index 62e98df..c114e27 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -30,14 +30,19 @@ def Chunks(a_List, a_ChunkSize): def GetButtonInRowCount(a_AllKeyCount): return min(max(int(math.sqrt(a_AllKeyCount) // 1), 1), 4) -def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): +def MakeAiogramKeyboard(a_ButtonList : [[str]]): + return types.ReplyKeyboardMarkup(keyboard=a_ButtonList, resize_keyboard = True) + +def MakeButtons(a_ButtonList : [ButtonWithAccess], a_UserGroups): buttons = [] for b in a_ButtonList: if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): buttons += [types.KeyboardButton(b.label)] step = GetButtonInRowCount(len(buttons)) - key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True) - return key + return Chunks(buttons, step) + +def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): + return MakeAiogramKeyboard(MakeButtons(a_ButtonList, a_UserGroups)) def MakeKeyboardRemove(): return types.ReplyKeyboardRemove() @@ -47,6 +52,11 @@ def MakeKeyboardForMods(a_ModList, a_UserGroups): return MakeKeyboard(buttons, a_UserGroups) class InlineButton: + def __init__(self, a_Label, a_CallBackData): + self.label = a_Label + self.callback_data = str(a_CallBackData) + +class InlineButtonWithAccess: def __init__(self, a_Label, a_CallBackPrefix, a_CallBackData, a_AccessString, a_AccessMode): self.label = a_Label self.callback_prefix = a_CallBackPrefix @@ -54,12 +64,23 @@ class InlineButton: self.access_string = a_AccessString self.access_mode = a_AccessMode -def MakeInlineKeyboard(a_ButtonList : [InlineButton], a_UserGroups): +def MakeAiogramInlineKeyboard(a_ButtonList : [InlineButton]): + buttons = [] + for row in a_ButtonList: + r = [] + for b in row: + r += [types.InlineKeyboardButton(text = b.label, callback_data = b.callback_data)] + buttons += r + inline_keyboard = InlineKeyboardMarkup(inline_keyboard=buttons) + return inline_keyboard + +def MakeInlineKeyboardButtons(a_ButtonList : [InlineButtonWithAccess], a_UserGroups): buttons = [] for b in a_ButtonList: if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): - buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{b.callback_prefix}{b.callback_data}')] + buttons += [InlineButton(b.label, f'{b.callback_prefix}{b.callback_data}')] step = GetButtonInRowCount(len(buttons)) - inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step)) - return inline_keyboard + return Chunks(buttons, step) +def MakeInlineKeyboard(a_ButtonList : [InlineButtonWithAccess], a_UserGroups): + return MakeAiogramInlineKeyboard(MakeInlineKeyboardButtons(a_ButtonList, a_UserGroups)) diff --git a/template/bd_item_add.py b/template/bd_item_add.py index 2ea4aa5..84fe313 100644 --- a/template/bd_item_add.py +++ b/template/bd_item_add.py @@ -39,7 +39,7 @@ def StartAddBDItemTemplate(a_FSM, a_FSMStart, a_MessageFunc, a_ParentTableName, check.keyboard_func = a_FinishButtonFunc return check return res_of_work_func - return simple_message.SimpleMessageTemplate(StartAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + return simple_message.SimpleMessageTemplateLegacy(StartAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) def FinishAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD, field_type = bd_item.FieldType.text): return FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_ParentKeyFieldName, a_FieldName, a_MessageFunc, a_AccessFunc, a_ButtonFunc, a_ButtonFunc, True, access_mode = access_mode, field_type = field_type) @@ -95,7 +95,7 @@ def FinishOrNextAddBDItemTemplate(a_FSM, a_AddBDItemFunc, a_ParentTableName, a_P return simple_message.WorkFuncResult(error) return res_of_work_func - return simple_message.SimpleMessageTemplate(FinishAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + return simple_message.SimpleMessageTemplateLegacy(FinishAddBDItem, a_ButtonFunc, a_AccessFunc, access_mode) def AddBDItem3RegisterHandlers(dp, a_StartCheckFunc, a_FSM, a_FSMName, a_FSMDesc, a_FSMPhoto, a_AddBDItemFunc, a_AddNameMessageFunc, a_AddDescMessageFunc, a_AddPhotoMessageFunc, a_FinishMessageFunc, a_ParentPrefix, a_ParentTableName : str, a_ParentKeyFieldName, a_NameField, a_DescField, a_PhotoField, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.ADD): keyboard_cancel = bd_item.GetCancelKeyboardButtonsTemplate(a_AccessFunc, access_mode) diff --git a/template/bd_item_delete.py b/template/bd_item_delete.py index 033c5de..6167bea 100644 --- a/template/bd_item_delete.py +++ b/template/bd_item_delete.py @@ -29,7 +29,7 @@ def DeleteBDItemTemplate(a_TableName, a_KeyName, a_PreDeleteWorkFunc, a_PostDele return await a_PostDeleteWorkFunc(a_CallbackQuery, item_id) - return simple_message.SimpleMessageTemplate(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode) + return simple_message.SimpleMessageTemplateLegacy(DeleteBDItem, a_ButtonFunc, a_AccessFunc, access_mode) def DeleteBDItemRegisterHandlers(dp, a_PrevPrefix, a_StartCheckFunc, a_TableName : str, a_KeyName, a_ParentIDFieldName,a_PreDeleteWorkFunc, a_PostDeleteWorkFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.DELETE): reg_func = dp.register_message_handler diff --git a/template/bd_item_select.py b/template/bd_item_select.py index e5344e4..a39a00f 100644 --- a/template/bd_item_select.py +++ b/template/bd_item_select.py @@ -24,14 +24,14 @@ def GetBDItemsListKeyboardButtonsTemplate(a_TableName : str, a_ParentIDFieldName if access is None: access = '' if bname: - b = keyboard.InlineButton(bname, a_NextPrefix, key_value, access, access_mode) + b = keyboard.InlineButtonWithAccess(bname, a_NextPrefix, key_value, access, access_mode) items_button_list += [b] return keyboard.MakeInlineKeyboard(items_button_list, a_UserGroups) return GetBDItemsListKeyboardButtons def SelectDBItemTemplate(a_TableName : str, a_ParentIDFieldName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, a_PrevPrefix, a_NextPrefix, access_mode = user_access.AccessMode.VIEW): keyboard_func = GetBDItemsListKeyboardButtonsTemplate(a_TableName, a_ParentIDFieldName, a_PrevPrefix, a_NextPrefix, a_GetButtonNameAndKeyValueAndAccessFunc) - return simple_message.InfoMessageTemplate(a_StartMessage, keyboard_func, a_AccessFunc, access_mode) + return simple_message.InfoMessageTemplateLegacy(a_StartMessage, keyboard_func, a_AccessFunc, access_mode) def FirstSelectBDItemRegisterHandlers(dp, a_PrefixBase, a_ButtonName, a_TableName : str, a_KeyName, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): diff --git a/template/simple_message.py b/template/simple_message.py index b01a6b9..a400720 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -18,13 +18,13 @@ class WorkFuncResult(): self.keyboard_func = keyboard_func -def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): +def InfoMessageTemplateLegacy(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): async def GetMessage(a_Message : types.message, state = None): return WorkFuncResult(a_HelpMessage) - return SimpleMessageTemplate(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode) + return SimpleMessageTemplateLegacy(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode) -def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): +def SimpleMessageTemplateLegacy(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): async def SimpleMessage(a_Message : types.message, state = None): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id)