docs: migration feeding updates (#1531)

This commit is contained in:
Oleg A 2024-07-06 20:33:01 +03:00 committed by GitHub
parent 7a96067952
commit 5f05dfc664
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 20 deletions

View file

@ -15,5 +15,8 @@ indent_size = 2
[**.{md,txt,rst}] [**.{md,txt,rst}]
trim_trailing_whitespace = false trim_trailing_whitespace = false
[**.rst]
indent_size = 2
[Makefile] [Makefile]
indent_style = tab indent_style = tab

View file

@ -2,7 +2,7 @@
Dispatcher Dispatcher
########## ##########
Dispatcher is root :obj:`Router` and in code Dispatcher can be used directly for routing updates or attach another routers into dispatcher. Dispatcher is root :class:`~aiogram.dispatcher.router.Router` and in code Dispatcher can be used directly for routing updates or attach another routers into dispatcher.
Here is only listed base information about Dispatcher. All about writing handlers, filters and etc. you can find in next pages: Here is only listed base information about Dispatcher. All about writing handlers, filters and etc. you can find in next pages:
@ -39,16 +39,26 @@ Example:
router1 = Router() router1 = Router()
dp.include_router(router1) dp.include_router(router1)
.. _Handling updates:
Handling updates Handling updates
================ ================
All updates can be propagated to the dispatcher by :obj:`Dispatcher.feed_update(bot=..., update=...)` method: All updates can be propagated to the dispatcher by :meth:`~aiogram.dispatcher.dispatcher.Dispatcher.feed_update` method:
.. code-block:: python .. code-block:: python
bot = Bot(...) from aiogram import Bot, Dispatcher
dp = Dispatcher()
... async def update_handler(update: Update, bot: Bot, dispatcher: Dispatcher):
result = await dp.feed_update(bot, update)
result = await dp.feed_update(bot=bot, update=incoming_update) Also you can feed raw update (dictionary) object to the dispatcher by :meth:`~aiogram.dispatcher.dispatcher.Dispatcher.feed_raw_update` method:
.. code-block:: python
from aiogram import Bot, Dispatcher
async def update_handler(raw_update: dict[str, Any], bot: Bot, dispatcher: Dispatcher):
result = await dp.feed_raw_update(bot, raw_update)

View file

@ -1,3 +1,7 @@
.. |Bot| replace:: :class:`~aiogram.client.bot.Bot`
.. |Dispatcher| replace:: :class:`~aiogram.dispatcher.dispatcher.Dispatcher`
.. |Router| replace:: :class:`~aiogram.dispatcher.router.Router`
========================== ==========================
Migration FAQ (2.x -> 3.0) Migration FAQ (2.x -> 3.0)
========================== ==========================
@ -33,24 +37,26 @@ Dependencies
Dispatcher Dispatcher
========== ==========
- The :class:`Dispatcher` class no longer accepts a `Bot` instance in its initializer. - The |Dispatcher| class no longer accepts a |Bot| instance in its initializer.
Instead, the `Bot` instance should be passed to the dispatcher only for starting polling Instead, the |Bot| instance should be passed to the dispatcher only for starting polling
or handling events from webhooks. This approach also allows for the use of multiple bot or handling events from webhooks. This approach also allows for the use of multiple bot
instances simultaneously ("multibot"). instances simultaneously ("multibot").
- :class:`Dispatcher` now can be extended with another Dispatcher-like - |Dispatcher| now can be extended with another Dispatcher-like thing named |Router|.
thing named :class:`Router` (:ref:`Read more » <Nested routers>`). With routes, you can easily modularize your code and potentially share these modules between projects.
- With routes, you can easily modularize your code and potentially share these modules between projects. (:ref:`Read more » <Nested routers>`.)
- Removed the **_handler** suffix from all event handler decorators and registering methods. - Removed the **_handler** suffix from all event handler decorators and registering methods.
(:ref:`Read more » <Event observers>`) (:ref:`Read more » <Event observers>`)
- The Executor has been entirely removed; you can now use the Dispatcher directly to start poll the API or handle webhooks from it. - The :class:`Executor` has been entirely removed; you can now use the |Dispatcher| directly to start poll the API or handle webhooks from it.
- The throttling method has been completely removed; you can now use middlewares to control - The throttling method has been completely removed; you can now use middlewares to control
the execution context and implement any throttling mechanism you desire. the execution context and implement any throttling mechanism you desire.
- Removed global context variables from the API types, Bot and Dispatcher object, - Removed global context variables from the API types, |Bot| and |Dispatcher| object.
From now on, if you want to access the current bot instance within handlers or filters, From now on, if you want to access the current bot instance within handlers or filters,
you should accept the argument :code:`bot: Bot` and use it instead of :code:`Bot.get_current()`. you should accept the argument :code:`bot: Bot` and use it instead of :code:`Bot.get_current()`.
In middlewares, it can be accessed via :code:`data["bot"]`. In middlewares, it can be accessed via :code:`data["bot"]`.
- To skip pending updates, you should now call the :class:`aiogram.methods.delete_webhook.DeleteWebhook` method directly, rather than passing :code:`skip_updates=True` to the start polling method. - To skip pending updates, you should now call the :class:`~aiogram.methods.delete_webhook.DeleteWebhook` method directly, rather than passing :code:`skip_updates=True` to the start polling method.
- To feed updates to the |Dispatcher|, instead of method :meth:`process_update`,
you should use method :meth:`~aiogram.dispatcher.dispatcher.Dispatcher.feed_update`.
(:ref:`Read more » <Handling updates>`)
Filtering events Filtering events
@ -128,11 +134,10 @@ Finite State machine
you will need to specify the state if you want to use it. you will need to specify the state if you want to use it.
- Added the possibility to change the FSM strategy. For example, - Added the possibility to change the FSM strategy. For example,
if you want to control the state for each user based on chat topics rather than if you want to control the state for each user based on chat topics rather than
the user in a chat, you can specify this in the Dispatcher. the user in a chat, you can specify this in the |Dispatcher|.
- Now :class:`aiogram.fsm.state.State` and :class:`aiogram.fsm.state.StateGroup` don't have helper - Now :class:`aiogram.fsm.state.State` and :class:`aiogram.fsm.state.StateGroup` don't have helper
methods like :code:`.set()`, :code:`.next()`, etc. methods like :code:`.set()`, :code:`.next()`, etc.
Instead, you should set states by passing them directly to
- Instead, you should set states by passing them directly to
:class:`aiogram.fsm.context.FSMContext` (:ref:`Read more » <Finite State Machine>`) :class:`aiogram.fsm.context.FSMContext` (:ref:`Read more » <Finite State Machine>`)
- The state proxy is deprecated; you should update the state data by calling - The state proxy is deprecated; you should update the state data by calling
:code:`state.set_data(...)` and :code:`state.get_data()` respectively. :code:`state.set_data(...)` and :code:`state.get_data()` respectively.
@ -155,8 +160,8 @@ Webhook
Telegram API Server Telegram API Server
=================== ===================
- The `server` parameter has been moved from the `Bot` instance to `api` in `BaseSession`. - The :obj:`server` parameter has been moved from the |Bot| instance to :obj:`api` parameter of the :class:`~aiogram.client.session.base.BaseSession`.
- The constant `aiogram.bot.api.TELEGRAM_PRODUCTION` has been moved to `aiogram.client.telegram.PRODUCTION`. - The constant :obj:`aiogram.bot.api.TELEGRAM_PRODUCTION` has been moved to :obj:`aiogram.client.telegram.PRODUCTION`.
Telegram objects transformation (to dict, to json, from json) Telegram objects transformation (to dict, to json, from json)