Skip to content

Handlers

Maksym Zavalniuk edited this page Sep 7, 2023 · 1 revision

How to create handlers?

  1. Create a file in the handlers folder.
  2. Import the dp from the loader.py and create your own router:
router = Router()
dp.include_router(router)


@router.message(CommandStart())
async def command_start_handler(message: Message) -> None:
    await message.answer(f"Hello, {hbold(message.from_user.full_name)}!")


@router.message()
async def echo_handler(message: Message, config: Settings) -> None:
    try:
        print(config)
        await message.send_copy(chat_id=message.chat.id)
    except TypeError:
        await message.answer("Nice try!")
  1. Add import in the handlers/__init__.py file:
from . import admin, echo  # noqa
Clone this wiki locally