Skip to main content

Object-oriented way to build telegram keyboard-menus

Project description

Pyromenu Build Status

Pyromenu is an easy-to-extend object-oriented library for building keyboard menus for telegram bots on the Pyrogram

Base usage case

from pyrogram import Client, Filters, ReplyKeyboardRemove
from pyromenu import KButton, KRow, KMenu


app = Client("app")
# it's menu declaration style
menu = KMenu(
  KRow(
    KButton("English"), KButton("Russian"), KButton("Portugues")
  ),
  KRow(
    KButton("exit")
  )
)


@app.on_message(Filters.command("start"))
def send_keyboard(clt, msg):
  msg.reply("Hi / Привет / Oi", reply_markup=menu.keyboard)

# use KButton update_filter property for creating Filter for Handler's
@app.on_message(KButon("exit").update_filter)
def remove_keyboard(clt, msg):
  msg.reply("Bye / Пока / Adeus", reply_markup=ReplyKeyboardRemove)

ABC's

Pyromenu define 3 abstract classes: Button, Row, Menu

  • Button
    • __hash__
    • __eq__
    • keyboard_button(properrty) - return KeyboardButton
    • update_filter(property) - return Filter
  • Row
    • keyboard_row(property) - return list of KeyboardButton's
    • buttons(property) - return list of Button's
  • Menu
    • keyboard(property) - return ReplyKeyboardButton
    • one_time_keyboard(property) - same as keyboard, but with one_time_keyboard flag on
    • filter(property) - return composite Filter of all button
    • matched_button - take message and return first matched button

Built-in's

Pyromenu provides simple built-in implementations of each abstract classes: KButton, KRow, KMenu

Usage

Pyromenu has a distinctive menu declaration style inspired by the principles of Elegant Objects.

from pyrogram import Client
from pyromenu import KButton, KRow, KMenu


app = Client("app")
menu = KMenu(
  KRow(
    KButton("English"), KButton("Russian"), KButton("Portugues")
  ),
  KRow(
    KButton("exit")
  )
)

Sending

app.send_message(chat_id, text, reply_markup=menu.keyboard)
# or
# send one time keyboard
app.send_message(chat_id, text, reply_markup=menu.one_time_keyboard)

Handling

@app.on_message(KButton("exit").update_filter)
def exit(clt, msg):
    msg.reply("Bye / Пока / Adeus" reply_markup=ReplyKeyboardRemove)
# or
# if you have more complex menu
@app.on_message(menu.filter)
def handle_menu(clt, msg):
    matched_button = menu.matched_button(msg)
    if matched_button == KButton("exit"):
        msg.reply("Bye / Пока / Adeus" reply_markup=ReplyKeyboardRemove)
    ...

Expanding

All built-in classes have minimal dependency on others. All you need to do is implement abstract class. For example if you need to create button, which send location request you can create that:

from pyrogram import KeyboardButton, Filters
from pyromenu.abc import Button


class LocationButton(Button):
    def __init__(self, text):
        self._text = text

    def __hash__(self):
        return hash(self._text)

    def __eq__(self, other_button):
        return hash(self) == hash(other_button)

    @property
    def keyboard_button(self):
        return KeyboardButton(self._text, request_location=True)

    @property
    def update_filter(self):
        return Filters.create(
            name=f"{self._text}ButtonFilter",
            func=lambda flt, msg: flt.btn_txt == msg.text,
            btn_txt=self._text,
        )

or that:

from pyrogram import KeyboardButton
from pyromenu import KButton

class LocationButton(KButton):
    @property
    def keyboard_button(self):
        btn = super().keyboard_button
        btn.request_location = True
        return btn

and it's will be works well with any built-in classes

Installing

pip3 install pyromenu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyromenu-2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pyromenu-2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file pyromenu-2.0.tar.gz.

File metadata

  • Download URL: pyromenu-2.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.3

File hashes

Hashes for pyromenu-2.0.tar.gz
Algorithm Hash digest
SHA256 e35b8433b8348b6f11828e6fae8631916cc2ddf125c75c6a2e2707bde4a8cef9
MD5 8a98f9b8a1ad5466e9b9cfa79002e3b2
BLAKE2b-256 31b955593799592158a1e6a0406a0863676ef6e18e9e3a66c1f69727a99a1244

See more details on using hashes here.

File details

Details for the file pyromenu-2.0-py3-none-any.whl.

File metadata

  • Download URL: pyromenu-2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.3

File hashes

Hashes for pyromenu-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 582e7f9c8e9050b6b5b9f0249521c2be84e6deb37719afe543eb3f5ab2c99c96
MD5 bd5e69c7565f5fa175289e206b382f6d
BLAKE2b-256 8f481779c023de28bbf69f943f4360c07988f09098360a6e0a36037dbd666468

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page