Модульный Telegram-бот с возможностью редактирования прав доступа, как пользователям, так и группам пользователей
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

#-*-coding utf-8-*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Работа с кнопками и клавиатурой
from bot_sys import user_access
from aiogram import types, Bot, Dispatcher
from aiogram.types import ReplyKeyboardRemove, ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
class ButtonWithAccess:
def __init__(self, a_Label, a_AccessMode : user_access.AccessMode, a_AccessString):
self.label = a_Label
self.access_mode = a_AccessMode
self.access_string = a_AccessString
def GetButtons(a_ModList):
buttons = []
for m in a_ModList:
b = m.GetModuleButtons()
if not b is None or len(b) != 0:
buttons += b
return buttons
import math
def Chunks(a_List, a_ChunkSize):
for i in range(0, len(a_List), a_ChunkSize):
yield a_List[i: i + a_ChunkSize]
def GetKeyInRowCount(a_AllKeyCount):
return min(max(int(math.sqrt(a_AllKeyCount) // 1), 1), 4)
def MakeKeyboard(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 = GetKeyInRowCount(len(buttons))
key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True)
return key
def MakeKeyboardRemove():
return types.ReplyKeyboardRemove()
def MakeKeyboardForMods(a_ModList, a_UserGroups):
buttons = GetButtons(a_ModList)
return MakeKeyboard(buttons, a_UserGroups)
class Button:
def __init__(self, a_Label, a_CallBackData):
self.label = a_Label
self.callback_data = str(a_CallBackData)
def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str):
buttons = []
for b in a_ButtonList:
buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')]
step = GetKeyInRowCount(len(buttons))
inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step))
return inline_keyboard