pyrogram/docs/source/start/invoking.rst

104 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2022-04-24 09:56:07 +00:00
Invoking Methods
================
2019-05-10 14:14:10 +00:00
2019-05-28 14:41:55 +00:00
At this point, we have successfully :doc:`installed Pyrogram <../intro/install>` and :doc:`authorized <auth>` our
2022-01-07 09:18:51 +00:00
account; we are now aiming towards the core of the framework.
2019-05-10 14:14:10 +00:00
2020-04-01 18:08:46 +00:00
.. contents:: Contents
:backlinks: none
:depth: 1
2020-04-01 18:08:46 +00:00
:local:
-----
2019-05-16 19:28:34 +00:00
Basic Usage
-----------
2022-04-24 09:56:07 +00:00
Making API calls with Pyrogram is very simple. Here's a basic example we are going to examine step by step:
2019-05-10 14:14:10 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
2022-01-07 09:18:51 +00:00
async def main():
async with app:
await app.send_message("me", "Hi!")
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
app.run(main())
Step-by-step
^^^^^^^^^^^^
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
#. Let's begin by importing the Client class.
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
.. code-block:: python
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
from pyrogram import Client
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
#. Now instantiate a new Client object, "my_account" is a session name of your choice.
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
.. code-block:: python
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
app = Client("my_account")
2019-05-10 14:14:10 +00:00
2022-04-24 09:56:07 +00:00
#. Async methods must be invoked within an async context.
2022-01-07 09:18:51 +00:00
Here we define an async function and put our code inside. Also notice the ``await`` keyword in front of the method
call; this is required for all asynchronous methods.
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
.. code-block:: python
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
async def main():
async with app:
await app.send_message("me", "Hi!")
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
#. Finally, we tell Python to schedule our ``main()`` async function by using Pyrogram's :meth:`~pyrogram.Client.run`
method.
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
.. code-block:: python
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
app.run(main())
2019-05-10 14:14:10 +00:00
Context Manager
---------------
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
The ``async with`` statement starts a context manager, which is used as a shortcut for starting, executing and stopping
the Client, asynchronously. It does so by automatically calling :meth:`~pyrogram.Client.start` and
:meth:`~pyrogram.Client.stop` in a more convenient way which also gracefully stops the client, even in case of
unhandled exceptions in your code.
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
Below there's the same example as above, but without the use of the context manager:
2019-05-10 14:14:10 +00:00
.. code-block:: python
2019-05-10 14:14:10 +00:00
from pyrogram import Client
app = Client("my_account")
2022-01-07 09:18:51 +00:00
async def main():
await app.start()
await app.send_message("me", "Hi!")
await app.stop()
2022-01-07 09:18:51 +00:00
app.run(main())
2019-05-10 14:14:10 +00:00
2022-01-07 09:18:51 +00:00
Using asyncio.run()
-------------------
Alternatively to the :meth:`~pyrogram.Client.run` method, you can use Python's ``asyncio.run()`` to execute the main
function, with one little caveat: the Client instance (and possibly other asyncio resources you are going to use) must
be instantiated inside the main function.
2019-05-10 14:14:10 +00:00
.. code-block:: python
2022-01-07 09:18:51 +00:00
import asyncio
2019-05-10 14:14:10 +00:00
from pyrogram import Client
async def main():
2022-01-07 09:18:51 +00:00
app = Client("my_account")
async with app:
await app.send_message("me", "Hi!")
2022-04-24 09:56:07 +00:00
asyncio.run(main())