2018-01-06 11:18:15 +00:00
|
|
|
Basic Usage
|
|
|
|
===========
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2018-03-25 19:30:39 +00:00
|
|
|
All the snippets below assume you have successfully created and started a :class:`Client <pyrogram.Client>`
|
|
|
|
instance. You also must be authorized, that is, a valid *.session file does exist in your working directory.
|
2018-01-06 11:18:15 +00:00
|
|
|
|
|
|
|
Simple API Access
|
|
|
|
-----------------
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
The easiest way to interact with the Telegram API is via the :class:`Client <pyrogram.Client>` class, which
|
|
|
|
exposes bot-like_ methods:
|
2018-01-06 11:18:15 +00:00
|
|
|
|
|
|
|
- Get information about the authorized user:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
print(app.get_me())
|
2018-01-06 11:18:15 +00:00
|
|
|
|
|
|
|
- Send a message to yourself (Saved Messages):
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
app.send_message("me", "Hi there! I'm using Pyrogram")
|
2018-04-11 21:18:17 +00:00
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
- Upload a new photo (with caption):
|
2018-04-11 21:18:17 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
app.send_photo("me", "/home/dan/perla.jpg", "Cute!")
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
.. seealso:: For a complete list of the available methods and an exhaustive description for each of them, have a look
|
|
|
|
at the :class:`Client <pyrogram.Client>` class.
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-01-15 10:57:16 +00:00
|
|
|
.. _using-raw-functions:
|
|
|
|
|
2018-01-06 11:18:15 +00:00
|
|
|
Using Raw Functions
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
If you want **complete**, low-level access to the Telegram API you have to use the raw
|
2018-03-25 19:30:39 +00:00
|
|
|
:mod:`functions <pyrogram.api.functions>` and :mod:`types <pyrogram.api.types>` exposed by the ``pyrogram.api``
|
2018-04-11 21:18:17 +00:00
|
|
|
package and call any Telegram API method you wish using the :meth:`send() <pyrogram.Client.send>` method provided by
|
2018-01-06 11:18:15 +00:00
|
|
|
the Client class.
|
|
|
|
|
|
|
|
Here some examples:
|
|
|
|
|
|
|
|
- Update first name, last name and bio:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import functions
|
|
|
|
|
2018-02-22 10:02:38 +00:00
|
|
|
...
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
app.send(
|
2018-01-06 11:18:15 +00:00
|
|
|
functions.account.UpdateProfile(
|
|
|
|
first_name="Dan", last_name="Tès",
|
|
|
|
about="Bio written from Pyrogram"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
- Share your Last Seen time only with your contacts:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import functions, types
|
|
|
|
|
2018-02-22 10:02:38 +00:00
|
|
|
...
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
app.send(
|
2018-01-06 11:18:15 +00:00
|
|
|
functions.account.SetPrivacy(
|
|
|
|
key=types.InputPrivacyKeyStatusTimestamp(),
|
|
|
|
rules=[types.InputPrivacyValueAllowContacts()]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-02-22 10:02:38 +00:00
|
|
|
- Invite users to your channel/supergroup:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import functions, types
|
|
|
|
|
|
|
|
...
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
app.send(
|
2018-02-22 10:02:38 +00:00
|
|
|
functions.channels.InviteToChannel(
|
2018-04-14 14:10:46 +00:00
|
|
|
channel=app.resolve_peer(123456789), # ID or Username
|
2018-02-22 10:02:38 +00:00
|
|
|
users=[ # The users you want to invite
|
2018-04-14 14:10:46 +00:00
|
|
|
app.resolve_peer(23456789), # By ID
|
|
|
|
app.resolve_peer("username"), # By username
|
|
|
|
app.resolve_peer("393281234567"), # By phone number
|
2018-02-22 10:02:38 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-01-06 11:18:15 +00:00
|
|
|
.. _bot-like: https://core.telegram.org/bots/api#available-methods
|