pyrogram/docs/source/start/Setup.rst

120 lines
4.4 KiB
ReStructuredText
Raw Normal View History

2018-06-05 14:37:57 +00:00
Setup
=====
2018-01-06 11:18:15 +00:00
2018-06-22 10:55:52 +00:00
Once you successfully `installed Pyrogram`_, you will still have to follow a few steps before you can actually use
2018-06-05 14:37:57 +00:00
the library to make API calls. This section provides all the information you need in order to set up a project
with Pyrogram.
2018-01-06 11:18:15 +00:00
API Keys
--------
2018-08-16 16:53:53 +00:00
The very first step requires you to obtain a valid Telegram API key (API id/hash pair).
2018-04-11 21:18:17 +00:00
If you already have one you can skip this step, otherwise:
2018-01-06 11:18:15 +00:00
#. Visit https://my.telegram.org/apps and log in with your Telegram Account.
#. Fill out the form to register a new Telegram application.
2018-08-16 16:53:53 +00:00
#. Done. The API key consists of two parts: **App api_id** and **App api_hash**.
2018-01-06 11:18:15 +00:00
2018-08-16 16:53:53 +00:00
.. important::
2019-04-12 13:52:06 +00:00
This API key is personal and must be kept secret.
2018-01-06 11:18:15 +00:00
Configuration
-------------
The API key obtained in the `previous step <#api-keys>`_ defines a token for your application allowing you to access
2019-04-12 13:52:06 +00:00
the Telegram database using the MTProto API — **it is therefore required for all authorizations of both users and bots**.
Having it handy, it's time to configure your Pyrogram project. There are two ways to do so, and you can choose what
fits better for you:
2018-01-06 11:18:15 +00:00
- Create a new ``config.ini`` file at the root of your working directory, copy-paste the following and replace the
2019-04-12 13:52:06 +00:00
**api_id** and **api_hash** values with your own. 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:
2018-01-06 11:18:15 +00:00
.. code-block:: ini
2018-02-22 10:02:38 +00:00
[pyrogram]
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef
2018-02-22 10:02:38 +00:00
2019-04-12 13:52:06 +00:00
- 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:
2018-02-22 10:02:38 +00:00
.. code-block:: python
2018-02-22 10:02:38 +00:00
from pyrogram import Client
2018-02-22 10:02:38 +00:00
app = Client(
2018-06-22 11:19:04 +00:00
"my_account",
2018-05-10 16:13:27 +00:00
api_id=12345,
api_hash="0123456789abcdef0123456789abcdef"
)
2018-02-22 10:02:38 +00:00
2018-08-16 16:55:27 +00:00
.. note::
2018-10-14 09:58:23 +00:00
2019-04-12 13:52:06 +00:00
From now on, the code snippets assume you are using the ``config.ini`` file, thus they won't show the *api_id* and
*api_hash* parameters usage to keep them as clean as possible.
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
User Authorization
------------------
2018-01-06 11:18:15 +00:00
2019-04-12 13:52:06 +00:00
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 (e.g.: "my_account") and call
the :meth:`run() <pyrogram.Client.run>` method:
2018-01-06 11:18:15 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
2018-06-22 11:19:04 +00:00
app.run()
2018-01-06 11:18:15 +00:00
This starts an interactive shell asking you to input your **phone number** (including your `Country Code`_)
and the **phone code** you will receive:
2018-10-14 09:58:23 +00:00
.. code-block:: text
2018-01-06 11:18:15 +00:00
Enter phone number: +39**********
Is "+39**********" correct? (y/n): y
Enter phone code: 32768
2019-04-12 13:52:06 +00:00
Logged in successfully as Dan
2018-01-06 11:18:15 +00:00
2019-04-12 13:52:06 +00:00
After successfully authorizing yourself, a new file called ``my_account.session`` will be created allowing Pyrogram
executing API calls with your identity. This file will be loaded again when you restart your app, and as long as you
keep the session alive, Pyrogram won't ask you again to enter your phone number.
2018-01-06 11:18:15 +00:00
2018-10-14 09:58:23 +00:00
.. important::
Your ``*.session`` files are personal and must be kept secret.
2018-01-06 11:18:15 +00:00
2019-04-12 13:52:06 +00:00
.. note::
The code above does nothing except asking for credentials and keeping the client online, hit ``CTRL+C`` now to stop
your application and keep reading.
2018-04-11 21:18:17 +00:00
Bot Authorization
-----------------
2019-03-22 11:39:15 +00:00
Bots are a special kind of users that are authorized via their tokens (instead of phone numbers), which are created by
2019-04-12 13:52:06 +00:00
BotFather_. Bot tokens replace the users' phone numbers only — you still need to
`configure a Telegram API key <#configuration>`_ with Pyrogram, even when using bots.
2018-04-12 11:43:16 +00:00
2019-03-22 11:39:15 +00:00
The authorization process is automatically managed. All you need to do is choose a ``session_name`` (can be anything,
2019-04-12 13:52:06 +00:00
usually your bot username) and pass your bot token using the ``bot_token`` parameter. The session file will be named
after the session name, which will be ``pyrogrambot.session`` for the example below.
2018-04-11 21:18:17 +00:00
.. code-block:: python
from pyrogram import Client
2019-04-12 13:52:06 +00:00
app = Client(
"pyrogrambot",
bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
)
2018-06-22 11:19:04 +00:00
app.run()
2018-01-06 11:18:15 +00:00
2018-06-22 10:55:52 +00:00
.. _installed Pyrogram: Installation.html
2018-04-11 21:18:17 +00:00
.. _`Country Code`: https://en.wikipedia.org/wiki/List_of_country_calling_codes
.. _BotFather: https://t.me/botfather