pyrogram/docs/source/start/invoking.rst

121 lines
3.1 KiB
ReStructuredText
Raw Normal View History

2019-05-12 17:26:55 +00:00
Calling 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
account; we are now aiming towards the core of the library. It's time to start playing with the API!
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
-----------
Making API method calls with Pyrogram is very simple. Here's a basic example we are going to examine step by step and
then expand to explain what happens underneath:
2019-05-10 14:14:10 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
with app:
app.send_message("me", "Hi!")
2019-05-10 14:14:10 +00:00
Basic step-by-step
^^^^^^^^^^^^^^^^^^
2019-05-10 14:14:10 +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
2019-06-04 14:10:32 +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
#. The ``with`` context manager is a shortcut for starting, executing and stopping the Client:
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
with app:
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
#. Now, you can call any method you like:
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
app.send_message("me", "Hi!")
2019-05-10 14:14:10 +00:00
Context Manager
---------------
2019-05-10 14:14:10 +00:00
The ``with`` statement starts a context manager, which is used as a shortcut to automatically call
:meth:`~pyrogram.Client.start` and :meth:`~pyrogram.Client.stop`, which are methods required for Pyrogram to work
properly. The context manager does also gracefully stop the client, even in case of unhandled exceptions in your code.
2019-05-10 14:14:10 +00:00
This is how Pyrogram looks without 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")
app.start()
app.send_message("me", "Hi!")
app.stop()
Asynchronous Calls
------------------
2019-05-10 14:14:10 +00:00
In case you want Pyrogram to run asynchronously (e.g.: if you are using third party libraries that require you to call
them with ``await``), use the asynchronous context manager:
2019-05-10 14:14:10 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
async def main():
async with app:
await app.send_message("me", "Hi!")
app.run(main())
Asynchronous step-by-step
^^^^^^^^^^^^^^^^^^^^^^^^^
#. Import the Client class and create an instance:
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
#. Async methods can't normally be executed at the top level, because they must be inside an async-defined function;
here we define one and put our code inside; the context manager is also being used differently in asyncio and
method calls require the await keyword:
.. code-block:: python
async def main():
async with app:
await app.send_message("me", "Hi!")
#. Finally, we tell Python to schedule our ``main()`` async function, which in turn will execute Pyrogram's code. Using
:meth:`~pyrogram.Client.run` this way is a friendly alternative for the much more verbose
``asyncio.get_event_loop().run_until_complete(main())``:
.. code-block:: python
2019-05-10 14:14:10 +00:00
app.run(main())