Template
Try to add markup
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
from telebot import TeleBot
|
||||
|
||||
from ..i18n import I18N
|
||||
from .base import MarkupManager
|
||||
from .simple import SimpleMarkup
|
||||
|
||||
|
||||
def setup_markup(bot: TeleBot, i18n: I18N):
|
||||
markup_mg = MarkupManager(bot, i18n)
|
||||
markup_mg.register_prototype(SimpleMarkup)
|
||||
return markup_mg
|
||||
@@ -0,0 +1,55 @@
|
||||
import abc
|
||||
from typing import Optional, Type
|
||||
|
||||
from telebot import TeleBot
|
||||
from telebot.types import CallbackQuery, InlineKeyboardMarkup
|
||||
|
||||
from ..i18n import I18N
|
||||
|
||||
|
||||
class Markup (metaclass=abc.ABCMeta):
|
||||
tag: str
|
||||
|
||||
def __init__(self, bot: TeleBot, i18n: I18N):
|
||||
self.bot = bot
|
||||
self.t = i18n
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.build(*args, **kwargs)
|
||||
|
||||
def check(self, call: CallbackQuery) -> bool:
|
||||
return call.data == self.tag
|
||||
|
||||
@abc.abstractmethod
|
||||
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||
pass
|
||||
|
||||
|
||||
class DummyMarkup (Markup):
|
||||
tag = "__dummy"
|
||||
|
||||
def check(self, call: CallbackQuery) -> bool:
|
||||
return True
|
||||
|
||||
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||
return None
|
||||
|
||||
|
||||
class MarkupManager:
|
||||
def __init__(self, bot: TeleBot, i18n: I18N):
|
||||
self.bot = bot
|
||||
self.i18n = i18n
|
||||
self._prototypes: list[Markup] = []
|
||||
self._dummy = DummyMarkup(self.bot, self.i18n)
|
||||
|
||||
def register_prototype(self, markup_proto_class: Type[Markup]):
|
||||
self._prototypes.append(markup_proto_class(self.bot, self.i18n))
|
||||
|
||||
def __call__(self, tag: str, *args, **kwargs):
|
||||
return self.get(tag).build(*args, **kwargs)
|
||||
|
||||
def get(self, tag: str) -> Optional[Markup]:
|
||||
for mp in self._prototypes:
|
||||
if mp.tag == tag:
|
||||
return mp
|
||||
return None
|
||||
@@ -0,0 +1,15 @@
|
||||
from typing import Optional
|
||||
|
||||
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
|
||||
|
||||
from .base import Markup
|
||||
|
||||
|
||||
class SimpleMarkup (Markup):
|
||||
tag = "start"
|
||||
|
||||
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||
return (InlineKeyboardMarkup()
|
||||
.add(InlineKeyboardButton("start", callback_data="start"))
|
||||
.add(InlineKeyboardButton("help", callback_data="help"))
|
||||
)
|
||||
Reference in New Issue
Block a user