MTPyroger/docs/source/start/Usage.rst

52 lines
1.5 KiB
ReStructuredText
Raw Normal View History

2018-06-05 14:38:12 +00:00
Usage
=====
2019-04-12 13:52:06 +00:00
Having your `project set up`_ and your account authorized_, it's time to start playing with the API. Let's start!
2018-06-05 14:38:12 +00:00
High-level API
--------------
The easiest and recommended way to interact with Telegram is via the high-level Pyrogram methods_ and types_, which are
named after the `Telegram Bot API`_.
Here's a simple example:
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
.. code-block:: python
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
from pyrogram import Client
2019-04-12 13:52:06 +00:00
app = Client("my_account")
2019-04-12 13:52:06 +00:00
app.start()
2019-04-12 13:52:06 +00:00
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")
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
app.stop()
2018-10-14 10:02:48 +00:00
You can also use Pyrogram in a context manager with the ``with`` statement. The Client will automatically
:meth:`start <pyrogram.Client.start>` and :meth:`stop <pyrogram.Client.stop>` gracefully, even in case of unhandled
exceptions in your code:
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
.. code-block:: python
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
from pyrogram import Client
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
app = Client("my_account")
2018-06-05 14:38:12 +00:00
2019-04-12 13:52:06 +00:00
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")
2018-06-05 14:38:12 +00:00
More examples on `GitHub <https://github.com/pyrogram/pyrogram/tree/develop/examples>`_.
2018-06-05 14:38:12 +00:00
.. _project set up: Setup.html
.. _authorized: Setup.html#user-authorization
.. _Telegram Bot API: https://core.telegram.org/bots/api
.. _methods: ../pyrogram/Client.html#messages
2019-04-12 13:52:06 +00:00
.. _types: ../pyrogram/Types.html