From fd95745fef4ed56e7371453c72479e1379b28263 Mon Sep 17 00:00:00 2001 From: sdmway Date: Tue, 7 May 2024 20:45:24 -0600 Subject: [PATCH] misc: consistency of comments and dispatcher instance creation --- examples/echo_bot.py | 14 ++++++++++---- examples/echo_bot_webhook.py | 2 +- examples/echo_bot_webhook_ssl.py | 2 +- examples/error_handling.py | 2 +- examples/finite_state_machine.py | 4 ++++ examples/multi_file_bot/bot.py | 3 ++- examples/quiz_scene.py | 8 ++++---- examples/scene.py | 10 +++++++--- examples/specify_updates.py | 2 +- 9 files changed, 31 insertions(+), 16 deletions(-) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 2808de7f..101588c2 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -3,7 +3,7 @@ import logging import sys from os import getenv -from aiogram import Bot, Dispatcher, html +from aiogram import Bot, Dispatcher, Router, html from aiogram.client.default import DefaultBotProperties from aiogram.enums import ParseMode from aiogram.filters import CommandStart @@ -13,10 +13,11 @@ from aiogram.types import Message TOKEN = getenv("BOT_TOKEN") # All handlers should be attached to the Router (or Dispatcher) -dp = Dispatcher() + +echo_router = Router() -@dp.message(CommandStart()) +@echo_router.message(CommandStart()) async def command_start_handler(message: Message) -> None: """ This handler receives messages with `/start` command @@ -29,7 +30,7 @@ async def command_start_handler(message: Message) -> None: await message.answer(f"Hello, {html.bold(message.from_user.full_name)}!") -@dp.message() +@echo_router.message() async def echo_handler(message: Message) -> None: """ Handler will forward receive a message back to the sender @@ -47,6 +48,11 @@ async def echo_handler(message: Message) -> None: async def main() -> None: # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) + + dp = Dispatcher() + + dp.include_router(echo_router) + # And the run events dispatching await dp.start_polling(bot) diff --git a/examples/echo_bot_webhook.py b/examples/echo_bot_webhook.py index 3ecdb85c..056c3f71 100644 --- a/examples/echo_bot_webhook.py +++ b/examples/echo_bot_webhook.py @@ -79,7 +79,7 @@ def main() -> None: # Register startup hook to initialize webhook dp.startup.register(on_startup) - # Initialize Bot instance with a default parse mode which will be passed to all API calls + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) # Create aiohttp.web.Application instance diff --git a/examples/echo_bot_webhook_ssl.py b/examples/echo_bot_webhook_ssl.py index b905af07..45c75015 100644 --- a/examples/echo_bot_webhook_ssl.py +++ b/examples/echo_bot_webhook_ssl.py @@ -90,7 +90,7 @@ def main() -> None: # Register startup hook to initialize webhook dp.startup.register(on_startup) - # Initialize Bot instance with a default parse mode which will be passed to all API calls + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) # Create aiohttp.web.Application instance diff --git a/examples/error_handling.py b/examples/error_handling.py index bfa786ef..fca83f82 100644 --- a/examples/error_handling.py +++ b/examples/error_handling.py @@ -102,7 +102,7 @@ async def handle_set_name(message: types.Message, command: CommandObject) -> Non async def main() -> None: - # Initialize Bot instance with a default parse mode which will be passed to all API calls + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) # And the run events dispatching await dp.start_polling(bot) diff --git a/examples/finite_state_machine.py b/examples/finite_state_machine.py index cb01b88b..f371d2ca 100644 --- a/examples/finite_state_machine.py +++ b/examples/finite_state_machine.py @@ -125,10 +125,14 @@ async def show_summary(message: Message, data: Dict[str, Any], positive: bool = async def main(): + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) + dp = Dispatcher() + dp.include_router(form_router) + # Start event dispatching await dp.start_polling(bot) diff --git a/examples/multi_file_bot/bot.py b/examples/multi_file_bot/bot.py index b8ba4544..0f1ba083 100644 --- a/examples/multi_file_bot/bot.py +++ b/examples/multi_file_bot/bot.py @@ -22,8 +22,9 @@ async def main() -> None: echo_router, ) - # Initialize Bot instance with a default parse mode which will be passed to all API calls + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) + # And the run events dispatching await dp.start_polling(bot) diff --git a/examples/quiz_scene.py b/examples/quiz_scene.py index 721795bc..b07888df 100644 --- a/examples/quiz_scene.py +++ b/examples/quiz_scene.py @@ -289,13 +289,13 @@ def create_dispatcher(): async def main(): - dispatcher = create_dispatcher() + dp = create_dispatcher() bot = Bot(token=TOKEN) - await dispatcher.start_polling(bot) + await dp.start_polling(bot) if __name__ == "__main__": - logging.basicConfig(level=logging.INFO) - asyncio.run(main()) # Alternatively, you can use aiogram-cli: # `aiogram run polling quiz_scene:create_dispatcher --log-level info --token BOT_TOKEN` + logging.basicConfig(level=logging.INFO) + asyncio.run(main()) diff --git a/examples/scene.py b/examples/scene.py index c6cfcc7f..ab83bd44 100644 --- a/examples/scene.py +++ b/examples/scene.py @@ -197,9 +197,13 @@ def create_dispatcher() -> Dispatcher: return dispatcher -if __name__ == "__main__": - # Recommended to use CLI instead of this snippet. - # `aiogram run polling scene_example:create_dispatcher --token BOT_TOKEN --log-level info` +def main() -> None: dp = create_dispatcher() bot = Bot(token=TOKEN) dp.run_polling(bot) + + +if __name__ == "__main__": + # Recommended to use CLI instead of this snippet. + # `aiogram run polling scene_example:create_dispatcher --token BOT_TOKEN --log-level info` + main() diff --git a/examples/specify_updates.py b/examples/specify_updates.py index 89a65d64..c25613a2 100644 --- a/examples/specify_updates.py +++ b/examples/specify_updates.py @@ -79,7 +79,7 @@ async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> Non async def main() -> None: - # Initialize Bot instance with a default parse mode which will be passed to all API calls + # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) dp = Dispatcher()