diff --git a/examples/README.md b/examples/README.md index f980f80..ee5b57a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,4 +8,5 @@ - [Миддлварь в хендлерах](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) \ No newline at end of file + - [Миддлварь в роутерах](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 diff --git a/examples/base_filter/main.py b/examples/base_filter/main.py new file mode 100644 index 0000000..b0a6a66 --- /dev/null +++ b/examples/base_filter/main.py @@ -0,0 +1,38 @@ +import asyncio +import logging + +from maxapi import Bot, Dispatcher +from maxapi.types import MessageCreated, CommandStart, UpdateUnion +from maxapi.filters import BaseFilter + +logging.basicConfig(level=logging.INFO) + +bot = Bot(token='тут_ваш_токен') +dp = Dispatcher() + + +class FilterChat(BaseFilter): + + """ + Фильтр, который срабатывает только в чате с названием `Test` + """ + + async def __call__(self, event: UpdateUnion): + + if not event.chat: + return False + + return event.chat == 'Test' + + +@dp.message_created(CommandStart(), FilterChat()) +async def custom_data(event: MessageCreated): + await event.message.answer('Привет!') + + +async def main(): + await dp.start_polling(bot) + + +if __name__ == '__main__': + asyncio.run(main()) \ No newline at end of file