pyrogram/docs/source/start/BasicUsage.rst

96 lines
2.7 KiB
ReStructuredText
Raw Normal View History

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-03-25 19:30:39 +00:00
The easiest way to interact with the API is via the :class:`Client <pyrogram.Client>` class which exposes bot-like_
methods. The purpose of this Client class is to make it even simpler to work with Telegram's API by abstracting the
2018-01-06 11:18:15 +00:00
raw functions listed in the API scheme.
The result is a much cleaner interface that allows you to:
- Get information about the authorized user:
.. code-block:: python
print(client.get_me())
- Send a message to yourself (Saved Messages):
.. code-block:: python
client.send_message(
chat_id="me",
text="Hi there! I'm using Pyrogram"
)
2018-03-25 19:30:39 +00:00
.. seealso:: For a complete list of the available methods 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``
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-01-06 11:18:15 +00:00
client.send(
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-01-06 11:18:15 +00:00
client.send(
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
...
client.send(
functions.channels.InviteToChannel(
2018-02-27 17:25:59 +00:00
channel=client.resolve_peer(123456789), # ID or Username
2018-02-22 10:02:38 +00:00
users=[ # The users you want to invite
client.resolve_peer(23456789), # By ID
client.resolve_peer("username"), # By username
client.resolve_peer("393281234567"), # By phone number
]
)
)
2018-01-06 11:18:15 +00:00
.. _bot-like: https://core.telegram.org/bots/api#available-methods