Browse Source

Новый модуль для отправки сообщений пользователю #18

master
parent
commit
8c5605145b
  1. 40
      bot_modules/mod_table_operate.py
  2. 143
      bot_modules/user_message.py
  3. 7
      main.py

40
bot_modules/mod_table_operate.py

@ -160,12 +160,21 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
def GetSelectPrefix(self):
return self.m_SelectPrefix
def GetDelPrefix(self):
return self.m_DelPrefix
def GetAddPrefix(self):
return self.m_AddPrefix
def GetShowPrefix(self):
return self.m_ShowPrefix
def GetViewItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID):
if not self.m_ChildModName:
return None
child_mod = self.GetModule(self.m_ChildModName)
cur_buttons = [
keyboard.InlineButtonWithAccess(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, self.GetAccess(), user_access.AccessMode.VIEW),
keyboard.InlineButtonWithAccess(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, child_mod.GetAccess(), user_access.AccessMode.VIEW),
]
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)
@ -287,7 +296,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
key_name = self.GetKeyFieldName()
cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_ItemID)
print(cur_item)
parent_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if parent_id_field_index and cur_item and cur_item[parent_id_field_index]:
@ -349,6 +357,17 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
return res, error
async def SendMessageToUser(self, a_Message, a_UserID, a_GetButtonsFunc = None, a_GetInlineButtonsFunc = None, parse_mode = None):
a_Message = a_Message.StaticCopy()
a_MessageFromUSer = None
user_groups = None
try:
print('SendMessage', self.m_Bot, a_Message, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_MessageFromUSer, user_groups, parse_mode)
await simple_message.SendMessage(self.m_Bot, a_Message, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_MessageFromUSer, user_groups, parse_mode=parse_mode)
return True
except:
return False
async def SendSubscribe(self, a_Type, a_ItemID, a_OwnerUserID):
for s_user_id in self.m_BotSubscribes.GetUserIDs(self.GetName(), a_Type, a_ItemID = a_ItemID):
a_BotMessage = self.GetMessage(SubscribeMessage(a_Type))
@ -356,18 +375,11 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
continue
if s_user_id == a_OwnerUserID:
continue
a_UserID = s_user_id
a_BotMessage = a_BotMessage.StaticCopy()
a_BotMessage.UpdateDesc(a_BotMessage.GetDesc().replace("#item_id", str(a_ItemID)))
a_GetButtonsFunc = None
a_GetInlineButtonsFunc = None
a_UserID = s_user_id
a_Message = None
user_groups = None
parse_mode = None
try:
await simple_message.SendMessage(self.m_Bot, a_BotMessage, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_Message, user_groups, parse_mode=parse_mode)
except:
return
await self.SendMessageToUser(a_BotMessage, a_UserID)
def SelectSourceTemplate(self, a_PrevPrefix, a_ButtonName):
parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
@ -586,6 +598,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
GetAccess,\
access_mode = user_access.AccessMode.VIEW\
)
self.m_ShowPrefix = a_Prefix
bd_item_view.ShowBDItemRegisterHandlers(self.m_Bot,\
a_Prefix,\
table_name,\
@ -600,6 +613,8 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_ButtonName = self.GetButton(ButtonNames.DEL)
if a_ButtonName:
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW)
self.m_DelPrefix = a_Prefix
bd_item_delete.DeleteBDItemRegisterHandlers(self.m_Bot, \
a_Prefix, \
table_name, \
@ -614,6 +629,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_ButtonName = self.GetButton(ButtonNames.ADD)
if a_ButtonName:
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True)
self.m_AddPrefix = a_Prefix
check_func = bd_item.GetCheckForTextFunc(a_ButtonName)
if a_Prefix:

143
bot_modules/user_message.py

@ -0,0 +1,143 @@
# -*- coding: utf8 -*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Сообщения пользователю
from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes, bot_messages
from bot_modules import mod_table_operate, mod_simple_message, users
# ---------------------------------------------------------
# БД
module_name = 'user_messge'
table_name = module_name
key_name = 'userMessageID'
desc_field = 'messageText'
access_field = 'catAccess'
create_datetime_field = 'catCreateDateTime'
parent_id_field = users.key_name
table = bd_table.Table(table_name, [
bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
bd_table.TableField(desc_field, bd_table.TableFieldDestiny.DESC, 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),
bd_table.TableField(parent_id_field, bd_table.TableFieldDestiny.PARENT_ID, bd_table.TableFieldType.INT),
])
init_access = f'{user_access.user_access_group_new}=-'
# ---------------------------------------------------------
# Сообщения и кнопки
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.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание в категории",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.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>Категория: </b>
Пользоватеель: #{parent_id_field}
Текст сообщения: #{desc_field}
Время создания: #{create_datetime_field}
''',
mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): '''
Отправка сообщения
Введите текст сообщения:
''',
mod_table_operate.Messages.SUCCESS_CREATE: f'''✅ Сообщение успешно отправлено пользователю!''',
mod_table_operate.Messages.START_EDIT: '''
Пожалуйста, выберите действие:
''',
mod_table_operate.Messages.SELECT_TO_EDIT: '''
Выберите проект, который вы хотите отредактировать.
''',
mod_table_operate.EditMessage(bd_table.TableFieldDestiny.DESC): f'''
Текущий текст сообщения:
#{desc_field}
Введите новый текст:
''',
mod_table_operate.EditMessage(bd_table.TableFieldDestiny.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: '''✅ Сообщение успешно удалёно!''',
}
messages_subscribes = {
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD):f'''Сообщение создано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT):f'''Сообщение отредактировано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL):f'''Сообщение удалено''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_EDIT):f'''Сообщение отредактировано #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_DEL):f'''Сообщение удалено #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT):f'''Сообщение создано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT):f'''Сообщение отредактировано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT):f'''Сообщение удалено''',
}
messages.update(messages_subscribes)
class ModuleUserMessage(mod_table_operate.TableOperateModule):
def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log):
super().__init__(table, messages, button_names, a_ParentModName, a_ChildModName, init_access, init_access, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log)
def GetName(self):
return module_name
async def AddBDItemFunc(self, a_ItemData, a_UserID):
res, error = await super().AddBDItemFunc(a_ItemData, a_UserID)
desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC)
user_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if not await self.SendMessageToUser(bot_messages.MakeBotMessage(a_ItemData[desc_field]), a_ItemData[user_id_field]):
return None, '❌ Ошибка отправки сообщения'
return res, error
def GetStartButtons(self, a_Message, a_UserGroups):
return [
[mod_table_operate.ButtonNames.ADD, user_access.AccessMode.ADD],
[mod_table_operate.ButtonNames.LIST, user_access.AccessMode.VIEW],
[mod_table_operate.ButtonNames.EDIT, user_access.AccessMode.EDIT],
[mod_table_operate.ButtonNames.DEL, user_access.AccessMode.DELETE],
]
def GetButtonNameAndKeyValueAndAccess(self, a_Item):
key_name_id = self.GetKeyFieldID()
date_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.CREATE_DATE)
access_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)
assert key_name_id != None
assert date_field_id != None
assert access_field_id != None
return \
a_Item[date_field_id],\
a_Item[key_name_id],\
a_Item[access_field_id]

7
main.py

@ -7,6 +7,7 @@ from bot_sys import config, log, aiogram_bot, bot_messages, bd_table, user_acces
from bot_modules import mod_agregator, start, profile, backup, users_groups_agregator, access, projects, tasks, needs, comments
from bot_modules import languages, messages, buttons, users, groups, user_in_groups
from bot_modules import orders_cat, orders, all_orders, authorize
from bot_modules import user_message
from bot_modules import bd_version
from bot_sys import bot_subscribes
@ -43,6 +44,7 @@ mod_user_in_groups_name = user_in_groups.module_name
mod_orders_cat_name = orders_cat.module_name
mod_orders_name = orders.module_name
mod_all_orders_name = all_orders.module_name
mod_user_message_name = user_message.module_name
start_mod_list = [mod_start_name]
mod_access = access.ModuleAccess(start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
@ -51,6 +53,9 @@ g_ModuleAgregator.AddModule(mod_access)
mod_users = users.ModuleUsers(None, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_users)
mod_user_message = user_message.ModuleUserMessage(mod_users_name, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_user_message)
child_mod_name_list = [mod_start_name, mod_users_name, mod_user_in_groups_name]
mod_groups = groups.ModuleGroups(None, mod_user_in_groups_name, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_groups)
@ -58,7 +63,7 @@ g_ModuleAgregator.AddModule(mod_groups)
mod_user_in_groups = user_in_groups.ModuleUserInGroups(mod_groups_name, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_user_in_groups)
child_mod_name_list = [mod_start_name, mod_users_name, mod_groups_name, mod_user_in_groups_name]
child_mod_name_list = [mod_start_name, mod_users_name, mod_groups_name, mod_user_in_groups_name, mod_user_message_name]
mod_users_groups_agregator = users_groups_agregator.ModuleUsersGroupsAgregator(child_mod_name_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_users_groups_agregator)

Loading…
Cancel
Save