From 01e9cdd2fdfefbf3dfb3ebc997a1643401755082 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 3 Aug 2025 13:55:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D1=81=20BaseFi?= =?UTF-8?q?lter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/README.md | 3 ++- examples/base_filter/main.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 examples/base_filter/main.py 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