MTPyroger/docs/source/topics/auto-auth.rst

69 lines
2.3 KiB
ReStructuredText
Raw Normal View History

2018-01-25 12:47:26 +00:00
Auto Authorization
==================
Manually writing phone number, phone code and password on the terminal every time you want to login can be tedious.
Pyrogram is able to automate both **Log In** and **Sign Up** processes, all you need to do is pass the relevant
2019-05-28 14:41:55 +00:00
parameters when creating a new :class:`~pyrogram.Client`.
2018-01-25 12:47:26 +00:00
2018-02-21 11:21:10 +00:00
.. note:: If you omit any of the optional parameter required for the authorization, Pyrogram will ask you to
2018-03-25 19:30:39 +00:00
manually write it. For instance, if you don't want to set a ``last_name`` when creating a new account you
2018-02-21 11:21:10 +00:00
have to explicitly pass an empty string ""; the default value (None) will trigger the input() call.
Log In
2018-01-25 12:47:26 +00:00
-------
2018-03-25 19:30:39 +00:00
To automate the **Log In** process, pass your ``phone_number`` and ``password`` (if you have one) in the Client parameters.
If you want to retrieve the phone code programmatically, pass a callback function in the ``phone_code`` field — this
2018-06-24 15:13:17 +00:00
function accepts a single positional argument (phone_number) and must return the correct phone code (e.g., "12345")
— otherwise, ignore this parameter, Pyrogram will ask you to input the phone code manually.
Example:
2018-01-25 12:47:26 +00:00
.. code-block:: python
from pyrogram import Client
2018-06-24 15:13:17 +00:00
def phone_code_callback(phone_number):
2018-01-25 12:47:26 +00:00
code = ... # Get your code programmatically
2018-06-24 15:13:17 +00:00
return code # e.g., "12345"
2018-01-25 12:47:26 +00:00
app = Client(
2018-01-25 12:47:26 +00:00
session_name="example",
phone_number="39**********",
2018-06-24 15:13:17 +00:00
phone_code=phone_code_callback, # Note the missing parentheses
2018-01-25 12:47:26 +00:00
password="password" # (if you have one)
)
2018-10-22 18:23:50 +00:00
with app:
print(app.get_me())
2018-01-25 12:47:26 +00:00
Sign Up
2018-01-25 12:47:26 +00:00
-------
To automate the **Sign Up** process (i.e., automatically create a new Telegram account), simply fill **both**
2018-03-25 19:30:39 +00:00
``first_name`` and ``last_name`` fields alongside the other parameters; they will be used to automatically create a new
2018-01-25 12:47:26 +00:00
Telegram account in case the phone number you passed is not registered yet.
2018-06-24 15:13:17 +00:00
Example:
2018-01-25 12:47:26 +00:00
.. code-block:: python
from pyrogram import Client
2018-06-24 15:13:17 +00:00
def phone_code_callback(phone_number):
2018-01-25 12:47:26 +00:00
code = ... # Get your code programmatically
2018-06-24 15:13:17 +00:00
return code # e.g., "12345"
2018-01-25 12:47:26 +00:00
app = Client(
2018-01-25 12:47:26 +00:00
session_name="example",
phone_number="39**********",
2018-06-24 15:13:17 +00:00
phone_code=phone_code_callback, # Note the missing parentheses
2018-01-25 12:47:26 +00:00
first_name="Pyrogram",
last_name="" # Can be an empty string
)
2018-10-22 18:23:50 +00:00
with app:
print(app.get_me())