11Advanced 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
710Telegram Raw API
811----------------
912
1013If 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
0 commit comments