|
| 1 | +More on Updates |
| 2 | +=============== |
| 3 | + |
| 4 | +Here we'll show some advanced usages when working with updates. |
| 5 | + |
| 6 | +.. note:: |
| 7 | + This page makes use of Handlers and Filters to show you how to handle updates. |
| 8 | + Learn more at `Update Handling <UpdateHandling.html>`_ and `Using Filters <UsingFilters.html>`_. |
| 9 | + |
| 10 | +Handler Groups |
| 11 | +-------------- |
| 12 | + |
| 13 | +If you register handlers with overlapping filters, only the first one is executed and any other handler will be ignored. |
| 14 | + |
| 15 | +In order to process the same update more than once, you can register your handler in a different group. |
| 16 | +Groups are identified by a number (number 0 being the default) and are sorted, that is, a lower group number has a |
| 17 | +higher priority. |
| 18 | + |
| 19 | +For example, in: |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + @app.on_message(Filters.text | Filters.sticker) |
| 24 | + def text_or_sticker(client, message): |
| 25 | + print("Text or Sticker") |
| 26 | +
|
| 27 | +
|
| 28 | + @app.on_message(Filters.text) |
| 29 | + def just_text(client, message): |
| 30 | + print("Just Text") |
| 31 | +
|
| 32 | +``just_text`` is never executed because ``text_or_sticker`` already handles texts. To enable it, simply register the |
| 33 | +function using a different group: |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + @app.on_message(Filters.text, group=1) |
| 38 | + def just_text(client, message): |
| 39 | + print("Just Text") |
| 40 | +
|
| 41 | +Or, if you want ``just_text`` to be fired *before* ``text_or_sticker`` (note ``-1``, which is less than ``0``): |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + @app.on_message(Filters.text, group=-1) |
| 46 | + def just_text(client, message): |
| 47 | + print("Just Text") |
| 48 | +
|
| 49 | +With :meth:`add_handler() <pyrogram.Client.add_handler>` (without decorators) the same can be achieved with: |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + app.add_handler(MessageHandler(just_text, Filters.text), -1) |
| 54 | +
|
| 55 | +Update propagation |
| 56 | +------------------ |
| 57 | + |
| 58 | +Registering multiple handlers, each in a different group, becomes useful when you want to handle the same update more |
| 59 | +than once. Any incoming update will be sequentially processed by all of your registered functions by respecting the |
| 60 | +groups priority policy described above. Even in case any handler raises an unhandled exception, Pyrogram will still |
| 61 | +continue to propagate the same update to the next groups until all the handlers are done. Example: |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | +
|
| 65 | + @app.on_message(Filters.private) |
| 66 | + def _(client, message): |
| 67 | + print(0) |
| 68 | +
|
| 69 | +
|
| 70 | + @app.on_message(Filters.private, group=1) |
| 71 | + def _(client, message): |
| 72 | + print(1 / 0) # Unhandled exception: ZeroDivisionError |
| 73 | +
|
| 74 | +
|
| 75 | + @app.on_message(Filters.private, group=2) |
| 76 | + def _(client, message): |
| 77 | + print(2) |
| 78 | +
|
| 79 | +All these handlers will handle the same kind of messages, that are, messages sent or received in private chats. |
| 80 | +The output for each incoming update will therefore be: |
| 81 | + |
| 82 | +.. code-block:: text |
| 83 | +
|
| 84 | + 0 |
| 85 | + ZeroDivisionError: division by zero |
| 86 | + 2 |
| 87 | +
|
| 88 | +Stop Propagation |
| 89 | +^^^^^^^^^^^^^^^^ |
| 90 | + |
| 91 | +In order to prevent further propagation of an update in the dispatching phase, you can do *one* of the following: |
| 92 | + |
| 93 | +- Call the update's bound-method ``.stop_propagation()`` (preferred way). |
| 94 | +- Manually ``raise StopPropagation`` error (more suitable for raw updates only). |
| 95 | + |
| 96 | +.. note:: |
| 97 | + |
| 98 | + Note that ``.stop_propagation()`` is just an elegant and intuitive way to raise a ``StopPropagation`` error; |
| 99 | + this means that any code coming *after* calling it won't be executed as your function just raised a custom exception |
| 100 | + to signal the dispatcher not to propagate the update anymore. |
| 101 | + |
| 102 | +Example with ``stop_propagation()``: |
| 103 | + |
| 104 | +.. code-block:: python |
| 105 | +
|
| 106 | + @app.on_message(Filters.private) |
| 107 | + def _(client, message): |
| 108 | + print(0) |
| 109 | +
|
| 110 | +
|
| 111 | + @app.on_message(Filters.private, group=1) |
| 112 | + def _(client, message): |
| 113 | + print(1) |
| 114 | + message.stop_propagation() |
| 115 | +
|
| 116 | +
|
| 117 | + @app.on_message(Filters.private, group=2) |
| 118 | + def _(client, message): |
| 119 | + print(2) |
| 120 | +
|
| 121 | +Example with ``raise StopPropagation``: |
| 122 | + |
| 123 | +.. code-block:: python |
| 124 | +
|
| 125 | + from pyrogram import StopPropagation |
| 126 | +
|
| 127 | + @app.on_message(Filters.private) |
| 128 | + def _(client, message): |
| 129 | + print(0) |
| 130 | +
|
| 131 | +
|
| 132 | + @app.on_message(Filters.private, group=1) |
| 133 | + def _(client, message): |
| 134 | + print(1) |
| 135 | + raise StopPropagation |
| 136 | +
|
| 137 | +
|
| 138 | + @app.on_message(Filters.private, group=2) |
| 139 | + def _(client, message): |
| 140 | + print(2) |
| 141 | +
|
| 142 | +The handler in group number 2 will never be executed because the propagation was stopped before. The output of both |
| 143 | +examples will be: |
| 144 | + |
| 145 | +.. code-block:: text |
| 146 | +
|
| 147 | + 0 |
| 148 | + 1 |
0 commit comments