From b2b7ddb6396fe6e415e4461648466cb7e8148861 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 14 Mar 2021 19:20:37 +0300 Subject: [PATCH] feat: an example of intergration between externally created Application and dispatcher (#433) * feat: an example of intergration between externally created Application and dispatcher * fix: imports * chore: fix EOF * chore: fix comment --- examples/separate_api_route_example.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/separate_api_route_example.py diff --git a/examples/separate_api_route_example.py b/examples/separate_api_route_example.py new file mode 100644 index 00000000..2f3cc61c --- /dev/null +++ b/examples/separate_api_route_example.py @@ -0,0 +1,34 @@ +# NOTE: This is an example of an integration between +# externally created Application object and the aiogram's dispatcher +# This can be used for a custom route, for instance + +from aiogram import Bot, Dispatcher, types +from aiogram.dispatcher.webhook import configure_app +from aiohttp import web + + +bot = Bot(token=config.bot_token) +dp = Dispatcher(bot) + + + +@dp.message_handler(commands=["start"]) +async def cmd_start(message: types.Message): + await message.reply("start!") + + +# handle /api route +async def api_handler(request): + return web.json_response({"status": "OK"}, status=200) + + +app = web.Application() +# add a custom route +app.add_routes([web.post('/api', api_handler)]) +# every request to /bot route will be retransmitted to dispatcher to be handled +# as a bot update +configure_app(dp, app, "/bot") + + +if __name__ == '__main__': + web.run_app(app, port=9000)