diff --git a/examples/README.md b/examples/README.md index ee5b57a..21c97bd 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,12 +1,13 @@ ## ⭐️ Примеры - [Эхо бот](https://github.com/love-apples/maxapi/blob/main/examples/echo/main.py) - - [Обработчик доступных событий](https://github.com/love-apples/maxapi/blob/main/examples/events/main.py) - - [Обработчики с MagicFilter](https://github.com/love-apples/maxapi/blob/main/examples/magic_filters/main.py) - - [Демонстрация роутинга, InputMedia и механика контекста](https://github.com/love-apples/maxapi/tree/main/examples/router_with_input_media) (audio.mp3 для команды /media) - - [Получение ID](https://github.com/love-apples/maxapi/tree/main/examples/get_ids/main.py) - - [Миддлварь в хендлерах](https://github.com/love-apples/maxapi/tree/main/examples/middleware_in_handlers/main.py) - - [Вебхуки](https://github.com/love-apples/maxapi/tree/main/examples/webhook) - - [Клавиатуры](https://github.com/love-apples/maxapi/tree/main/examples/keyboard/main.py) - - [Миддлварь в роутерах](https://github.com/love-apples/maxapi/tree/main/examples/middleware_for_router/main.py) - - [BaseFilter](https://github.com/love-apples/maxapi/tree/main/examples/base_filter/main.py) \ No newline at end of file +- [Обработчик доступных событий](https://github.com/love-apples/maxapi/blob/main/examples/events/main.py) +- [Обработчики с MagicFilter](https://github.com/love-apples/maxapi/blob/main/examples/magic_filters/main.py) +- [Демонстрация роутинга, InputMedia и механика контекста](https://github.com/love-apples/maxapi/tree/main/examples/router_with_input_media) (audio.mp3 для команды /media) +- [Получение ID](https://github.com/love-apples/maxapi/tree/main/examples/get_ids/main.py) +- [Миддлварь в хендлерах](https://github.com/love-apples/maxapi/tree/main/examples/middleware_in_handlers/main.py) +- [Вебхуки](https://github.com/love-apples/maxapi/tree/main/examples/webhook) +- [Клавиатуры](https://github.com/love-apples/maxapi/tree/main/examples/keyboard/main.py) +- [Миддлварь в роутерах](https://github.com/love-apples/maxapi/tree/main/examples/middleware_for_router/main.py) +- [Свой фильтр на BaseFilter](https://github.com/love-apples/maxapi/tree/main/examples/base_filter/main.py) +- [Фильтр callback payload](https://github.com/love-apples/maxapi/tree/main/examples/callback_payload/main.py) \ No newline at end of file diff --git a/examples/callback_payload/main.py b/examples/callback_payload/main.py new file mode 100644 index 0000000..86efc98 --- /dev/null +++ b/examples/callback_payload/main.py @@ -0,0 +1,61 @@ +import asyncio +import logging + +from maxapi import Bot, Dispatcher, F +from maxapi.filters.callback_payload import CallbackPayload +from maxapi.filters.command import CommandStart +from maxapi.types import ( + CallbackButton, + MessageCreated, + MessageCallback, +) +from maxapi.utils.inline_keyboard import InlineKeyboardBuilder + +logging.basicConfig(level=logging.INFO) + +bot = Bot('тут_ваш_токен') +dp = Dispatcher() + + +class MyPayload(CallbackPayload, prefix='mypayload'): + foo: str + action: str + + +class AnotherPayload(CallbackPayload, prefix='another'): + bar: str + value: int + + +@dp.message_created(CommandStart()) +async def show_keyboard(event: MessageCreated): + kb = InlineKeyboardBuilder() + kb.row( + CallbackButton( + text='Первая кнопка', + payload=MyPayload(foo='123', action='edit').pack(), + ), + CallbackButton( + text='Вторая кнопка', + payload=AnotherPayload(bar='abc', value=42).pack(), + ), + ) + await event.message.answer('Нажми кнопку!', attachments=[kb.as_markup()]) + + +@dp.message_callback(MyPayload.filter(F.foo == '123')) +async def on_first_callback(event: MessageCallback, payload: MyPayload): + await event.answer(new_text=f'Первая кнопка: foo={payload.foo}, action={payload.action}') + + +@dp.message_callback(AnotherPayload.filter()) +async def on_second_callback(event: MessageCallback, payload: AnotherPayload): + await event.answer(new_text=f'Вторая кнопка: bar={payload.bar}, value={payload.value}') + + +async def main(): + await dp.start_polling(bot) + + +if __name__ == '__main__': + asyncio.run(main())