From dd6c8ff9ea518412b53c4013c31fa0cd13df2b55 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 20 Jul 2025 21:46:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=BD?= =?UTF-8?q?=D0=B8=D0=B7=D0=BA=D0=BB=D1=83=D1=80=D0=BE=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=B2=D0=B5=D0=B1=D1=85=D1=83=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/webhook/low_level.py | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/webhook/low_level.py diff --git a/examples/webhook/low_level.py b/examples/webhook/low_level.py new file mode 100644 index 0000000..2bb2f68 --- /dev/null +++ b/examples/webhook/low_level.py @@ -0,0 +1,55 @@ +import asyncio +import logging + +from fastapi import Request +from fastapi.responses import JSONResponse + +from maxapi import Bot, Dispatcher +from maxapi.methods.types.getted_updates import process_update_webhook +from maxapi.types import MessageCreated +from maxapi.dispatcher import webhook_app + +logging.basicConfig(level=logging.INFO) + +bot = Bot('тут_ваш_токен') +dp = Dispatcher() + + +@dp.message_created() +async def handle_message(event: MessageCreated): + await event.message.answer('Бот работает через вебхук!') + +# Регистрация обработчика +# для вебхука +@webhook_app.post('/') +async def _(request: Request): + + # Сериализация полученного запроса + event_json = await request.json() + + # Десериализация полученного запроса + # в pydantic + event_object = await process_update_webhook( + event_json=event_json, + bot=bot + ) + + # ...свой код + print(f'Информация из вебхука: {event_json}') + # ...свой код + + # Окончательная обработка запроса + await dp.handle(event_object) + + # Ответ вебхука + return JSONResponse(content={'ok': True}, status_code=200) + + +async def main(): + + # Запуск сервера + await dp.init_serve(bot, log_level='critical') + + +if __name__ == '__main__': + asyncio.run(main())