2018-12-28 15:19:42 +00:00
|
|
|
Advanced Usage
|
|
|
|
==============
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Pyrogram's API, which consists of well documented convenience methods_ and facade types_, exists to provide a much
|
|
|
|
easier interface to the undocumented and often confusing Telegram API.
|
|
|
|
|
|
|
|
In this section, you'll be shown the alternative way of communicating with Telegram using Pyrogram: the main "raw"
|
|
|
|
Telegram API with its functions and types.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
Telegram Raw API
|
|
|
|
----------------
|
|
|
|
|
|
|
|
If you can't find a high-level method for your needs or if you want complete, low-level access to the whole
|
2019-04-12 13:52:06 +00:00
|
|
|
Telegram API, you have to use the raw :mod:`functions <pyrogram.api.functions>` and :mod:`types <pyrogram.api.types>`.
|
|
|
|
|
|
|
|
As already hinted, raw functions and types can be really confusing, mainly because people don't realize soon enough they
|
|
|
|
accept *only* the right types and that all required parameters must be filled in. This section will therefore explain
|
|
|
|
some pitfalls to take into consideration when working with the raw API.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
.. hint::
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Every available high-level methods in Pyrogram is built on top of these raw functions.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
Nothing stops you from using the raw functions only, but they are rather complex and `plenty of them`_ are already
|
2019-04-12 13:52:06 +00:00
|
|
|
re-implemented by providing a much simpler and cleaner interface which is very similar to the Bot API (yet much more
|
|
|
|
powerful).
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
If you think a raw function should be wrapped and added as a high-level method, feel free to ask in our Community_!
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Invoking Functions
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Unlike the methods_ found in Pyrogram's API, which can be called in the usual simple way, functions to be invoked from
|
|
|
|
the raw Telegram API have a different way of usage and are more complex.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
First of all, both `raw functions`_ and `raw types`_ live in their respective packages (and sub-packages):
|
|
|
|
``pyrogram.api.functions``, ``pyrogram.api.types``. They all exist as Python classes, meaning you need to create an
|
|
|
|
instance of each every time you need them and fill them in with the correct values using named arguments.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Next, to actually invoke the raw function you have to use the :meth:`send() <pyrogram.Client.send>` method provided by
|
|
|
|
the Client class and pass the function object you created.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Here's some examples:
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
- Update first name, last name and bio:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
|
|
|
from pyrogram.api import functions
|
|
|
|
|
|
|
|
with Client("my_account") as app:
|
|
|
|
app.send(
|
|
|
|
functions.account.UpdateProfile(
|
|
|
|
first_name="Dan", last_name="Tès",
|
|
|
|
about="Bio written from Pyrogram"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
- Disable links to your account when someone forwards your messages:
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
|
|
|
from pyrogram.api import functions, types
|
|
|
|
|
|
|
|
with Client("my_account") as app:
|
|
|
|
app.send(
|
|
|
|
functions.account.SetPrivacy(
|
2019-04-12 13:52:06 +00:00
|
|
|
key=types.PrivacyKeyForwards(),
|
|
|
|
rules=[types.InputPrivacyValueDisallowAll()]
|
2018-12-28 15:19:42 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
- Invite users to your channel/supergroup:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
|
|
|
from pyrogram.api import functions, types
|
|
|
|
|
|
|
|
with Client("my_account") as app:
|
|
|
|
app.send(
|
|
|
|
functions.channels.InviteToChannel(
|
|
|
|
channel=app.resolve_peer(123456789), # ID or Username
|
|
|
|
users=[ # The users you want to invite
|
|
|
|
app.resolve_peer(23456789), # By ID
|
|
|
|
app.resolve_peer("username"), # By username
|
2019-04-12 13:52:06 +00:00
|
|
|
app.resolve_peer("+393281234567"), # By phone number
|
2018-12-28 15:19:42 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Chat IDs
|
|
|
|
^^^^^^^^
|
|
|
|
|
|
|
|
The way Telegram works makes it impossible to directly send a message to a user or a chat by using their IDs only.
|
|
|
|
Instead, a pair of ``id`` and ``access_hash`` wrapped in a so called ``InputPeer`` is always needed. Pyrogram allows
|
|
|
|
sending messages with IDs only thanks to cached access hashes.
|
|
|
|
|
|
|
|
There are three different InputPeer types, one for each kind of Telegram entity.
|
|
|
|
Whenever an InputPeer is needed you must pass one of these:
|
|
|
|
|
2019-05-16 19:28:34 +00:00
|
|
|
- :obj:`InputPeerUser <../telegram/types/InputPeerUser>` - Users
|
|
|
|
- :obj:`InputPeerChat <../telegram/types/InputPeerChat>` - Basic Chats
|
|
|
|
- :obj:`InputPeerChannel <../telegram/types/InputPeerChannel>` - Either Channels or Supergroups
|
2019-04-12 13:52:06 +00:00
|
|
|
|
|
|
|
But you don't necessarily have to manually instantiate each object because, luckily for you, Pyrogram already provides
|
|
|
|
:meth:`resolve_peer() <pyrogram.Client.resolve_peer>` as a convenience utility method that returns the correct InputPeer
|
|
|
|
by accepting a peer ID only.
|
|
|
|
|
|
|
|
Another thing to take into consideration about chat IDs is the way they are represented: they are all integers and
|
|
|
|
all positive within their respective raw types.
|
|
|
|
|
|
|
|
Things are different when working with Pyrogram's API because having them in the same space can theoretically lead to
|
|
|
|
collisions, and that's why Pyrogram (as well as the official Bot API) uses a slightly different representation for each
|
|
|
|
kind of ID.
|
|
|
|
|
|
|
|
For example, given the ID *123456789*, here's how Pyrogram can tell entities apart:
|
|
|
|
|
|
|
|
- ``+ID`` User: *123456789*
|
|
|
|
- ``-ID`` Chat: *-123456789*
|
|
|
|
- ``-100ID`` Channel (and Supergroup): *-100123456789*
|
|
|
|
|
|
|
|
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
|
|
|
|
high-level method.
|
|
|
|
|
2019-05-13 16:04:44 +00:00
|
|
|
.. _methods: ../api/methods
|
|
|
|
.. _types: ../api/types
|
|
|
|
.. _plenty of them: ../api/methods
|
|
|
|
.. _raw functions: ../telegram/functions
|
|
|
|
.. _raw types: ../telegram/types
|
2018-12-28 15:19:42 +00:00
|
|
|
.. _Community: https://t.me/PyrogramChat
|