Object-oriented way to build telegram keyboard-menus
Project description
Pyromenu
Pyromenu is an easy-to-extend object-oriented library for building keyboard menus for telegram bots on the Pyrogram.
Usage example
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 method for creating Filter for Handlers
@app.on_message(KButon("exit").update_filter())
def remove_keyboard(clt, msg):
msg.reply("Bye / Пока / Adeus", reply_markup=ReplyKeyboardRemove())
How it works?
Pyromenu contains 3 built-in classes KMenu, KRow, KButton, which are implementations of Menu, Row and Button interfaces. Each interface contains a minimal set of methods for implementing keyboard menu creation.
- Button declares 2 methods
keyboard_button
andupdate_filter
, where first return Pyrogram KeyboardButton object, second return Pyromenu Filter object. - Row declares only one method -
keyboard_row
, which return list of KeyboardButton objects. - Menu declares 2 methods
keyboard
andone_time_keyboard
, which returns Pyrogram ReplyKeyboardMarkup, but second return it with the one_time_keyboard attribute turned on.
How to expand?
All built-in classes have minimal dependency on others. All you need to do is implement interface. For example if you need to create button, which send location request you can create that:
from pyrogram import KeyboardButton, Filters
from pyromenu.interfaces import Button
class LocationButton(Button):
def __init__(self, text):
self._text = text
def keyboard_button(self):
return KeyboardButton(self._text, request_location=True)
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):
def keyboard_button(self):
return KeyboardButton(self._text, request_location=True)
and it's will be works well with KRow class, because it wait a Button
-compatible interface.
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.