Alexei
2 years ago
10 changed files with 254 additions and 97 deletions
@ -0,0 +1,12 @@ |
|||||||
|
# -*- coding: utf8 -*- |
||||||
|
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> |
||||||
|
|
||||||
|
# Права пользователей. Утилиты |
||||||
|
|
||||||
|
table_name = 'module_access' |
||||||
|
mod_name_field = 'modName' |
||||||
|
moduleaccess_field = 'modAccess' |
||||||
|
mod_default_access_field = 'itemDefaultAccess' |
||||||
|
|
||||||
|
def GetAccessForModuleRequest(module_name, access, default_access): |
||||||
|
return f"INSERT OR IGNORE INTO {table_name} ({mod_name_field}, {moduleaccess_field}, {mod_default_access_field}) VALUES ('{module_name}', '{access}', '{default_access}');" |
@ -0,0 +1,86 @@ |
|||||||
|
# -*- coding: utf8 -*- |
||||||
|
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> |
||||||
|
|
||||||
|
# Простой модуль с одним сообщением |
||||||
|
|
||||||
|
from bot_sys import keyboard, user_access |
||||||
|
from bot_modules import access_utils |
||||||
|
from template import simple_message |
||||||
|
|
||||||
|
class SimpleMessageModule(mod_interface.IModule): |
||||||
|
def __init__(self, a_StartMessage, a_StartButtonName, a_InitAccess, a_ChildModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_Log): |
||||||
|
self.m_ChildModuleNameList = a_ChildModuleNameList |
||||||
|
self.m_InitAccess = a_InitAccess |
||||||
|
self.m_Bot = a_Bot |
||||||
|
self.m_ModuleAgregator = a_ModuleAgregator |
||||||
|
self.m_BotMessages = a_BotMessages |
||||||
|
self.m_BotButtons = a_BotButtons |
||||||
|
self.m_Log = a_Log |
||||||
|
|
||||||
|
self.m_StartButtonName = CreateButton(f'{GetName()}_start', a_StartButtonName) |
||||||
|
self.m_StartMessage = CreateMessage(f'{GetName()}_start', a_StartMessage) |
||||||
|
|
||||||
|
async def StartMessageHandler(a_Message, state = None): |
||||||
|
return self.StartMessageHandler(a_Message, state) |
||||||
|
self.m_StartMessageHandlerFunc = StartMessageHandler |
||||||
|
|
||||||
|
def GetAccess(): |
||||||
|
return self.GetAccess() |
||||||
|
self.m_GetAccessFunc = GetAccess |
||||||
|
|
||||||
|
def GetStartKeyboardButtons(a_Message, a_UserGroups): |
||||||
|
return self.GetStartKeyboardButtons(a_Message, a_UserGroups) |
||||||
|
self.m_GetStartKeyboardButtonsFunc = GetStartKeyboardButtons |
||||||
|
|
||||||
|
self.m_StartMessageHandler = simple_message.SimpleMessageTemplate( |
||||||
|
self.m_Bot, |
||||||
|
self.m_StartMessageHandlerFunc, |
||||||
|
None, |
||||||
|
self.m_GetStartKeyboardButtonsFunc, |
||||||
|
self.m_GetAccessFunc |
||||||
|
) |
||||||
|
|
||||||
|
# Основной обработчик главного сообщения |
||||||
|
async def StartMessageHandler(a_Message, state = None): |
||||||
|
return simple_message.WorkFuncResult(self.m_StartMessage) |
||||||
|
|
||||||
|
def CreateMessage(a_MessageName, a_MessageDesc): |
||||||
|
msg = self.m_BotMessages.CreateMessage(a_MessageName, a_MessageDesc, self.m_Log.GetTimeNow()) |
||||||
|
return msg |
||||||
|
|
||||||
|
def CreateButton(a_ButtonName, a_ButtonDesc): |
||||||
|
btn = self.m_BotButtons.CreateMessage(a_ButtonName, a_ButtonDesc, self.m_Log.GetTimeNow()) |
||||||
|
return btn |
||||||
|
|
||||||
|
def GetStartKeyboardButtons(a_Message, a_UserGroups): |
||||||
|
def GetButtons(a_ModNameList): |
||||||
|
buttons = [] |
||||||
|
for n in a_ModNameList: |
||||||
|
m = self.m_ModuleAgregator.GetModule(n) |
||||||
|
b = m.GetModuleButtons() |
||||||
|
if not b is None or len(b) != 0: |
||||||
|
buttons += b |
||||||
|
return buttons |
||||||
|
|
||||||
|
buttons = GetButtons(self.m_ChildModuleNameList) |
||||||
|
return MakeButtons(buttons, a_UserGroups) |
||||||
|
|
||||||
|
def GetInitBDCommands(): |
||||||
|
return [ |
||||||
|
access_utils.GetAccessForModuleRequest(GetName(), self.m_InitAccess, self.m_InitAccess), |
||||||
|
] |
||||||
|
|
||||||
|
def GetAccess(): |
||||||
|
return self.m_Bot.GetAccessForModule(module_name) |
||||||
|
|
||||||
|
def GetModuleButtons(): |
||||||
|
return [ |
||||||
|
keyboard.ButtonWithAccess(self.m_StartButtonName, user_access.AccessMode.VIEW, GetAccess()), |
||||||
|
] |
||||||
|
|
||||||
|
def RegisterHandlers(): |
||||||
|
self.m_Bot.RegisterMessageHandler( |
||||||
|
self.m_StartMessageHandler, |
||||||
|
bd_item.GetCheckForTextFunc(self.m_StartButtonName) |
||||||
|
) |
||||||
|
|
@ -0,0 +1,68 @@ |
|||||||
|
#-*-coding utf-8-*- |
||||||
|
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> |
||||||
|
|
||||||
|
# Работа с сообщениями |
||||||
|
|
||||||
|
class BotMessage: |
||||||
|
def __init__(self, a_BotMessages, a_MessageName : str, a_MessageDesc : str, a_Language : str, a_PhotoID : str, a_DateTime): |
||||||
|
self.m_BotMessages = a_BotMessages |
||||||
|
self.m_MessageName = a_MessageName |
||||||
|
self.m_MessageDesc = a_MessageDesc |
||||||
|
self.m_Language = a_Language |
||||||
|
self.m_PhotoID = a_PhotoID |
||||||
|
self.m_DateTime = a_DateTime |
||||||
|
|
||||||
|
def GetName(): |
||||||
|
return self.m_MessageName |
||||||
|
|
||||||
|
def GetDesc(): |
||||||
|
return self.m_MessageDesc |
||||||
|
|
||||||
|
def GetLanguage(): |
||||||
|
return self.m_Language |
||||||
|
|
||||||
|
def GetPhotoID(): |
||||||
|
return self.m_PhotoID |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
msg = GetMessageForLang(self.m_Language) |
||||||
|
return msg.GetDesc() |
||||||
|
|
||||||
|
def GetMessageForLang(self, a_Language): |
||||||
|
last_update = self.m_BotMessages.m_LastUpdate |
||||||
|
new_msg = self |
||||||
|
if self.m_DateTime < last_update: |
||||||
|
msg = self.m_BotMessages.GetMessages() |
||||||
|
if not msg.get(a_Language, None): |
||||||
|
a_Language = self.m_Language |
||||||
|
if not msg.get(a_Language, None): |
||||||
|
a_Language = self.m_BotMessages.a_DefaultLanguage |
||||||
|
new_msg = msg[a_Language].get(self.m_MessageName, self) |
||||||
|
if a_Language == self.m_Language: |
||||||
|
self.m_MessageDesc = new_msg.m_MessageDesc |
||||||
|
self.m_Language = new_msg.m_Language |
||||||
|
self.m_PhotoID = new_msg.m_PhotoID |
||||||
|
self.m_DateTime = new_msg.m_DateTime |
||||||
|
return new_msg |
||||||
|
|
||||||
|
class BotMessages: |
||||||
|
def __init__(self, a_DefaultLanguage): |
||||||
|
self.a_DefaultLanguage = a_DefaultLanguage |
||||||
|
self.m_Messages = {} |
||||||
|
self.m_LastUpdate = None |
||||||
|
|
||||||
|
def GetMessages(): |
||||||
|
return self.m_Messages |
||||||
|
|
||||||
|
def UpdateSignal(a_DateTime): |
||||||
|
self.m_LastUpdate = a_DateTime |
||||||
|
|
||||||
|
def CreateMessage(a_MessageName, a_MessageDesc, a_DateTime): |
||||||
|
cur_msg = BotMessage(a_MessageName, a_MessageDesc, self.a_DefaultLanguage, 0, a_DateTime) |
||||||
|
msg = GetMessages() |
||||||
|
if not msg.get(self.a_DefaultLanguage, None): |
||||||
|
msg[self.a_DefaultLanguage] = {} |
||||||
|
if not msg[self.a_DefaultLanguage].get(a_MessageName, None): |
||||||
|
msg[self.a_DefaultLanguage][a_MessageName] = cur_msg |
||||||
|
return cur_msg |
||||||
|
|
Loading…
Reference in new issue