diff --git a/CHANGES.rst b/CHANGES.rst index 048d5509..37174798 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -19,6 +19,35 @@ Changelog 3.0.0b7 (2023-02-18) ===================== +.. warning:: + + Note that this version has incompatibility with Python 3.8-3.10 in case when you create an instance of Dispatcher outside of the any coroutine. + + Sorry for the inconvenience, it will be fixed in the next version. + + This code will not work: + + .. code-block:: python + dp = Dispatcher() + + def main(): + dp.run_polling() + + main() + + But if you change it like this it should works as well: + + .. code-block:: python + + router = Router() + + async def main(): + dp = Dispatcher() + dp.include_router(router) + + asyncio.run(main()) + + Features -------- diff --git a/Makefile b/Makefile index 5696616b..66dd4eba 100644 --- a/Makefile +++ b/Makefile @@ -119,4 +119,4 @@ prepare-release: bump towncrier-build release: git add . git commit -m "Release $(shell poetry version -s)" - git tag v$(shell poetry version -s) + git tag v$(shell hatch version -s) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 29cf3b52..d51a0eeb 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -1,16 +1,18 @@ +import asyncio import logging -from aiogram import Bot, Dispatcher, types +from aiogram import Bot, Dispatcher, Router, types from aiogram.filters import Command from aiogram.types import Message +# Bot token can be obtained via https://t.me/BotFahter TOKEN = "42:TOKEN" -dp = Dispatcher() -logger = logging.getLogger(__name__) +# All handlers should be attached to the Router (or Dispatcher) +router = Router() -@dp.message(Command(commands=["start"])) +@router.message(Command(commands=["start"])) async def command_start_handler(message: Message) -> None: """ This handler receive messages with `/start` command @@ -23,7 +25,7 @@ async def command_start_handler(message: Message) -> None: await message.answer(f"Hello, {message.from_user.full_name}!") -@dp.message() +@router.message() async def echo_handler(message: types.Message) -> None: """ Handler will forward received message back to the sender @@ -38,12 +40,18 @@ async def echo_handler(message: types.Message) -> None: await message.answer("Nice try!") -def main() -> None: - # Initialize Bot instance with an default parse mode which will be passed to all API calls +async def main() -> None: + # Dispatcher is a root router + dp = Dispatcher() + # ... and all other routers should be attached to Dispatcher + dp.include_router(router) + + # Initialize Bot instance with a default parse mode which will be passed to all API calls bot = Bot(TOKEN, parse_mode="HTML") # And the run events dispatching - dp.run_polling(bot) + await dp.start_polling(bot) if __name__ == "__main__": - main() + logging.basicConfig(level=logging.INFO) + asyncio.run(main()) diff --git a/examples/specify_updates.py b/examples/specify_updates.py index 6a6abb65..f9380b91 100644 --- a/examples/specify_updates.py +++ b/examples/specify_updates.py @@ -1,3 +1,4 @@ +import asyncio import logging from aiogram import Bot, Dispatcher, Router @@ -11,13 +12,14 @@ from aiogram.types import ( ) TOKEN = "6wo" -dp = Dispatcher() logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) +router = Router() -@dp.message(Command(commands=["start"])) + +@router.message(Command(commands=["start"])) async def command_start_handler(message: Message) -> None: """ This handler receive messages with `/start` command @@ -31,7 +33,7 @@ async def command_start_handler(message: Message) -> None: ) -@dp.chat_member() +@router.chat_member() async def chat_member_update(chat_member: ChatMemberUpdated, bot: Bot) -> None: await bot.send_message( chat_member.chat.id, @@ -71,20 +73,22 @@ async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> Non ) -def main() -> None: +async def main() -> None: # Initialize Bot instance with a default parse mode which will be passed to all API calls bot = Bot(TOKEN, parse_mode="HTML") + dp = Dispatcher() + dp.include_router(router) sub_router.include_router(deep_dark_router) - - dp.include_router(sub_router) - dp.include_router(sub_sub_router) + router.include_router(sub_router) + router.include_router(sub_sub_router) useful_updates = dp.resolve_used_update_types() # And the run events dispatching - dp.run_polling(bot, allowed_updates=useful_updates) + await dp.start_polling(bot, allowed_updates=useful_updates) if __name__ == "__main__": - main() + logging.basicConfig(level=logging.INFO) + asyncio.run(main())