morington

keyboards

Nov 29th, 2023
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. Клава:
  2.  
  3. from typing import Optional
  4.  
  5. from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
  6. from aiogram.utils.keyboard import InlineKeyboardButton, InlineKeyboardMarkup
  7.  
  8.  
  9. def template(
  10.         text: str,
  11.         callback_data: Optional[str] = None
  12. ) -> dict[str, str]:
  13.     """
  14.    Example: template("text", "callback")
  15.  
  16.    :param text: str
  17.    :param callback_data: Optional[str]
  18.    :return: dict[str, str]
  19.    """
  20.     return {
  21.         "text": text,
  22.         "callback_data": callback_data if callback_data is not None else "CALLBACK_DATA"
  23.     }
  24.  
  25.  
  26. def callback_keyb(
  27.         buttons: list[list | dict]
  28. ) -> InlineKeyboardMarkup:
  29.     """
  30.    Example: [template(), template() [template(), template()]]
  31.  
  32.    :param buttons: list[dict]
  33.    :return: InlineKeyboardMarkup
  34.    """
  35.     keyboard: list = []
  36.  
  37.     for button in buttons:
  38.         if isinstance(button, list):
  39.             keyboard.append([
  40.                 InlineKeyboardButton(**btn)
  41.                 for btn in button
  42.             ])
  43.         elif isinstance(button, dict):
  44.             keyboard.append([InlineKeyboardButton(**button)])
  45.  
  46.     return InlineKeyboardMarkup(inline_keyboard=keyboard)
  47.  
  48.  
  49. def reply_keyb(
  50.         buttons: list[str | list[str]]
  51. ) -> ReplyKeyboardMarkup:
  52.     """
  53.    Example: ["BTN_1", "BTN_2" ["BTN_3", "BTN_4"]]
  54.  
  55.    :param buttons: list[str]
  56.    :return: ReplyKeyboardMarkup
  57.    """
  58.     keyboard: list = []
  59.  
  60.     for item in buttons:
  61.         if isinstance(item, list):
  62.             keyboard.append([
  63.                 KeyboardButton(text=text)
  64.                 for text in item
  65.             ])
  66.         elif isinstance(item, str):
  67.             keyboard.append([KeyboardButton(text=item)])
  68.  
  69.     return ReplyKeyboardMarkup(
  70.         keyboard=keyboard,
  71.         resize_keyboard=True
  72.     )
Advertisement
Add Comment
Please, Sign In to add comment