Browse Source

Названия кнопок теперь можно редактировать из самого бота

pull/2/head
Alexei 2 years ago
parent
commit
c6ceb4433a
  1. 200
      bot_modules/buttons.py
  2. 185
      bot_modules/languages.py
  3. 237
      bot_modules/messages.py
  4. 4
      bot_modules/start.py
  5. 27
      main.py

200
bot_modules/buttons.py

@ -0,0 +1,200 @@
# -*- coding: utf8 -*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Сообщения
from bot_sys import bot_bd, keyboard, user_access, user_messages, bd_table, bot_messages
from bot_modules import mod_table_operate, mod_simple_message, access_utils
from template import bd_item
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
class FSMCreateButton(StatesGroup):
name = State()
desc = State()
photo = State()
class FSMEditButtonPhotoItem(StatesGroup):
item_id = State()
item_field = State()
class FSMEditButtonNameItem(StatesGroup):
item_id = State()
item_field = State()
class FSMEditButtonDescItem(StatesGroup):
item_id = State()
item_field = State()
class FSMEditButtonAccessItem(StatesGroup):
item_id = State()
item_field = State()
# ---------------------------------------------------------
# БД
module_name = 'buttons'
table_name = module_name
key_name = 'buttonID'
name_field = 'buttonName'
desc_field = 'buttonDesc'
photo_field = 'buttonPhoto'
access_field = 'buttonAccess'
create_datetime_field = 'buttonCreateDateTime'
parent_id_field = 'languageID'
table_name_field = bd_table.TableField(name_field, bd_table.TableFieldDestiny.NAME, bd_table.TableFieldType.STR)
table_parent_id_field = bd_table.TableField(parent_id_field, bd_table.TableFieldDestiny.PARENT_ID, bd_table.TableFieldType.INT)
table = bd_table.Table(table_name, [
bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
table_name_field,
bd_table.TableField(desc_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
bd_table.TableField(photo_field, bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldType.STR),
bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR),
bd_table.TableField(create_datetime_field, bd_table.TableFieldDestiny.CREATE_DATE, bd_table.TableFieldType.STR),
table_parent_id_field,
],
[
[table_name_field, table_parent_id_field],
]
)
init_access = f'{user_access.user_access_group_all}=-'
fsm = {
mod_table_operate.FSMs.CREATE: FSMCreateButton,
mod_table_operate.FSMs.EDIT_NAME: FSMEditButtonNameItem,
mod_table_operate.FSMs.EDIT_DESC: FSMEditButtonDescItem,
mod_table_operate.FSMs.EDIT_PHOTO: FSMEditButtonPhotoItem,
mod_table_operate.FSMs.EDIT_ACCESS: FSMEditButtonAccessItem,
}
# ---------------------------------------------------------
# Сообщения и кнопки
button_names = {
mod_simple_message.ButtonNames.START: "❍ Названия кнопок",
mod_table_operate.ButtonNames.LIST: "📃 Список названий кнопок",
mod_table_operate.ButtonNames.ADD: "☑ Добавить название кнопки",
mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать названия кнопок",
mod_table_operate.ButtonNames.EDIT_PHOTO: "☐ Изменить изображение у названия кнопок",
mod_table_operate.ButtonNames.EDIT_NAME: "≂ Изменить название у названия кнопок",
mod_table_operate.ButtonNames.EDIT_DESC: "𝌴 Изменить описание у названия кнопок",
mod_table_operate.ButtonNames.EDIT_ACCESS: "✋ Изменить доступ к названию кнопки",
mod_table_operate.ButtonNames.DEL: "❌ Удалить название кнопки",
}
messages = {
mod_simple_message.Messages.START: f'''
<b>{button_names[mod_simple_message.ButtonNames.START]}</b>
''',
mod_table_operate.Messages.SELECT: '''
Пожалуйста, выберите название кнопки:
''',
mod_table_operate.Messages.ERROR_FIND: '''
Ошибка, название кнопки не найдено
''',
mod_table_operate.Messages.OPEN: f'''
<b>Название кнопки: #{name_field}</b>
#{desc_field}
Время создания: #{create_datetime_field}
''',
mod_table_operate.Messages.CREATE_NAME: '''
Создание названия кнопки Шаг 1
Введите название название кнопки:
''',
mod_table_operate.Messages.CREATE_DESC: '''
Создание названия кнопки. Шаг 2
Введите описание название кнопки:
''',
mod_table_operate.Messages.CREATE_PHOTO: '''
Создание названия кнопки
Загрузите обложку для названия кнопок (Фото):
На данный момент не поддерживается!
Нажмите пропустить
''',
mod_table_operate.Messages.SUCCESS_CREATE: '''✅ Название кнопки успешно добавлено!''',
mod_table_operate.Messages.START_EDIT: '''
Пожалуйста, выберите действие:
''',
mod_table_operate.Messages.SELECT_TO_EDIT: '''
Выберите название кнопки, который вы хотите отредактировать.
''',
mod_table_operate.Messages.EDIT_PHOTO: '''
Загрузите новую обложку для названия кнопок (Фото):
Она будет отображаться в его описании.
''',
mod_table_operate.Messages.EDIT_NAME: f'''
Текущее название названия кнопок:
#{name_field}
Введите новое название названия кнопки:
''',
mod_table_operate.Messages.EDIT_DESC: f'''
Текущее описание названия кнопок:
#{desc_field}
Введите новое описание названия кнопки:
''',
mod_table_operate.Messages.EDIT_ACCESS: f'''
Текущий доступ к названию кнопки:
#{access_field}
{user_access.user_access_readme}
Введите новую строку доступа:
''',
mod_table_operate.Messages.SUCCESS_EDIT: '''✅ Название кнопки успешно отредактировано!''',
mod_table_operate.Messages.SELECT_TO_DELETE: '''
Выберите название кнопки, которое вы хотите удалить.
''',
mod_table_operate.Messages.SUCCESS_DELETE: '''✅ Название кнопки успешно удалено!''',
}
class ModuleButtons(mod_table_operate.TableOperateModule):
def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log):
super().__init__(table, messages, button_names, fsm, a_ParentModName, a_ChildModName, init_access, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log)
def GetName(self):
return module_name
def AddOrIgnoreMessage(self, a_Message):
table_name = self.m_Table.GetName()
name_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.NAME)
photo_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PHOTO)
desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC)
access_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ACCESS)
create_datetime_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.CREATE_DATE)
parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
lang_id = self.GetModule(self.m_ParentModName).GetLangID(a_Message.m_Language)
return self.m_Bot.SQLRequest(f'INSERT OR IGNORE INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (a_Message.m_PhotoID, a_Message.m_MessageName, a_Message.m_MessageDesc, access_utils.GetItemDefaultAccessForModule(self.m_Bot, module_name), lang_id))
def FlushMessages(self):
msg = self.m_BotButtons.GetMessages()
for lang, msg_dict in msg.items():
for msg_name, message in msg_dict.items():
self.AddOrIgnoreMessage(message)
table_name = self.m_Table.GetName()
msgs_bd = bd_item.GetAllItemsTemplate(self.m_Bot, table_name)()
if msgs_bd:
for m in msgs_bd:
name = m[1]
desc = m[2]
photo_id = m[3]
lang_id = m[6]
lang_name = self.GetModule(self.m_ParentModName).GetLangName(lang_id)
self.m_BotButtons.CreateMessage(name, desc, self.m_Log.GetTimeNow(), a_MessagePhotoID = photo_id, a_MessageLang = lang_name)
self.m_BotButtons.UpdateSignal(self.m_Log.GetTimeNow())
def OnChange(self):
self.FlushMessages()

185
bot_modules/languages.py

@ -192,188 +192,3 @@ class ModuleLanguages(mod_table_operate.TableOperateModule):
def OnChange(self):
self.FlushLanguages()
'''
# стартовое язык
async def LanguagesOpen(a_Language : types.message, state = None):
return simple_message.WorkFuncResult(base_language_message)
def GetButtonNameAndKeyValueAndAccess(a_Item):
# languageName languageID languageAccess
return a_Item[1], a_Item[0], a_Item[4]
def ShowMessageTemplate(a_StringLanguage, keyboard_template_func = None):
async def ShowLanguage(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
msg = a_StringLanguage.\
replace(f'#{name_field}', a_Item[1]).\
replace(f'#{desc_field}', a_Item[2]).\
replace(f'#{create_datetime_field}', a_Item[5]).\
replace(f'#{access_field}', a_Item[4])
keyboard_func = None
if keyboard_template_func:
keyboard_func = keyboard_template_func(a_Item[0])
return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func)
return ShowLanguage
def SimpleMessageTemplateLegacy(a_StringLanguage):
async def ShowLanguage(a_CallbackQuery : types.CallbackQuery, a_Item):
return simple_message.WorkFuncResult(a_StringLanguage)
return ShowLanguage
# Удаление языка
async def LanguagePreDelete(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
access = a_Item[4]
return simple_message.WorkFuncResult('', None, item_access = access)
async def LanguagePostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID):
log.Success(f'Язык №{a_ItemID} был удалён пользователем {a_CallbackQuery.from_user.id}.')
#TODO: удалить вложенные
FlushLanguages()
return simple_message.WorkFuncResult(language_success_delete_message)
# ---------------------------------------------------------
# Работа с базой данных языков
def AddBDItemFunc(a_ItemData, a_UserID):
res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name) + f";{a_UserID}=+"))
if error:
log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
else:
log.Success(f'Пользоватлель {a_UserID}. Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
FlushLanguages()
return res, error
# ---------------------------------------------------------
# API
def AddOrIgnoreLang(a_Lang : str):
res, error = bot_bd.SQLRequestToBD(f'INSERT OR IGNORE INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (0, a_Lang, '', access.GetItemDefaultAccessForModule(module_name)))
m_LanguageIDs = {}
def GetLangID(a_Lang):
global m_LanguageIDs
return m_LanguageIDs.get(a_Lang, None)
def GetLangName(a_LangID):
global m_LanguageIDs
for lang_name, lang_id in m_LanguageIDs:
if a_LangID == lang_id:
return lang_name
return user_messages.default_language
def FlushLanguages():
global m_LanguageIDs
msg = user_messages.GetMessages()
for lang, msg_dict in msg.items():
AddOrIgnoreLang(lang)
langs = bd_item.GetAllItemsTemplate(table_name)()
if langs:
for l in langs:
m_LanguageIDs[l[1]] = str(l[0])
print('FlushLanguages', m_LanguageIDs)
# Инициализация БД
def GetInitBDCommands():
return init_bd_cmds
def GetAccess():
return access.GetAccessForModule(module_name)
# Доступные кнопки
def GetModuleButtons():
return [
keyboard.ButtonWithAccess(languages_button_name, user_access.AccessMode.VIEW, GetAccess()),
]
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
defaul_keyboard_func = GetStartLanguageKeyboardButtons
# Список языков
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, \
key_name, \
ShowMessageTemplate(language_open_message, GetViewItemInlineKeyboardTemplate), \
GetButtonNameAndKeyValueAndAccess, \
select_language_message, \
GetAccess, \
defaul_keyboard_func\
)
# Удаление языка
bd_item_delete.DeleteBDItemRegisterHandlers(dp, \
None, \
bd_item.GetCheckForTextFunc(del_language_button_name), \
table_name, \
key_name, \
None, \
LanguagePreDelete, \
LanguagePostDelete, \
GetButtonNameAndKeyValueAndAccess, \
select_language_message, \
GetAccess, \
defaul_keyboard_func\
)
# Добавление языка
bd_item_add.AddBDItem3RegisterHandlers(dp, \
bd_item.GetCheckForTextFunc(add_language_button_name), \
FSMCreateLanguage,\
FSMCreateLanguage.name,\
FSMCreateLanguage.desc, \
FSMCreateLanguage.photo,\
AddBDItemFunc, \
SimpleMessageTemplateLegacy(language_create_name_message), \
SimpleMessageTemplateLegacy(language_create_desc_message), \
SimpleMessageTemplateLegacy(language_create_photo_message), \
SimpleMessageTemplateLegacy(language_success_create_message), \
None,\
None, \
None, \
name_field, \
desc_field, \
photo_field, \
GetButtonNameAndKeyValueAndAccess, \
GetAccess, \
GetStartLanguageKeyboardButtons\
)
# Редактирование языка
edit_keyboard_func = GetEditLanguageKeyboardButtons
def RegisterEdit(a_ButtonName, a_FSM, a_EditLanguage, a_FieldName, a_FieldType, a_AccessMode = user_access.AccessMode.EDIT):
bd_item_edit.EditBDItemRegisterHandlers(dp, \
None, \
a_FSM, \
bd_item.GetCheckForTextFunc(a_ButtonName), \
language_select_to_edit_message, \
ShowMessageTemplate(a_EditLanguage), \
ShowMessageTemplate(language_success_edit_message), \
table_name, \
key_name, \
None, \
a_FieldName, \
GetButtonNameAndKeyValueAndAccess, \
GetAccess, \
edit_keyboard_func, \
access_mode = a_AccessMode, \
field_type = a_FieldType\
)
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)
RegisterEdit(edit_language_access_button_name, FSMEditLanguageAccessItem, language_edit_access_message, access_field, bd_item.FieldType.text)
'''

237
bot_modules/messages.py

@ -160,7 +160,6 @@ messages = {
class ModuleMessages(mod_table_operate.TableOperateModule):
def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log):
super().__init__(table, messages, button_names, fsm, a_ParentModName, a_ChildModName, init_access, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log)
self.m_LanguageIDs = {}
def GetName(self):
return module_name
@ -199,239 +198,3 @@ class ModuleMessages(mod_table_operate.TableOperateModule):
def OnChange(self):
self.FlushMessages()
'''
# ---------------------------------------------------------
# Работа с кнопками
def GetEditMessageKeyboardButtons(a_Message, a_UserGroups):
cur_buttons = GetModuleButtons() + [
keyboard.ButtonWithAccess(edit_message_photo_button_name, user_access.AccessMode.EDIT, GetAccess()),
keyboard.ButtonWithAccess(edit_message_name_button_name, user_access.AccessMode.EDIT, GetAccess()),
keyboard.ButtonWithAccess(edit_message_desc_button_name, user_access.AccessMode.EDIT, GetAccess()),
keyboard.ButtonWithAccess(edit_message_access_button_name, user_access.AccessMode.ACCEES_EDIT, GetAccess()),
]
mods = [start]
return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups)
def GetStartMessageKeyboardButtons(a_Message, a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(list_message_button_name, user_access.AccessMode.VIEW, GetAccess()),
keyboard.ButtonWithAccess(add_message_button_name, user_access.AccessMode.ADD, GetAccess()),
keyboard.ButtonWithAccess(del_message_button_name, user_access.AccessMode.DELETE, GetAccess()),
keyboard.ButtonWithAccess(edit_message_button_name, user_access.AccessMode.EDIT, GetAccess())
]
mods = [start, languages]
return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups)
def GetViewItemInlineKeyboardTemplate(a_ItemID):
def GetViewItemInlineKeyboard(a_Message, a_UserGroups):
cur_buttons = [
#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
# ---------------------------------------------------------
# Обработка сообщений
# стартовое сообщение
async def MessagesOpen(a_Message : types.message, state = None):
return simple_message.WorkFuncResult(base_message_message)
def GetButtonNameAndKeyValueAndAccess(a_Item):
# messageName messageID messageAccess
return a_Item[1], a_Item[0], a_Item[4]
def ShowMessageTemplate(a_StringMessage, keyboard_template_func = None):
async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
if message_success_edit_message == a_StringMessage:
FlushMessages()
# TODO FlushMessages происходит рано. Нужно после изменений
msg = a_StringMessage.\
replace(f'#{name_field}', a_Item[1]).\
replace(f'#{desc_field}', a_Item[2]).\
replace(f'#{create_datetime_field}', a_Item[5]).\
replace(f'#{access_field}', a_Item[4])
keyboard_func = None
if keyboard_template_func:
keyboard_func = keyboard_template_func(a_Item[0])
return simple_message.WorkFuncResult(msg, photo_id = a_Item[3], item_access = a_Item[4], keyboard_func = keyboard_func)
return ShowMessage
def SimpleMessageTemplateLegacy(a_StringMessage):
async def ShowMessage(a_CallbackQuery : types.CallbackQuery, a_Item):
return simple_message.WorkFuncResult(a_StringMessage)
return ShowMessage
# Удаление сообщения
async def MessagePreDelete(a_CallbackQuery : types.CallbackQuery, a_Item):
if (len(a_Item) < 6):
return simple_message.WorkFuncResult(error_find_proj_message)
access = a_Item[4]
return simple_message.WorkFuncResult('', None, item_access = access)
async def MessagePostDelete(a_CallbackQuery : types.CallbackQuery, a_ItemID):
log.Success(f'Сообщение №{a_ItemID} была удалена пользователем {a_CallbackQuery.from_user.id}.')
#TODO: удалить вложенные
FlushMessages()
return simple_message.WorkFuncResult(message_success_delete_message)
# ---------------------------------------------------------
# Работа с базой данных сообщений
def AddBDItemFunc(a_ItemData, a_UserID):
res, error = bot_bd.SQLRequestToBD(f'INSERT INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (a_ItemData[photo_field], a_ItemData[name_field], a_ItemData[desc_field], access.GetItemDefaultAccessForModule(module_name) + f";{a_UserID}=+", a_ItemData[parent_id_field]))
if error:
log.Error(f'Пользоватлель {a_UserID}. Ошибка добавления записи в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
else:
log.Success(f'Пользоватлель {a_UserID}. Добавлена запись в таблицу {table_name} ({a_ItemData[photo_field]}, {a_ItemData[name_field]}, {a_ItemData[desc_field]}, {access.GetItemDefaultAccessForModule(module_name)}).')
FlushMessages()
return res, error
# ---------------------------------------------------------
# API
def AddOrIgnoreMessage(a_Message : user_messages.Message):
return bot_bd.SQLRequestToBD(f'INSERT OR IGNORE INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (a_Message.m_PhotoID, a_Message.m_MessageName, a_Message.m_MessageDesc, access.GetItemDefaultAccessForModule(module_name), languages.GetLangID(a_Message.m_Language)))
def FlushMessages():
msg = user_messages.GetMessages()
for lang, msg_dict in msg.items():
for msg_name, message in msg_dict.items():
AddOrIgnoreMessage(message)
msgs_bd = bd_item.GetAllItemsTemplate(table_name)()
if msgs_bd:
for m in msgs_bd:
name = m[1]
lang_id = m[6]
lang_name = languages.GetLangName(lang_id)
new_msg = user_messages.Message(name, m[2], lang_name, m[3], log.GetTimeNow())
if not msg.get(lang_name, None):
msg[lang_name] = {}
msg[lang_name][name] = new_msg
user_messages.UpdateSignal(log.GetTimeNow())
# Инициализация БД
def GetInitBDCommands():
return init_bd_cmds
def GetAccess():
return access.GetAccessForModule(module_name)
# Доступные кнопки
def GetModuleButtons():
return [keyboard.ButtonWithAccess(messages_button_name, user_access.AccessMode.VIEW, GetAccess())]
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
defaul_keyboard_func = GetStartMessageKeyboardButtons
def RegisterSelectParent(a_ButtonName, access_mode):
a_PrefixBase = a_ButtonName
return bd_item_select.FirstSelectBDItemRegisterHandlers(dp, \
a_PrefixBase, \
a_ButtonName, \
languages.table_name, \
languages.key_name, \
languages.GetButtonNameAndKeyValueAndAccess, \
languages.select_language_message, \
languages.GetAccess, access_mode = access_mode\
)
# Стартовое сообщение
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)
bd_item_view.LastSelectAndShowBDItemRegisterHandlers(dp, \
a_Prefix, parent_id_field, \
table_name, key_name, \
ShowMessageTemplate(message_open_message), \
GetButtonNameAndKeyValueAndAccess, \
select_message_message, \
GetAccess, \
defaul_keyboard_func, \
access_mode = user_access.AccessMode.VIEW\
)
global select_messages_prefix
select_messages_prefix = a_Prefix
# Удаление сообщения
a_Prefix = RegisterSelectParent(del_message_button_name, user_access.AccessMode.DELETE)
bd_item_delete.DeleteBDItemRegisterHandlers(dp, \
a_Prefix, \
bd_item.GetCheckForPrefixFunc(a_Prefix), \
table_name, \
key_name, \
parent_id_field, \
MessagePreDelete, \
MessagePostDelete, \
GetButtonNameAndKeyValueAndAccess, \
select_message_message, \
GetAccess, \
defaul_keyboard_func\
)
# Добавление сообщения
a_Prefix = RegisterSelectParent(add_message_button_name, user_access.AccessMode.ADD)
bd_item_add.AddBDItem3RegisterHandlers(dp, \
bd_item.GetCheckForPrefixFunc(a_Prefix), \
FSMCreateMessage, \
FSMCreateMessage.name,\
FSMCreateMessage.desc, \
FSMCreateMessage.photo,\
AddBDItemFunc, \
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, \
name_field, \
desc_field, \
photo_field, \
GetButtonNameAndKeyValueAndAccess, \
GetAccess, \
GetStartMessageKeyboardButtons\
)
def RegisterEdit(a_ButtonName, a_FSM, a_EditMessage, a_FieldName, a_FieldType, a_AccessMode = user_access.AccessMode.EDIT):
a_Prefix = RegisterSelectParent(a_ButtonName, a_AccessMode)
bd_item_edit.EditBDItemRegisterHandlers(dp, \
a_Prefix, \
a_FSM, \
bd_item.GetCheckForPrefixFunc(a_Prefix), \
message_select_to_edit_message, \
ShowMessageTemplate(a_EditMessage), \
ShowMessageTemplate(message_success_edit_message), \
table_name, \
key_name, \
parent_id_field, \
a_FieldName, \
GetButtonNameAndKeyValueAndAccess, \
GetAccess, \
edit_keyboard_func, \
access_mode = a_AccessMode, \
field_type = a_FieldType\
)
# Редактирование сообщения
edit_keyboard_func = GetEditMessageKeyboardButtons
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)
RegisterEdit(edit_message_desc_button_name, FSMEditMessageDescItem, message_edit_desc_message, desc_field, bd_item.FieldType.text)
RegisterEdit(edit_message_access_button_name, FSMEditMessageAccessItem, message_edit_access_message, access_field, bd_item.FieldType.text)
'''

4
bot_modules/start.py

@ -7,6 +7,8 @@ from bot_sys import user_access
from bot_modules import mod_simple_message, profile
from template import bd_item
module_name = 'start'
button_names = {
mod_simple_message.ButtonNames.START: "☰ Главное меню",
}
@ -25,7 +27,7 @@ class ModuleStart(mod_simple_message.SimpleMessageModule):
super().__init__(messages, button_names, init_access, a_ChildModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log)
def GetName(self):
return 'start'
return module_name
# Основной обработчик главного сообщения
async def StartMessageHandler(self, a_Message, state = None):

27
main.py

@ -6,7 +6,7 @@ log_start_message = 'Бот успешно запущен!'
import os
from bot_sys import config, log, bot_bd, user_access, aiogram_bot, bot_messages, bd_table
from bot_modules import mod_agregator, start, profile, backup, groups, access, projects, tasks, needs, comments, languages, messages
from bot_modules import mod_agregator, start, profile, backup, groups, access, projects, tasks, needs, comments, languages, messages, buttons
g_Log = log
g_Bot = aiogram_bot.AiogramBot(config.GetTelegramBotApiToken(), bot_bd.GetBDFileName(), config.GetRootIDs(), g_Log)
@ -18,14 +18,14 @@ g_BotButtons = bot_messages.BotMessages(default_language)
g_ModuleAgregator = mod_agregator.ModuleAgregator()
mod_start_name = 'start'
mod_tasks_name = 'tasks'
mod_needs_name = 'needs'
mod_comments_name = 'comments'
mod_projects_name = 'projects'
mod_languages_name = 'languages'
mod_messages_name = 'messages'
mod_buttons_name = 'buttons'
mod_start_name = start.module_name
mod_tasks_name = tasks.module_name
mod_needs_name = needs.module_name
mod_comments_name = comments.module_name
mod_projects_name = projects.module_name
mod_languages_name = languages.module_name
mod_messages_name = messages.module_name
mod_buttons_name = buttons.module_name
start_mod_list = [mod_start_name]
@ -57,14 +57,18 @@ start_mod_name_list = [mod_start_name]#, mod_projects_name, mod_tasks_name, mod_
mod_comments= comments.ModuleComments(mod_needs_name, None, start_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_comments)
start_mod_name_list = [mod_start_name, mod_messages_name]#, mod_messages_name, mod_buttons_name
start_mod_name_list = [mod_start_name, mod_messages_name, mod_buttons_name]
mod_languages = languages.ModuleLanguages(None, mod_messages_name, start_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_languages)
start_mod_name_list = [mod_start_name]#, mod_messages_name, mod_buttons_name
start_mod_name_list = [mod_start_name]
mod_messages = messages.ModuleMessages(mod_languages_name, None, start_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_messages)
start_mod_name_list = [mod_start_name]
mod_buttons = buttons.ModuleButtons(mod_languages_name, None, start_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_buttons)
start_mod_name_list = [
mod_profile.GetName(),
mod_backup.GetName(),
@ -94,6 +98,7 @@ g_BotButtons.UpdateSignal(g_Log.GetTimeNow())
mod_languages.FlushLanguages()
mod_messages.FlushMessages()
mod_buttons.FlushMessages()
for m in modules:
m.RegisterHandlers()

Loading…
Cancel
Save