mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-31 06:16:06 +00:00
Update docs with new content
This commit is contained in:
@@ -24,40 +24,40 @@ Making API method calls with Pyrogram is very simple. Here's an example we are g
|
||||
|
||||
app.stop()
|
||||
|
||||
Let's begin by importing the Client class from the Pyrogram package:
|
||||
#. Let's begin by importing the Client class from the Pyrogram package:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram import Client
|
||||
from pyrogram import Client
|
||||
|
||||
Now instantiate a new Client object, "my_account" is a session name of your choice:
|
||||
#. Now instantiate a new Client object, "my_account" is a session name of your choice:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
app = Client("my_account")
|
||||
app = Client("my_account")
|
||||
|
||||
To actually make use of any method, the client has to be started first:
|
||||
#. To actually make use of any method, the client has to be started first:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
app.start()
|
||||
app.start()
|
||||
|
||||
Now, you can call any method you like:
|
||||
#. Now, you can call any method you like:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
print(app.get_me()) # Print information about yourself
|
||||
print(app.get_me()) # Print information about yourself
|
||||
|
||||
# Send messages to yourself:
|
||||
app.send_message("me", "Hi!") # Text message
|
||||
app.send_location("me", 51.500729, -0.124583) # Location
|
||||
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI") # Sticker
|
||||
# Send messages to yourself:
|
||||
app.send_message("me", "Hi!") # Text message
|
||||
app.send_location("me", 51.500729, -0.124583) # Location
|
||||
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI") # Sticker
|
||||
|
||||
Finally, when done, simply stop the client:
|
||||
#. Finally, when done, simply stop the client:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
app.stop()
|
||||
app.stop()
|
||||
|
||||
Context Manager
|
||||
---------------
|
||||
|
@@ -45,36 +45,37 @@ arrives:
|
||||
|
||||
app.run()
|
||||
|
||||
Let's examine these four new pieces. First one: a callback function we defined which accepts two arguments -
|
||||
*(client, message)*. This will be the function that gets executed every time a new message arrives and Pyrogram will
|
||||
call that function by passing the client instance and the new message instance as argument.
|
||||
#. Let's examine these four new pieces. First one: a callback function we defined which accepts two arguments -
|
||||
*(client, message)*. This will be the function that gets executed every time a new message arrives and Pyrogram will
|
||||
call that function by passing the client instance and the new message instance as argument.
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
def my_function(client, message):
|
||||
print(message)
|
||||
def my_function(client, message):
|
||||
print(message)
|
||||
|
||||
Second one: the :class:`~pyrogram.MessageHandler`. This object tells Pyrogram the function we defined above must only
|
||||
handle updates that are in form of a :class:`~pyrogram.Message`:
|
||||
#. Second one: the :class:`~pyrogram.MessageHandler`. This object tells Pyrogram the function we defined above must
|
||||
only handle updates that are in form of a :class:`~pyrogram.Message`:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
my_handler = MessageHandler(my_function)
|
||||
my_handler = MessageHandler(my_function)
|
||||
|
||||
Third: the method :meth:`~pyrogram.Client.add_handler`. This method is used to actually register the handler and let
|
||||
Pyrogram know it needs to be taken into consideration when new updates arrive and the internal dispatching phase begins.
|
||||
#. Third: the method :meth:`~pyrogram.Client.add_handler`. This method is used to actually register the handler and let
|
||||
Pyrogram know it needs to be taken into consideration when new updates arrive and the internal dispatching phase
|
||||
begins.
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
app.add_handler(my_handler)
|
||||
app.add_handler(my_handler)
|
||||
|
||||
Last one, the :meth:`~pyrogram.Client.run` method. What this does is simply call :meth:`~pyrogram.Client.start` and a
|
||||
special method :meth:`~pyrogram.Client.idle` that keeps your main scripts alive until you press ``CTRL+C``; the client
|
||||
will be automatically stopped after that.
|
||||
#. Last one, the :meth:`~pyrogram.Client.run` method. What this does is simply call :meth:`~pyrogram.Client.start` and
|
||||
a special method :meth:`~pyrogram.Client.idle` that keeps your main scripts alive until you press ``CTRL+C``; the
|
||||
client will be automatically stopped after that.
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
app.run()
|
||||
app.run()
|
||||
|
||||
Using Decorators
|
||||
----------------
|
||||
|
Reference in New Issue
Block a user