2018-12-28 15:19:42 +00:00
|
|
|
Advanced Usage
|
|
|
|
==============
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
Pyrogram's API -- which consists of well documented :doc:`methods <../api/methods/index>` and
|
|
|
|
:doc:`types <../api/types/index>` -- exists to provide an easier interface to the more complex Telegram API.
|
2019-04-12 13:52:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-01 18:08:46 +00:00
|
|
|
.. contents:: Contents
|
|
|
|
:backlinks: none
|
2020-08-22 06:05:05 +00:00
|
|
|
:depth: 1
|
2020-04-01 18:08:46 +00:00
|
|
|
:local:
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
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
|
2020-08-22 06:05:05 +00:00
|
|
|
Telegram API, you have to use the raw :mod:`~pyrogram.raw.functions` and :mod:`~pyrogram.raw.types`.
|
2019-04-12 13:52:06 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
As already hinted, raw functions and types can be less convenient. 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
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
.. tip::
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
Every available high-level method in Pyrogram is built on top of these raw functions.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Invoking Functions
|
2022-01-07 09:18:51 +00:00
|
|
|
------------------
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-07-09 18:02:51 +00:00
|
|
|
Unlike the :doc:`methods <../api/methods/index>` found in Pyrogram's API, which can be called in the usual simple way,
|
2022-01-07 09:18:51 +00:00
|
|
|
functions to be invoked from the raw Telegram API have a different way of usage.
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2019-07-09 18:02:51 +00:00
|
|
|
First of all, both :doc:`raw functions <../telegram/functions/index>` and :doc:`raw types <../telegram/types/index>`
|
2020-08-22 06:05:05 +00:00
|
|
|
live in their respective packages (and sub-packages): ``pyrogram.raw.functions``, ``pyrogram.raw.types``. They all exist
|
2019-07-09 18:02:51 +00:00
|
|
|
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-05-28 14:41:55 +00:00
|
|
|
Next, to actually invoke the raw function you have to use the :meth:`~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
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram.raw import functions
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
with Client("my_account") as app:
|
|
|
|
app.send(
|
|
|
|
functions.account.UpdateProfile(
|
2022-01-07 09:18:51 +00:00
|
|
|
first_name="First Name", last_name="Last Name",
|
|
|
|
about="New bio text"
|
2018-12-28 15:19:42 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
- Set online/offline status:
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram.raw import functions, types
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
with Client("my_account") as app:
|
2022-01-07 09:18:51 +00:00
|
|
|
# Set online status
|
|
|
|
app.send(functions.account.UpdateStatus(offline=False))
|
|
|
|
|
|
|
|
# Set offline status
|
|
|
|
app.send(functions.account.UpdateStatus(offline=True))
|
2018-12-28 15:19:42 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
- Get chat info:
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram.raw import functions, types
|
2018-12-28 15:19:42 +00:00
|
|
|
|
|
|
|
with Client("my_account") as app:
|
2022-01-07 09:18:51 +00:00
|
|
|
r = app.send(
|
|
|
|
functions.channels.GetFullChannel(
|
|
|
|
channel=app.resolve_peer("username")
|
2018-12-28 15:19:42 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
print(r)
|
|
|
|
|
2019-04-12 13:52:06 +00:00
|
|
|
Chat IDs
|
2022-01-07 09:18:51 +00:00
|
|
|
--------
|
2019-04-12 13:52:06 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
The way Telegram works makes it not possible to directly send a message to a user or a chat by using their IDs only.
|
2019-04-12 13:52:06 +00:00
|
|
|
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:
|
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
- :class:`~pyrogram.raw.types.InputPeerUser` - Users
|
|
|
|
- :class:`~pyrogram.raw.types.InputPeerChat` - Basic Chats
|
2022-01-07 09:18:51 +00:00
|
|
|
- :class:`~pyrogram.raw.types.InputPeerChannel` - Channels & Supergroups
|
2019-04-12 13:52:06 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
But you don't necessarily have to manually instantiate each object because Pyrogram already provides
|
2019-05-28 14:41:55 +00:00
|
|
|
:meth:`~pyrogram.Client.resolve_peer` as a convenience utility method that returns the correct InputPeer
|
2019-04-12 13:52:06 +00:00
|
|
|
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.
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
Things are different when working with Pyrogram's API because having them in the same space could lead to
|
|
|
|
collisions, and that's why Pyrogram uses a slightly different representation for each kind of ID.
|
2019-04-12 13:52:06 +00:00
|
|
|
|
|
|
|
For example, given the ID *123456789*, here's how Pyrogram can tell entities apart:
|
|
|
|
|
2019-06-07 13:50:15 +00:00
|
|
|
- ``+ID`` User: *123456789*
|
|
|
|
- ``-ID`` Chat: *-123456789*
|
|
|
|
- ``-100ID`` Channel or Supergroup: *-100123456789*
|
2019-04-12 13:52:06 +00:00
|
|
|
|
|
|
|
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-17 11:44:44 +00:00
|
|
|
.. _Community: https://t.me/Pyrogram
|