Skip to content

Commit d49aed3

Browse files
committed
Merge branch 'develop' into asyncio
# Conflicts: # pyrogram/__init__.py
2 parents 95a7bef + 05aed5e commit d49aed3

19 files changed

Lines changed: 251 additions & 227 deletions

docs/source/conf.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
# Import after sys.path.insert() to avoid issues
2626
from pyrogram import __version__
2727

28+
from pygments.styles.friendly import FriendlyStyle
29+
30+
FriendlyStyle.background_color = "#f3f2f1"
31+
2832
# -- General configuration ------------------------------------------------
2933

3034
# If your documentation needs a minimal Sphinx version, state it here.
@@ -60,7 +64,7 @@
6064

6165
# General information about the project.
6266
project = 'Pyrogram'
63-
copyright = '2017-2018, Dan Tès'
67+
copyright = '2017-2019, Dan Tès'
6468
author = 'Dan Tès'
6569

6670
# The version info for the project you're documenting, acts as replacement for
@@ -85,7 +89,7 @@
8589
exclude_patterns = []
8690

8791
# The name of the Pygments (syntax highlighting) style to use.
88-
pygments_style = 'tango'
92+
pygments_style = 'friendly'
8993

9094
# If true, `todo` and `todoList` produce output, else they produce nothing.
9195
todo_include_todos = False

docs/source/pyrogram/Handlers.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Handlers
99
MessageHandler
1010
DeletedMessagesHandler
1111
CallbackQueryHandler
12+
InlineQueryHandler
1213
UserStatusHandler
1314
DisconnectHandler
1415
RawUpdateHandler
@@ -22,6 +23,9 @@ Handlers
2223
.. autoclass:: CallbackQueryHandler
2324
:members:
2425

26+
.. autoclass:: InlineQueryHandler
27+
:members:
28+
2529
.. autoclass:: UserStatusHandler
2630
:members:
2731

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,46 @@
11
Advanced Usage
22
==============
33

4-
In this section, you'll be shown the alternative way of communicating with Telegram using Pyrogram: the main Telegram
5-
API with its raw functions and types.
4+
Pyrogram's API, which consists of well documented convenience methods_ and facade types_, exists to provide a much
5+
easier interface to the undocumented and often confusing Telegram API.
6+
7+
In this section, you'll be shown the alternative way of communicating with Telegram using Pyrogram: the main "raw"
8+
Telegram API with its functions and types.
69

710
Telegram Raw API
811
----------------
912

1013
If you can't find a high-level method for your needs or if you want complete, low-level access to the whole
11-
Telegram API, you have to use the raw :mod:`functions <pyrogram.api.functions>` and :mod:`types <pyrogram.api.types>`
12-
exposed by the ``pyrogram.api`` package and call any Telegram API method you wish using the
13-
:meth:`send() <pyrogram.Client.send>` method provided by the Client class.
14+
Telegram API, you have to use the raw :mod:`functions <pyrogram.api.functions>` and :mod:`types <pyrogram.api.types>`.
15+
16+
As already hinted, raw functions and types can be really confusing, mainly because people don't realize soon enough they
17+
accept *only* the right types and that all required parameters must be filled in. This section will therefore explain
18+
some pitfalls to take into consideration when working with the raw API.
1419

1520
.. hint::
1621

17-
Every available high-level method mentioned in the previous page is built on top of these raw functions.
22+
Every available high-level methods in Pyrogram is built on top of these raw functions.
1823

1924
Nothing stops you from using the raw functions only, but they are rather complex and `plenty of them`_ are already
20-
re-implemented by providing a much simpler and cleaner interface which is very similar to the Bot API.
25+
re-implemented by providing a much simpler and cleaner interface which is very similar to the Bot API (yet much more
26+
powerful).
2127

2228
If you think a raw function should be wrapped and added as a high-level method, feel free to ask in our Community_!
2329

24-
Caveats
25-
-------
26-
27-
As hinted before, raw functions and types can be confusing, mainly because people don't realize they must accept
28-
*exactly* the right values, but also because most of them don't have enough Python experience to fully grasp how things
29-
work.
30-
31-
This section will therefore explain some pitfalls to take into consideration when working with the raw API.
30+
Invoking Functions
31+
^^^^^^^^^^^^^^^^^^
3232

33-
Chat IDs
34-
^^^^^^^^
35-
36-
The way Telegram works makes it impossible to directly send a message to a user or a chat by using their IDs only.
37-
Instead, a pair of ``id`` and ``access_hash`` wrapped in a so called ``InputPeer`` is always needed.
33+
Unlike the methods_ found in Pyrogram's API, which can be called in the usual simple way, functions to be invoked from
34+
the raw Telegram API have a different way of usage and are more complex.
3835

39-
There are three different InputPeer types, one for each kind of Telegram entity.
40-
Whenever an InputPeer is needed you must pass one of these:
36+
First of all, both `raw functions`_ and `raw types`_ live in their respective packages (and sub-packages):
37+
``pyrogram.api.functions``, ``pyrogram.api.types``. They all exist as Python classes, meaning you need to create an
38+
instance of each every time you need them and fill them in with the correct values using named arguments.
4139

42-
- `InputPeerUser <https://docs.pyrogram.ml/types/InputPeerUser>`_ - Users
43-
- `InputPeerChat <https://docs.pyrogram.ml/types/InputPeerChat>`_ - Basic Chats
44-
- `InputPeerChannel <https://docs.pyrogram.ml/types/InputPeerChannel>`_ - Either Channels or Supergroups
40+
Next, to actually invoke the raw function you have to use the :meth:`send() <pyrogram.Client.send>` method provided by
41+
the Client class and pass the function object you created.
4542

46-
But you don't necessarily have to manually instantiate each object because, luckily for you, Pyrogram already provides
47-
:meth:`resolve_peer() <pyrogram.Client.resolve_peer>` as a convenience utility method that returns the correct InputPeer
48-
by accepting a peer ID only.
49-
50-
Another thing to take into consideration about chat IDs is the way they are represented: they are all integers and
51-
all positive within their respective raw types.
52-
53-
Things are different when working with Pyrogram's API because having them in the same space can theoretically lead to
54-
collisions, and that's why Pyrogram (as well as the official Bot API) uses a slightly different representation for each
55-
kind of ID.
56-
57-
For example, given the ID *123456789*, here's how Pyrogram can tell entities apart:
58-
59-
- ``+ID`` - User: *123456789*
60-
- ``-ID`` - Chat: *-123456789*
61-
- ``-100ID`` - Channel (and Supergroup): *-100123456789*
62-
63-
So, every time you take a raw ID, make sure to translate it into the correct ID when you want to use it with an
64-
high-level method.
65-
66-
Examples
67-
--------
43+
Here's some examples:
6844

6945
- Update first name, last name and bio:
7046

@@ -81,7 +57,7 @@ Examples
8157
)
8258
)
8359
84-
- Share your Last Seen time only with your contacts:
60+
- Disable links to your account when someone forwards your messages:
8561

8662
.. code-block:: python
8763
@@ -91,8 +67,8 @@ Examples
9167
with Client("my_account") as app:
9268
app.send(
9369
functions.account.SetPrivacy(
94-
key=types.InputPrivacyKeyStatusTimestamp(),
95-
rules=[types.InputPrivacyValueAllowContacts()]
70+
key=types.PrivacyKeyForwards(),
71+
rules=[types.InputPrivacyValueDisallowAll()]
9672
)
9773
)
9874
@@ -110,12 +86,51 @@ Examples
11086
users=[ # The users you want to invite
11187
app.resolve_peer(23456789), # By ID
11288
app.resolve_peer("username"), # By username
113-
app.resolve_peer("393281234567"), # By phone number
89+
app.resolve_peer("+393281234567"), # By phone number
11490
]
11591
)
11692
)
11793
94+
Chat IDs
95+
^^^^^^^^
96+
97+
The way Telegram works makes it impossible to directly send a message to a user or a chat by using their IDs only.
98+
Instead, a pair of ``id`` and ``access_hash`` wrapped in a so called ``InputPeer`` is always needed. Pyrogram allows
99+
sending messages with IDs only thanks to cached access hashes.
100+
101+
There are three different InputPeer types, one for each kind of Telegram entity.
102+
Whenever an InputPeer is needed you must pass one of these:
103+
104+
- `InputPeerUser <https://docs.pyrogram.ml/types/InputPeerUser>`_ - Users
105+
- `InputPeerChat <https://docs.pyrogram.ml/types/InputPeerChat>`_ - Basic Chats
106+
- `InputPeerChannel <https://docs.pyrogram.ml/types/InputPeerChannel>`_ - Either Channels or Supergroups
107+
108+
But you don't necessarily have to manually instantiate each object because, luckily for you, Pyrogram already provides
109+
:meth:`resolve_peer() <pyrogram.Client.resolve_peer>` as a convenience utility method that returns the correct InputPeer
110+
by accepting a peer ID only.
111+
112+
Another thing to take into consideration about chat IDs is the way they are represented: they are all integers and
113+
all positive within their respective raw types.
114+
115+
Things are different when working with Pyrogram's API because having them in the same space can theoretically lead to
116+
collisions, and that's why Pyrogram (as well as the official Bot API) uses a slightly different representation for each
117+
kind of ID.
118+
119+
For example, given the ID *123456789*, here's how Pyrogram can tell entities apart:
120+
121+
- ``+ID`` User: *123456789*
122+
- ``-ID`` Chat: *-123456789*
123+
- ``-100ID`` Channel (and Supergroup): *-100123456789*
124+
125+
So, every time you take a raw ID, make sure to translate it into the correct ID when you want to use it with an
126+
high-level method.
127+
128+
129+
118130

131+
.. _methods: ../pyrogram/Client.html#messages
132+
.. _types: ../pyrogram/Types.html
119133
.. _plenty of them: ../pyrogram/Client.html#messages
120-
.. _Raw Functions: Usage.html#using-raw-functions
134+
.. _raw functions: ../pyrogram/functions
135+
.. _raw types: ../pyrogram/types
121136
.. _Community: https://t.me/PyrogramChat

docs/source/resources/ConfigurationFile.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Configuration File
22
==================
33

4-
As already mentioned in previous sections, Pyrogram can also be configured by the use of an INI file.
4+
As already mentioned in previous sections, Pyrogram can be configured by the use of an INI file.
55
This page explains how this file is structured in Pyrogram, how to use it and why.
66

77
Introduction
88
------------
99

10-
The idea behind using a configuration file is to help keeping your code free of settings (private) information such as
10+
The idea behind using a configuration file is to help keeping your code free of private settings information such as
1111
the API Key and Proxy without having you to even deal with how to load such settings. The configuration file, usually
1212
referred as ``config.ini`` file, is automatically loaded from the root of your working directory; all you need to do is
1313
fill in the necessary parts.
@@ -46,7 +46,7 @@ These are all the sections Pyrogram uses in its configuration file:
4646
Pyrogram
4747
^^^^^^^^
4848

49-
The ``[pyrogram]`` section contains your Telegram API credentials *api_id* and *api_hash*.
49+
The ``[pyrogram]`` section contains your Telegram API credentials: *api_id* and *api_hash*.
5050

5151
.. code-block:: ini
5252

docs/source/resources/CustomizeSessions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
Customize Sessions
22
==================
33

4-
As you may probably know, Telegram allows Users (and Bots) having more than one session (authorizations) registered
4+
As you may probably know, Telegram allows users (and bots) having more than one session (authorizations) registered
55
in the system at the same time.
66

77
Briefly explaining, sessions are simply new logins in your account. They can be reviewed in the settings of an official
8-
app (or by invoking `GetAuthorizations <../functions/account/GetAuthorizations.html>`_ with Pyrogram) and store some useful
9-
information about the client who generated them.
8+
app (or by invoking `GetAuthorizations <../functions/account/GetAuthorizations.html>`_ with Pyrogram). They store some
9+
useful information such as the client who's using them and from which country and IP address.
1010

1111

1212
.. figure:: https://i.imgur.com/lzGPCdZ.png
1313
:width: 70%
1414
:align: center
1515

16-
A Pyrogram session running on Linux, Python 3.6.
16+
**A Pyrogram session running on Linux, Python 3.6.**
1717

1818
That's how a session looks like on the Android app, showing the three main pieces of information.
1919

docs/source/resources/ErrorHandling.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Error Handling
44
Errors are inevitable when working with the API, and they must be correctly handled with ``try..except`` blocks.
55

66
There are many errors that Telegram could return, but they all fall in one of these categories
7-
(which are in turn children of the :obj:`RPCError <pyrogram.RPCError>` superclass)
7+
(which are in turn children of the :obj:`RPCError <pyrogram.RPCError>` superclass):
88

99
- :obj:`303 - See Other <pyrogram.errors.SeeOther>`
1010
- :obj:`400 - Bad Request <pyrogram.errors.BadRequest>`

docs/source/resources/MoreOnUpdates.rst

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
More on Updates
22
===============
33

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>`_.
4+
Here we'll show some advanced usages when working with `update handlers`_ and `filters`_.
95

106
Handler Groups
117
--------------
128

13-
If you register handlers with overlapping filters, only the first one is executed and any other handler will be ignored.
9+
If you register handlers with overlapping (conflicting) filters, only the first one is executed and any other handler
10+
will be ignored. This is intended by design.
1411

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.
12+
In order to handle the very same update more than once, you have to register your handler in a different dispatching
13+
group. Dispatching groups hold one or more handlers and are processed sequentially, they are identified by a number
14+
(number 0 being the default) and sorted, that is, a lower group number has a higher priority:
1815

19-
For example, in:
16+
For example, take these two handlers:
2017

2118
.. code-block:: python
19+
:emphasize-lines: 1, 6
2220
2321
@app.on_message(Filters.text | Filters.sticker)
2422
def text_or_sticker(client, message):
@@ -29,8 +27,8 @@ For example, in:
2927
def just_text(client, message):
3028
print("Just Text")
3129
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:
30+
Here, ``just_text`` is never executed because ``text_or_sticker``, which has been registered first, already handles
31+
texts (``Filters.text`` is shared and conflicting). To enable it, register the function using a different group:
3432

3533
.. code-block:: python
3634
@@ -69,7 +67,7 @@ continue to propagate the same update to the next groups until all the handlers
6967
7068
@app.on_message(Filters.private, group=1)
7169
def _(client, message):
72-
print(1 / 0) # Unhandled exception: ZeroDivisionError
70+
raise Exception("Unhandled exception!") # Simulate an unhandled exception
7371
7472
7573
@app.on_message(Filters.private, group=2)
@@ -82,7 +80,7 @@ The output for each incoming update will therefore be:
8280
.. code-block:: text
8381
8482
0
85-
ZeroDivisionError: division by zero
83+
Exception: Unhandled exception!
8684
2
8785
8886
Stop Propagation
@@ -153,9 +151,9 @@ Continue Propagation
153151

154152
As opposed to `stopping the update propagation <#stop-propagation>`_ and also as an alternative to the
155153
`handler groups <#handler-groups>`_, you can signal the internal dispatcher to continue the update propagation within
156-
the group regardless of the next handler's filters. This allows you to register multiple handlers with overlapping
157-
filters in the same group; to let the dispatcher process the next handler you can do *one* of the following in each
158-
handler you want to grant permission to continue:
154+
**the same group** regardless of the next handler's filters. This allows you to register multiple handlers with
155+
overlapping filters in the same group; to let the dispatcher process the next handler you can do *one* of the following
156+
in each handler you want to grant permission to continue:
159157

160158
- Call the update's bound-method ``.continue_propagation()`` (preferred way).
161159
- Manually ``raise ContinuePropagation`` exception (more suitable for raw updates only).
@@ -218,4 +216,7 @@ The output of both (equivalent) examples will be:
218216
219217
0
220218
1
221-
2
219+
2
220+
221+
.. _`update handlers`: UpdateHandling.html
222+
.. _`filters`: UsingFilters.html

0 commit comments

Comments
 (0)