Source code for navmenu.item_contents

from abc import ABC, abstractmethod

from .keyboard import ButtonColors


[docs]class ItemContent(ABC): """Generic menu item content.""" __slots__ = ()
[docs] @abstractmethod def get_content(self, payload: str) -> dict: """Process the payload and return item content. Args: payload: An incoming message payload. Returns: Item content. """ pass
[docs] @abstractmethod def serialize(self) -> dict: """Serialize the class instance to a dictionary. Returns: Serialized class instance. """ pass
[docs]class TextItemContent(ItemContent): """A text menu item. Attributes: text: The item text. color: The item color. """ __slots__ = 'text', 'color' def __init__(self, text: str, color: int = ButtonColors.DEFAULT) -> None: self.text = text self.color = color def __repr__(self) -> str: return f'TextItemContent({repr(self.text)}, {self.color})'
[docs] def get_content(self, payload: str) -> dict: return { 'type': 'button', 'text': self.text, 'color': self.color, 'payload': payload, }
[docs] def serialize(self) -> dict: res = { 'text': self.text, } if self.color != ButtonColors.DEFAULT: res['color'] = self.color return res