2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-31 22:35:36 +00:00

Allow accessing Object fields using square brackets

This commit is contained in:
Dan
2018-04-14 16:10:46 +02:00
parent e8193435a9
commit c84c64a5e1
9 changed files with 65 additions and 80 deletions

View File

@@ -9,31 +9,29 @@ Basic Usage
Simple API Access
-----------------
The easiest way to interact with the Telegram 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 the
API by abstracting the raw functions listed in the schema.
The result is a much cleaner interface that allows you to:
The easiest way to interact with the Telegram API is via the :class:`Client <pyrogram.Client>` class, which
exposes bot-like_ methods:
- Get information about the authorized user:
.. code-block:: python
print(client.get_me())
print(app.get_me())
- Send a message to yourself (Saved Messages):
.. code-block:: python
client.send_message("me", "Hi there! I'm using Pyrogram")
app.send_message("me", "Hi there! I'm using Pyrogram")
- Upload a photo (with caption):
- Upload a new photo (with caption):
.. code-block:: python
client.send_photo("me", "/home/dan/perla.jpg", "Cute!")
app.send_photo("me", "/home/dan/perla.jpg", "Cute!")
.. seealso:: For a complete list of the available methods have a look at the :class:`Client <pyrogram.Client>` class.
.. 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.
.. _using-raw-functions:
@@ -55,7 +53,7 @@ Here some examples:
...
client.send(
app.send(
functions.account.UpdateProfile(
first_name="Dan", last_name="Tès",
about="Bio written from Pyrogram"
@@ -70,7 +68,7 @@ Here some examples:
...
client.send(
app.send(
functions.account.SetPrivacy(
key=types.InputPrivacyKeyStatusTimestamp(),
rules=[types.InputPrivacyValueAllowContacts()]
@@ -85,13 +83,13 @@ Here some examples:
...
client.send(
app.send(
functions.channels.InviteToChannel(
channel=client.resolve_peer(123456789), # ID or Username
channel=app.resolve_peer(123456789), # ID or Username
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
app.resolve_peer(23456789), # By ID
app.resolve_peer("username"), # By username
app.resolve_peer("393281234567"), # By phone number
]
)
)

View File

@@ -21,28 +21,28 @@ Configuration
There are two ways to configure a Pyrogram application project, and you can choose the one that fits better for you:
Create a new ``config.ini`` file at the root of your working directory, copy-paste the following and replace the
**api_id** and **api_hash** values with `your own <#api-keys>`_. This is the preferred method because allows you
to keep your credentials out of your code without having to deal with how to load them:
- Create a new ``config.ini`` file at the root of your working directory, copy-paste the following and replace the
**api_id** and **api_hash** values with `your own <#api-keys>`_. This is the preferred method because allows you
to keep your credentials out of your code without having to deal with how to load them:
.. code-block:: ini
.. code-block:: ini
[pyrogram]
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef
[pyrogram]
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef
Alternatively, you can pass your API key to Pyrogram by simply using the *api_id* and *api_hash*
parameters of the Client class. This way you can have full control on how to store and load your credentials:
- Alternatively, you can pass your API key to Pyrogram by simply using the *api_id* and *api_hash*
parameters of the Client class. This way you can have full control on how to store and load your credentials:
.. code-block:: python
.. code-block:: python
from pyrogram import Client
from pyrogram import Client
client = Client(
session_name="example",
api_id=12345
api_hash="0123456789abcdef0123456789abcdef"
)
app = Client(
session_name="my_account",
api_id=12345
api_hash="0123456789abcdef0123456789abcdef"
)
.. note:: The examples below assume you have created a ``config.ini`` file, thus they won't show the *api_id*
and *api_hash* parameters usage.
@@ -52,15 +52,15 @@ User Authorization
In order to use the API, Telegram requires that Users be authorized via their phone numbers.
Pyrogram automatically manages this access, all you need to do is create an instance of
the :class:`Client <pyrogram.Client>` class by passing to it a ``<session_name>`` of your choice
the :class:`Client <pyrogram.Client>` class by passing to it a ``session_name`` of your choice
(e.g.: "my_account") and call the :meth:`start() <pyrogram.Client.start>` method:
.. code-block:: python
from pyrogram import Client
client = Client("my_account")
client.start()
app = Client("my_account")
app.start()
This starts an interactive shell asking you to input your **phone number** (including your `Country Code`_)
and the **phone code** you will receive:
@@ -90,11 +90,11 @@ Instead of phone numbers, Bots are authorized via their tokens which are created
from pyrogram import Client
client = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
client.start()
app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
app.start()
That's all, no further action is needed. The session file created will be named after the Bot user_id, which is
``123456.session`` in the example above.
That's all, no further action is needed. The session file will be named after the Bot user_id, which is
``123456.session`` for the example above.
.. _`Country Code`: https://en.wikipedia.org/wiki/List_of_country_calling_codes
.. _BotFather: https://t.me/botfather