v0.3.x · pyrogram_patch

Router

Router groups related handlers into a module. Multiple routers can be included in a PatchManager, each with its own filter prefix.

Defining Handlers

from pyrogram import filters
from pyrogram_patch.router import Router

router = Router()

@router.on_message(filters.command("start"))
async def start(client, message):
    await message.reply("Hello!")

@router.on_callback_query(filters.regex(r"^action:"))
async def handle_action(client, query):
    await query.answer("OK")

@router.on_command("help")
async def cmd_help(client, message):
    await message.reply("Available commands: /start /help")

Handler Decorators

DecoratorUpdate type
@router.on_message(filter)Message
@router.on_callback_query(filter)CallbackQuery
@router.on_inline_query(filter)InlineQuery
@router.on_edited_message(filter)Edited message
@router.on_command(cmd)Message matching /cmd

Router Prefix Filter

from pyrogram import filters

ADMIN_IDS = [123456789]

# Every handler in this router requires the user to be an admin
admin_router = Router(filter=filters.user(ADMIN_IDS))

@admin_router.on_command("ban")
async def ban_user(client, message):
    pass  # only admins reach here

Including Routers

from pyrogram_patch import patch

app = Client("bot", bot_token="TOKEN")
manager = patch(app)

manager.include_router(router)
manager.include_router(admin_router)