mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-16 20:59:29 +00:00
Update docs
This commit is contained in:
parent
f9b1d02af4
commit
0295954d66
@ -3,7 +3,10 @@ Update Handling
|
|||||||
|
|
||||||
Updates are events that happen in your Telegram account (incoming messages, new channel posts, new members join, ...)
|
Updates are events that happen in your Telegram account (incoming messages, new channel posts, new members join, ...)
|
||||||
and are handled by registering one or more callback functions with an Handler. There are multiple Handlers to choose
|
and are handled by registering one or more callback functions with an Handler. There are multiple Handlers to choose
|
||||||
from, one for each kind of update.
|
from, one for each kind of update:
|
||||||
|
|
||||||
|
- `MessageHandler <../pyrogram/MessageHandler.html>`_
|
||||||
|
- `RawUpdateHandler <../pyrogram/RawUpdateHandler.html>`_
|
||||||
|
|
||||||
Registering an Handler
|
Registering an Handler
|
||||||
----------------------
|
----------------------
|
||||||
@ -31,7 +34,7 @@ We shall examine the :obj:`MessageHandler <pyrogram.MessageHandler>`, which will
|
|||||||
app.idle()
|
app.idle()
|
||||||
|
|
||||||
- If you prefer not to use decorators, there is an alternative way for registering Handlers.
|
- If you prefer not to use decorators, there is an alternative way for registering Handlers.
|
||||||
This is useful, for example, when you want to keep your callback functions in a separate file.
|
This is useful, for example, when you want to keep your callback functions in separate files.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -55,8 +58,8 @@ Using Filters
|
|||||||
For a finer grained control over what kind of messages will be allowed or not in your callback functions, you can use
|
For a finer grained control over what kind of messages will be allowed or not in your callback functions, you can use
|
||||||
:class:`Filters <pyrogram.Filters>`.
|
:class:`Filters <pyrogram.Filters>`.
|
||||||
|
|
||||||
- This example will show you how to handle only messages containing an :obj:`Audio <pyrogram.api.types.pyrogram.Audio>`
|
- This example will show you how to **only** handle messages containing an
|
||||||
object:
|
:obj:`Audio <pyrogram.api.types.pyrogram.Audio>` object and filter out any other message:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -71,7 +74,7 @@ For a finer grained control over what kind of messages will be allowed or not in
|
|||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from pyrogram import Filters, Messagehandler
|
from pyrogram import Filters, MessageHandler
|
||||||
|
|
||||||
|
|
||||||
def my_handler(client, message):
|
def my_handler(client, message):
|
||||||
@ -128,7 +131,7 @@ can also accept arguments:
|
|||||||
def my_handler(client, message):
|
def my_handler(client, message):
|
||||||
print(message)
|
print(message)
|
||||||
|
|
||||||
More handlers using different filters can also live together:
|
More handlers using different filters can also live together.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -145,3 +148,41 @@ More handlers using different filters can also live together:
|
|||||||
@app.on_message(Filters.chat("PyrogramChat"))
|
@app.on_message(Filters.chat("PyrogramChat"))
|
||||||
def from_pyrogramchat(client, message):
|
def from_pyrogramchat(client, message):
|
||||||
print("New message in @PyrogramChat")
|
print("New message in @PyrogramChat")
|
||||||
|
|
||||||
|
Handler Groups
|
||||||
|
--------------
|
||||||
|
|
||||||
|
If you register handlers with overlapping filters, only the first one is executed and any other handler will be ignored.
|
||||||
|
|
||||||
|
In order to process the same message more than once, you can register your handler in a different group.
|
||||||
|
Groups are identified by a number (number 0 being the default) and are sorted. This means that a lower group number has
|
||||||
|
a higher priority.
|
||||||
|
|
||||||
|
For example, in:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
@app.on_message(Filters.text | Filters.sticker)
|
||||||
|
def text_or_sticker(client, message):
|
||||||
|
print("Text or Sticker")
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_message(Filters.text)
|
||||||
|
def just_text(client, message):
|
||||||
|
print("Just Text")
|
||||||
|
|
||||||
|
``just_text`` is never executed. To enable it, simply register the function using a different group:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
@app.on_message(Filters.text, group=1)
|
||||||
|
def just_text(client, message):
|
||||||
|
print("Just Text")
|
||||||
|
|
||||||
|
or, if you want ``just_text`` to be fired *before* ``text_or_sticker``:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
@app.on_message(Filters.text, group=-1)
|
||||||
|
def just_text(client, message):
|
||||||
|
print("Just Text")
|
Loading…
Reference in New Issue
Block a user