MTPyroger/docs/source/start/Usage.rst

118 lines
3.7 KiB
ReStructuredText
Raw Normal View History

2018-06-05 14:38:12 +00:00
Usage
=====
Having your `project set up`_ and your account authorized_, it's time to play with the API.
In this section, you'll be shown two ways of communicating with Telegram using Pyrogram. Let's start!
High-level API
--------------
The easiest and recommended way to interact with Telegram is via the high-level Pyrogram methods_ and types_, which are
named after the `Telegram Bot API`_.
2018-06-22 11:27:43 +00:00
Examples (more on `GitHub <https://github.com/pyrogram/pyrogram/tree/develop/examples>`_):
2018-06-05 14:38:12 +00:00
- Get information about the authorized user:
.. code-block:: python
print(app.get_me())
- Send a message to yourself (Saved Messages):
.. code-block:: python
app.send_message("me", "Hi there! I'm using Pyrogram")
- Upload a new photo (with caption):
.. code-block:: python
app.send_photo("me", "/home/dan/perla.jpg", "Cute!")
2018-06-26 22:12:09 +00:00
Raw Functions
-------------
2018-06-05 14:38:12 +00:00
2018-06-22 11:10:14 +00:00
If you can't find a high-level method for your needs or if want complete, low-level access to the whole Telegram API,
2018-06-05 14:38:12 +00:00
you have to use the raw :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 the Client class.
.. hint:: Every high-level method mentioned in the section above is built on top of these raw functions.
2018-06-25 16:27:30 +00:00
2018-06-05 14:38:12 +00:00
Nothing stops you from using the raw functions only, but they are rather complex and `plenty of them`_ are already
re-implemented by providing a much simpler and cleaner interface which is very similar to the Bot API.
2018-06-25 16:27:30 +00:00
2018-06-05 14:38:12 +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_!
2018-06-22 11:27:43 +00:00
Examples (more on `GitHub <https://github.com/pyrogram/pyrogram/tree/develop/examples>`_):
2018-06-05 14:38:12 +00:00
- Update first name, last name and bio:
.. code-block:: python
2018-06-22 11:27:43 +00:00
from pyrogram import Client
2018-06-05 14:38:12 +00:00
from pyrogram.api import functions
2018-06-22 11:27:43 +00:00
app = Client("my_account")
app.start()
2018-06-05 14:38:12 +00:00
app.send(
functions.account.UpdateProfile(
first_name="Dan", last_name="Tès",
about="Bio written from Pyrogram"
)
)
2018-06-22 11:27:43 +00:00
app.stop()
2018-06-05 14:38:12 +00:00
- Share your Last Seen time only with your contacts:
.. code-block:: python
2018-06-22 11:27:43 +00:00
from pyrogram import Client
2018-06-05 14:38:12 +00:00
from pyrogram.api import functions, types
2018-06-22 11:27:43 +00:00
app = Client("my_account")
app.start()
2018-06-05 14:38:12 +00:00
app.send(
functions.account.SetPrivacy(
key=types.InputPrivacyKeyStatusTimestamp(),
rules=[types.InputPrivacyValueAllowContacts()]
)
)
2018-06-22 11:27:43 +00:00
app.stop()
2018-06-05 14:38:12 +00:00
- Invite users to your channel/supergroup:
.. code-block:: python
2018-06-22 11:27:43 +00:00
from pyrogram import Client
2018-06-05 14:38:12 +00:00
from pyrogram.api import functions, types
2018-06-22 11:27:43 +00:00
app = Client("my_account")
app.start()
2018-06-05 14:38:12 +00:00
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
app.resolve_peer("393281234567"), # By phone number
]
)
)
2018-06-22 11:27:43 +00:00
app.stop()
2018-06-05 14:38:12 +00:00
.. _methods: ../pyrogram/Client.html#available-methods
.. _plenty of them: ../pyrogram/Client.html#available-methods
.. _types: ../pyrogram/types/index.html
.. _Raw Functions: Usage.html#using-raw-functions
.. _Community: https://t.me/PyrogramChat
.. _project set up: Setup.html
.. _authorized: Setup.html#user-authorization
.. _Telegram Bot API: https://core.telegram.org/bots/api