๐Ÿ—‚ Routersยถ

Raito uses an extended Router class on top of aiogramโ€™s built-in router. It adds priority-based loading, autoload control, and seamless integration with the hot reload system.


Basic usageยถ

from raito import Router

router = Router(name="my_router")

@router.message()
async def handler(message):
    await message.answer("Hello!")

Raito will automatically discover and register this router if the file is inside routers_dir.


Parametersยถ

router = Router(
    name="my_router",
    priority=10,
    autoload=True,
)
  • name โ€” unique router name; if omitted, Raito derives it from the filename

  • priority โ€” load order: higher values load first (default: 0)

  • autoload โ€” if False, the router is discovered but not loaded on startup (default: True)


Priorityยถ

When multiple routers are present, they load in descending priority order. Routers with the same priority load in alphabetical file order.

# loaded first
router = Router(name="auth", priority=100)
# loaded after auth
router = Router(name="general", priority=0)

This matters when one router has filters or middleware that others depend on.


Autoloadยถ

Set autoload=False to register a router without activating it at startup. You can then load it manually via Telegram commands or RouterLoader.

router = Router(name="debug", autoload=False)
.rt load debug

Naming & conflictsยถ

Each router must have a unique name across routers_dir. If two files expose a router with the same name, Raito renames the second one automatically:

WARNING: Duplicate router name: handler. Will rename to handler_0x7f3a1b2c...

To avoid this, always set an explicit name:

router = Router(name="shop_catalog")

Additional methodsยถ

Router inherits everything from aiogramโ€™s Router and adds:

  • router.on_pagination(name, *filters) โ€” register a pagination callback handler

  • router.on_command_signature_error() โ€” handle incorrect command usage

  • router.lifespan() โ€” define startup/shutdown logic (see ๐Ÿƒ Lifespan)