MTPyroger/docs/source/start/invoking.rst

88 lines
2.3 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
:local:
-----
2019-05-16 19:28:34 +00:00
Basic Usage
-----------
2019-05-12 17:26:55 +00:00
Making API method calls with Pyrogram is very simple. Here's an example we are going to examine:
2019-05-10 14:14:10 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
app.start()
print(app.get_me())
app.send_message("me", "Hi, it's me!")
app.send_location("me", 51.500729, -0.124583)
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI")
app.stop()
2019-06-04 14:10:32 +00:00
#. Let's begin by importing the Client class from the Pyrogram package:
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
2019-06-04 14:10:32 +00:00
#. To actually make use of any method, the client has to be started first:
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.start()
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
2019-06-04 14:10:32 +00:00
print(app.get_me()) # Print information about yourself
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
# 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
2019-05-10 14:14:10 +00:00
2019-06-04 14:10:32 +00:00
#. Finally, when done, simply stop 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
2019-06-04 14:10:32 +00:00
app.stop()
2019-05-10 14:14:10 +00:00
Context Manager
---------------
You can also use Pyrogram's Client in a context manager with the ``with`` statement. The client will automatically
2019-05-28 14:41:55 +00:00
:meth:`~pyrogram.Client.start` and :meth:`~pyrogram.Client.stop` gracefully, even in case of unhandled exceptions in
your code. The example above can be therefore rewritten in a much nicer way:
2019-05-10 14:14:10 +00:00
.. code-block:: python
from pyrogram import Client
app = Client("my_account")
with app:
print(app.get_me())
app.send_message("me", "Hi there! I'm using **Pyrogram**")
app.send_location("me", 51.500729, -0.124583)
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI")
More examples can be found on `GitHub <https://github.com/pyrogram/pyrogram/tree/develop/examples>`_.