2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-01 06:45:39 +00:00

Remove API key requirement for existing sessions

This commit is contained in:
Dan
2022-04-24 11:56:07 +02:00
parent b8f39725c5
commit 124bcb4db7
28 changed files with 250 additions and 435 deletions

View File

@@ -1,7 +1,7 @@
Authorization
=============
Once a :doc:`project is set up <../intro/setup>`, you will still have to follow a few steps before you can actually use Pyrogram to make
Once a :doc:`project is set up <setup>`, you will still have to follow a few steps before you can actually use Pyrogram to make
API calls. This section provides all the information you need in order to authorize yourself as user or bot.
.. contents:: Contents
@@ -23,7 +23,11 @@ the :meth:`~pyrogram.Client.run` method:
from pyrogram import Client
app = Client("my_account")
api_id = 12345
api_hash = "0123456789abcdef0123456789abcdef"
app = Client("my_account", api_id=api_id, api_hash=api_hash)
app.run()
This starts an interactive shell asking you to input your **phone number**, including your `Country Code`_ (the plus
@@ -38,8 +42,8 @@ authorized or via SMS:
Logged in successfully
After successfully authorizing yourself, a new file called ``my_account.session`` will be created allowing Pyrogram to
execute API calls with your identity. This file is personal and 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.
execute API calls with your identity. This file is personal and will be loaded again when you restart your app.
You can now remove the api_id and api_hash values from the code as they are not needed anymore.
.. note::
@@ -51,7 +55,7 @@ Bot Authorization
Bots are a special kind of users that are authorized via their tokens (instead of phone numbers), which are created by
the `Bot Father`_. Bot tokens replace the users' phone numbers only — you still need to
:doc:`configure a Telegram API key <../intro/setup>` with Pyrogram, even when using bots.
:doc:`configure a Telegram API key <../start/setup>` with Pyrogram, even when using bots.
The authorization process is automatically managed. All you need to do is choose a ``session_name`` (can be anything,
usually your bot username) and pass your bot token using the ``bot_token`` parameter. The session file will be named
@@ -61,12 +65,29 @@ after the session name, which will be ``my_bot.session`` for the example below.
from pyrogram import Client
api_id = 12345
api_hash = "0123456789abcdef0123456789abcdef"
bot_token = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
app = Client(
"my_bot",
bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
api_id=api_id, api_hash=api_hash,
bot_token=bot_token
)
app.run()
.. _Country Code: https://en.wikipedia.org/wiki/List_of_country_calling_codes
.. _Bot Father: https://t.me/botfather
.. _Bot Father: https://t.me/botfather
.. note::
The API key (api_id and api_hash) and the bot_token is not needed anymore after a successful authorization.
This means you can now simply use:
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
app.run()

View File

@@ -22,10 +22,12 @@ Making API calls with Pyrogram is very simple. Here's a basic example we are goi
app = Client("my_account")
async def main():
async with app:
await app.send_message("me", "Hi!")
app.run(main())
Step-by-step
@@ -76,11 +78,13 @@ Below there's the same example as above, but without the use of the context mana
app = Client("my_account")
async def main():
await app.start()
await app.send_message("me", "Hi!")
await app.stop()
app.run(main())
Using asyncio.run()
@@ -95,10 +99,12 @@ be instantiated inside the main function.
import asyncio
from pyrogram import Client
async def main():
app = Client("my_account")
async with app:
await app.send_message("me", "Hi!")
asyncio.run(main())

View File

@@ -0,0 +1,40 @@
Project Setup
=============
We have just :doc:`installed Pyrogram <../intro/install>`. In this page we'll discuss what you need to do in order to set up a
project with the framework.
.. contents:: Contents
:backlinks: none
:depth: 1
:local:
-----
API Key
-------
The first step requires you to obtain a valid Telegram API key (api_id and api_hash pair):
#. Visit https://my.telegram.org/apps and log in with your Telegram account.
#. Fill out the form with your details and register a new Telegram application.
#. Done. The API key consists of two parts: **api_id** and **api_hash**. Keep it secret.
.. note::
The API key defines a token for a Telegram *application* you are going to build.
This means that you are able to authorize multiple users or bots with a single API key.
Configuration
-------------
Having the API key from the previous step in handy, we can now begin to configure a Pyrogram project: pass your API key to Pyrogram by using the *api_id* and *api_hash* parameters of the Client class:
.. code-block:: python
from pyrogram import Client
api_id = 12345
api_hash = "0123456789abcdef0123456789abcdef"
app = Client("my_account", api_id=api_id, api_hash=api_hash)

View File

@@ -39,10 +39,12 @@ The most elegant way to register a message handler is by using the :meth:`~pyrog
app = Client("my_account")
@app.on_message()
async def my_handler(client, message):
await message.forward("me")
app.run()
The defined function ``my_handler``, which accepts the two arguments *(client, message)*, will be the function that gets
@@ -63,9 +65,11 @@ function and registers it in your Client. It is useful in case you want to progr
from pyrogram import Client
from pyrogram.handlers import MessageHandler
async def my_function(client, message):
await message.forward("me")
app = Client("my_account")
my_handler = MessageHandler(my_function)