mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-31 22:35:36 +00:00
Docs revamp. Part 4
This commit is contained in:
102
docs/source/api/bound-methods.rst
Normal file
102
docs/source/api/bound-methods.rst
Normal file
@@ -0,0 +1,102 @@
|
||||
Bound Methods
|
||||
=============
|
||||
|
||||
Some Pyrogram types define what are called bound methods. Bound methods are functions attached to a class which are
|
||||
accessed via an instance of that class. They make it even easier to call specific methods by automatically inferring
|
||||
some of the required arguments.
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 8
|
||||
|
||||
from pyrogram import Client
|
||||
|
||||
app = Client("my_account")
|
||||
|
||||
|
||||
@app.on_message()
|
||||
def hello(client, message)
|
||||
message.reply("hi")
|
||||
|
||||
|
||||
app.run()
|
||||
|
||||
.. currentmodule:: pyrogram
|
||||
|
||||
- Message_
|
||||
- CallbackQuery_
|
||||
- InlineQuery_
|
||||
|
||||
.. _Message:
|
||||
|
||||
Message
|
||||
-------
|
||||
|
||||
- :meth:`Message.click()`
|
||||
- :meth:`Message.delete()`
|
||||
- :meth:`Message.download()`
|
||||
- :meth:`Message.edit()`
|
||||
- :meth:`Message.edit_caption()`
|
||||
- :meth:`Message.edit_media()`
|
||||
- :meth:`Message.edit_reply_markup()`
|
||||
- :meth:`Message.forward()`
|
||||
- :meth:`Message.pin()`
|
||||
- :meth:`Message.reply()`
|
||||
- :meth:`Message.reply_animation()`
|
||||
- :meth:`Message.reply_audio()`
|
||||
- :meth:`Message.reply_cached_media()`
|
||||
- :meth:`Message.reply_chat_action()`
|
||||
- :meth:`Message.reply_contact()`
|
||||
- :meth:`Message.reply_document()`
|
||||
- :meth:`Message.reply_game()`
|
||||
- :meth:`Message.reply_inline_bot_result()`
|
||||
- :meth:`Message.reply_location()`
|
||||
- :meth:`Message.reply_media_group()`
|
||||
- :meth:`Message.reply_photo()`
|
||||
- :meth:`Message.reply_poll()`
|
||||
- :meth:`Message.reply_sticker()`
|
||||
- :meth:`Message.reply_venue()`
|
||||
- :meth:`Message.reply_video()`
|
||||
- :meth:`Message.reply_video_note()`
|
||||
- :meth:`Message.reply_voice()`
|
||||
|
||||
.. automethod:: Message.click()
|
||||
.. automethod:: Message.delete()
|
||||
.. automethod:: Message.download()
|
||||
.. automethod:: Message.edit()
|
||||
.. automethod:: Message.edit_caption()
|
||||
.. automethod:: Message.edit_media()
|
||||
.. automethod:: Message.edit_reply_markup()
|
||||
.. automethod:: Message.forward()
|
||||
.. automethod:: Message.pin()
|
||||
.. automethod:: Message.reply()
|
||||
.. automethod:: Message.reply_animation()
|
||||
.. automethod:: Message.reply_audio()
|
||||
.. automethod:: Message.reply_cached_media()
|
||||
.. automethod:: Message.reply_chat_action()
|
||||
.. automethod:: Message.reply_contact()
|
||||
.. automethod:: Message.reply_document()
|
||||
.. automethod:: Message.reply_game()
|
||||
.. automethod:: Message.reply_inline_bot_result()
|
||||
.. automethod:: Message.reply_location()
|
||||
.. automethod:: Message.reply_media_group()
|
||||
.. automethod:: Message.reply_photo()
|
||||
.. automethod:: Message.reply_poll()
|
||||
.. automethod:: Message.reply_sticker()
|
||||
.. automethod:: Message.reply_venue()
|
||||
.. automethod:: Message.reply_video()
|
||||
.. automethod:: Message.reply_video_note()
|
||||
.. automethod:: Message.reply_voice()
|
||||
|
||||
.. _CallbackQuery:
|
||||
|
||||
CallbackQuery
|
||||
-------------
|
||||
|
||||
.. automethod:: CallbackQuery.answer()
|
||||
|
||||
.. _InlineQuery:
|
||||
|
||||
InlineQuery
|
||||
-----------
|
||||
|
||||
.. automethod:: InlineQuery.answer()
|
@@ -1,7 +1,16 @@
|
||||
Pyrogram Client
|
||||
===============
|
||||
|
||||
The :class:`Client <pyrogram.Client>` is the main class. It exposes easy-to-use methods that are named
|
||||
after the well established Telegram Bot API methods, thus offering a familiar look to Bot developers.
|
||||
This class exposes high-level methods for an easy access to the API.
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 1-3
|
||||
|
||||
from pyrogram import Client
|
||||
|
||||
app = Client("my_account")
|
||||
|
||||
with app:
|
||||
app.send_message("me", "Hi!")
|
||||
|
||||
.. autoclass:: pyrogram.Client()
|
||||
|
@@ -1,7 +1,7 @@
|
||||
Decorators
|
||||
==========
|
||||
|
||||
While still being methods bound to the :obj:`Client` class, decorators are of a special kind and thus deserve a
|
||||
While still being methods bound to the :obj:`Client <pyrogram.Client>` class, decorators are of a special kind and thus deserve a
|
||||
dedicated page.
|
||||
|
||||
Decorators are able to register callback functions for handling updates in a much easier and cleaner way compared to
|
||||
@@ -9,13 +9,12 @@ Decorators are able to register callback functions for handling updates in a muc
|
||||
:meth:`add_handler() <pyrogram.Client.add_handler>`, automatically. All you need to do is adding the decorators on top
|
||||
of your functions.
|
||||
|
||||
**Example:**
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 6
|
||||
|
||||
from pyrogram import Client
|
||||
|
||||
app = Client(...)
|
||||
app = Client("my_account")
|
||||
|
||||
|
||||
@app.on_message()
|
||||
|
@@ -5,27 +5,26 @@ All Pyrogram API errors live inside the ``errors`` sub-package: ``pyrogram.error
|
||||
The errors ids listed here are shown as *UPPER_SNAKE_CASE*, but the actual exception names to import from Pyrogram
|
||||
follow the usual *PascalCase* convention.
|
||||
|
||||
**Example**:
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 1, 5
|
||||
|
||||
from pyrogram.errors import InternalServerError
|
||||
from pyrogram.errors import FloodWait
|
||||
|
||||
try:
|
||||
...
|
||||
except FloodWait:
|
||||
except FloodWait as e:
|
||||
...
|
||||
|
||||
303 - See Other
|
||||
---------------
|
||||
303 - SeeOther
|
||||
--------------
|
||||
|
||||
.. csv-table::
|
||||
:file: ../../../compiler/error/source/303_SEE_OTHER.tsv
|
||||
:delim: tab
|
||||
:header-rows: 1
|
||||
|
||||
400 - Bad Request
|
||||
-----------------
|
||||
400 - BadRequest
|
||||
----------------
|
||||
|
||||
.. csv-table::
|
||||
:file: ../../../compiler/error/source/400_BAD_REQUEST.tsv
|
||||
@@ -48,8 +47,8 @@ follow the usual *PascalCase* convention.
|
||||
:delim: tab
|
||||
:header-rows: 1
|
||||
|
||||
406 - Not Acceptable
|
||||
--------------------
|
||||
406 - NotAcceptable
|
||||
-------------------
|
||||
|
||||
.. csv-table::
|
||||
:file: ../../../compiler/error/source/406_NOT_ACCEPTABLE.tsv
|
||||
@@ -64,8 +63,8 @@ follow the usual *PascalCase* convention.
|
||||
:delim: tab
|
||||
:header-rows: 1
|
||||
|
||||
500 - Internal Server Error
|
||||
---------------------------
|
||||
500 - InternalServerError
|
||||
-------------------------
|
||||
|
||||
.. csv-table::
|
||||
:file: ../../../compiler/error/source/500_INTERNAL_SERVER_ERROR.tsv
|
||||
|
@@ -7,13 +7,12 @@ For a much more convenient way of registering callback functions have a look at
|
||||
In case you decided to manually create an handler, use :meth:`add_handler() <pyrogram.Client.add_handler>` to register
|
||||
it.
|
||||
|
||||
**Example:**
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 1, 10
|
||||
|
||||
from pyrogram import Client, MessageHandler
|
||||
|
||||
app = Client(...)
|
||||
app = Client("my_account")
|
||||
|
||||
|
||||
def dump(client, message):
|
||||
@@ -34,6 +33,7 @@ it.
|
||||
CallbackQueryHandler
|
||||
InlineQueryHandler
|
||||
UserStatusHandler
|
||||
PollHandler
|
||||
DisconnectHandler
|
||||
RawUpdateHandler
|
||||
|
||||
@@ -52,6 +52,9 @@ it.
|
||||
.. autoclass:: UserStatusHandler()
|
||||
:members:
|
||||
|
||||
.. autoclass:: PollHandler()
|
||||
:members:
|
||||
|
||||
.. autoclass:: DisconnectHandler()
|
||||
:members:
|
||||
|
||||
|
@@ -3,13 +3,12 @@ Available Methods
|
||||
|
||||
All Pyrogram methods listed here are bound to a :obj:`Client <pyrogram.Client>` instance.
|
||||
|
||||
**Example:**
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 6
|
||||
|
||||
from pyrogram import Client
|
||||
|
||||
app = Client(...)
|
||||
app = Client("my_account")
|
||||
|
||||
with app:
|
||||
app.send_message("haskell", "hi")
|
||||
|
@@ -3,9 +3,8 @@ Available Types
|
||||
|
||||
All Pyrogram types listed here are accessible through the main package directly.
|
||||
|
||||
**Example:**
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 1
|
||||
|
||||
from pyrogram import User, Message, ...
|
||||
|
||||
@@ -112,167 +111,72 @@ InputMessageContent
|
||||
------------
|
||||
|
||||
.. autoclass:: User()
|
||||
:members:
|
||||
|
||||
.. autoclass:: UserStatus()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Chat()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ChatPreview()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ChatPhoto()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ChatMember()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ChatMembers()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ChatPermissions()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Dialog()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Dialogs()
|
||||
:members:
|
||||
|
||||
.. Messages & Media
|
||||
----------------
|
||||
|
||||
.. autoclass:: Message()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Messages()
|
||||
:members:
|
||||
|
||||
.. autoclass:: MessageEntity()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Photo()
|
||||
:members:
|
||||
|
||||
.. autoclass:: PhotoSize()
|
||||
:members:
|
||||
|
||||
.. autoclass:: UserProfilePhotos()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Audio()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Document()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Animation()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Video()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Voice()
|
||||
:members:
|
||||
|
||||
.. autoclass:: VideoNote()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Contact()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Location()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Venue()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Sticker()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Game()
|
||||
:members:
|
||||
|
||||
.. autoclass:: Poll()
|
||||
:members:
|
||||
|
||||
.. autoclass:: PollOption()
|
||||
:members:
|
||||
|
||||
.. Keyboards
|
||||
---------
|
||||
|
||||
.. autoclass:: ReplyKeyboardMarkup()
|
||||
:members:
|
||||
|
||||
.. autoclass:: KeyboardButton()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ReplyKeyboardRemove()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InlineKeyboardMarkup()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InlineKeyboardButton()
|
||||
:members:
|
||||
|
||||
.. autoclass:: ForceReply()
|
||||
:members:
|
||||
|
||||
.. autoclass:: CallbackQuery()
|
||||
:members:
|
||||
|
||||
.. autoclass:: GameHighScore()
|
||||
:members:
|
||||
|
||||
.. autoclass:: CallbackGame()
|
||||
:members:
|
||||
|
||||
.. Input Media
|
||||
-----------
|
||||
|
||||
.. autoclass:: InputMedia()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputMediaPhoto()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputMediaVideo()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputMediaAudio()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputMediaAnimation()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputMediaDocument()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputPhoneContact()
|
||||
:members:
|
||||
|
||||
|
||||
.. Inline Mode
|
||||
-----------
|
||||
|
||||
.. autoclass:: InlineQuery()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InlineQueryResult()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InlineQueryResultArticle()
|
||||
:members:
|
||||
|
||||
.. InputMessageContent
|
||||
-------------------
|
||||
|
||||
.. autoclass:: InputMessageContent()
|
||||
:members:
|
||||
|
||||
.. autoclass:: InputTextMessageContent()
|
||||
:members:
|
||||
|
@@ -66,7 +66,7 @@ How the Documentation is Organized
|
||||
|
||||
Contents are organized into self-contained topics and can be all accessed from the sidebar, or by following them in
|
||||
order using the Next button at the end of each page. Here below you can, instead, find a list of the most relevant
|
||||
pages.
|
||||
pages for a quick access.
|
||||
|
||||
Getting Started
|
||||
^^^^^^^^^^^^^^^
|
||||
@@ -86,23 +86,27 @@ API Reference
|
||||
- `Client Class`_ - Details about the Client class.
|
||||
- `Available Methods`_ - A list of available high-level methods.
|
||||
- `Available Types`_ - A list of available high-level types.
|
||||
- `Bound Methods`_ - A list of convenient bound methods.
|
||||
|
||||
.. _Client Class: api/client
|
||||
.. _Available Methods: api/methods
|
||||
.. _Available Types: api/types
|
||||
.. _Bound Methods: api/bound-methods
|
||||
|
||||
Topics
|
||||
^^^^^^
|
||||
Relevant Topics
|
||||
^^^^^^^^^^^^^^^
|
||||
|
||||
- `Smart Plugins`_ - How to modularize your application.
|
||||
- `Advanced Usage`_ - How to use Telegram's raw API.
|
||||
- `Release Notes`_ - Release notes for Pyrogram releases.
|
||||
- `Pyrogram FAQ`_ - Answers to common Pyrogram questions.
|
||||
- `Pyrogram Glossary`_ - A list of words with brief explanations.
|
||||
|
||||
.. _Smart Plugins: topics/smart-plugins
|
||||
.. _Advanced Usage: topics/advanced-usage
|
||||
.. _Release Notes: topics/releases
|
||||
.. _Pyrogram FAQ: topics/faq
|
||||
.. _Pyrogram Glossary: topics/glossary
|
||||
|
||||
.. toctree::
|
||||
:hidden:
|
||||
@@ -128,6 +132,7 @@ Topics
|
||||
api/client
|
||||
api/methods
|
||||
api/types
|
||||
api/bound-methods
|
||||
api/handlers
|
||||
api/decorators
|
||||
api/filters
|
||||
@@ -152,6 +157,7 @@ Topics
|
||||
topics/voice-calls
|
||||
topics/releases
|
||||
topics/faq
|
||||
topics/glossary
|
||||
|
||||
.. toctree::
|
||||
:hidden:
|
||||
|
@@ -61,6 +61,7 @@ after the session name, which will be ``pyrogrambot.session`` for the example be
|
||||
"my_bot",
|
||||
bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
|
||||
)
|
||||
|
||||
app.run()
|
||||
|
||||
.. _project is set up: setup.html
|
||||
|
@@ -29,12 +29,12 @@ Install Pyrogram
|
||||
Bleeding Edge
|
||||
-------------
|
||||
|
||||
Things are constantly evolving in Pyrogram, although new releases are published only when enough changes are added,
|
||||
but this doesn't mean you can't try new features right now!
|
||||
Pyrogram is always evolving, although new releases on PyPI are published only when enough changes are added, but this
|
||||
doesn't mean you can't try new features right now!
|
||||
|
||||
In case you would like to try out the latest Pyrogram features and additions, the `GitHub repo`_ is always kept updated
|
||||
with new changes; you can install the development version straight from the ``develop`` branch using this command
|
||||
(note "develop.zip" in the link):
|
||||
In case you'd like to try out the latest Pyrogram features, the `GitHub repo`_ is always kept updated with new changes;
|
||||
you can install the development version straight from the ``develop`` branch using this command (note "develop.zip" in
|
||||
the link):
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
@@ -44,7 +44,8 @@ Asynchronous
|
||||
------------
|
||||
|
||||
Pyrogram heavily depends on IO-bound network code (it's a cloud-based messaging framework after all), and here's
|
||||
where asyncio shines the most by providing extra performance while running on a single OS-level thread only.
|
||||
where asyncio shines the most by providing extra performance and efficiency while running on a single OS-level thread
|
||||
only.
|
||||
|
||||
**A fully asynchronous variant of Pyrogram is therefore available** (Python 3.5.3+ required).
|
||||
Use this command to install (note "asyncio.zip" in the link):
|
||||
|
@@ -10,7 +10,7 @@ Get Pyrogram Real Fast
|
||||
|
||||
2. Get your own Telegram API key from https://my.telegram.org/apps.
|
||||
|
||||
3. Open your best text editor and paste the following:
|
||||
3. Open your best text editor and paste the following:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -40,10 +40,9 @@ Enjoy the API
|
||||
-------------
|
||||
|
||||
That was just a quick overview that barely scratched the surface!
|
||||
In the next few pages of the introduction, we'll take a much more in-depth look of what we have just done.
|
||||
In the next few pages of the introduction, we'll take a much more in-depth look of what we have just done above.
|
||||
|
||||
Feeling eager? You can take a shortcut to `Calling Methods`_ and come back later to learn some more
|
||||
details.
|
||||
Feeling eager to continue? You can take a shortcut to `Calling Methods`_ and come back later to learn some more details.
|
||||
|
||||
.. _community: //t.me/pyrogramchat
|
||||
.. _Calling Methods: ../start/invoking
|
@@ -1,51 +1,57 @@
|
||||
Error Handling
|
||||
==============
|
||||
|
||||
Errors are inevitable when working with the API, and they must be correctly handled with ``try..except`` blocks.
|
||||
|
||||
There are many errors that Telegram could return, but they all fall in one of these categories
|
||||
(which are in turn children of the ``RPCError`` superclass):
|
||||
|
||||
- `303 - See Other <../api/errors#see-other>`_
|
||||
- `400 - Bad Request <../api/errors#bad-request>`_
|
||||
- `401 - Unauthorized <../api/errors#unauthorized>`_
|
||||
- `403 - Forbidden <../api/errors#forbidden>`_
|
||||
- `406 - Not Acceptable <../api/errors#not-acceptable>`_
|
||||
- `420 - Flood <../api/errors#flood>`_
|
||||
- `500 - Internal Server Error <../api/errors#internal-server-error>`_
|
||||
|
||||
As stated above, there are really many (too many) errors, and in case Pyrogram does not know anything yet about a
|
||||
specific one, it raises a special ``520 Unknown Error`` exception and logs it
|
||||
in the ``unknown_errors.txt`` file. Users are invited to report these unknown errors.
|
||||
|
||||
Examples
|
||||
--------
|
||||
Errors are inevitable when working with the API, and they must be correctly handled with ``try..except`` blocks in order
|
||||
to control the behaviour of your application. Pyrogram errors all live inside the ``errors`` package:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.errors import (
|
||||
BadRequest, Flood, InternalServerError,
|
||||
SeeOther, Unauthorized, UnknownError
|
||||
)
|
||||
from pyrogram import errors
|
||||
|
||||
try:
|
||||
...
|
||||
except BadRequest:
|
||||
pass
|
||||
except Flood:
|
||||
pass
|
||||
except InternalServerError:
|
||||
pass
|
||||
except SeeOther:
|
||||
pass
|
||||
except Unauthorized:
|
||||
pass
|
||||
except UnknownError:
|
||||
pass
|
||||
RPCError
|
||||
--------
|
||||
|
||||
Exception objects may also contain some informative values.
|
||||
E.g.: ``FloodWait`` holds the amount of seconds you have to wait
|
||||
before you can try again. The value is always stored in the ``x`` field of the returned exception object:
|
||||
The father of all errors is named ``RPCError``. This error exists in form of a Python exception and is able to catch all
|
||||
Telegram API related errors.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.errors import RPCError
|
||||
|
||||
.. warning::
|
||||
|
||||
It must be noted that catching this error is bad practice, especially when no feedback is given (i.e. by
|
||||
logging/printing the full error traceback), because it makes it impossible to understand what went wrong.
|
||||
|
||||
Error Categories
|
||||
----------------
|
||||
|
||||
The ``RPCError`` packs together all the possible errors Telegram could raise, but to make things tidier, Pyrogram
|
||||
provides categories of errors, which are named after the common HTTP errors:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.errors import BadRequest, Forbidden, ...
|
||||
|
||||
- `303 - SeeOther <../api/errors#seeother>`_
|
||||
- `400 - BadRequest <../api/errors#badrequest>`_
|
||||
- `401 - Unauthorized <../api/errors#unauthorized>`_
|
||||
- `403 - Forbidden <../api/errors#forbidden>`_
|
||||
- `406 - NotAcceptable <../api/errors#notacceptable>`_
|
||||
- `420 - Flood <../api/errors#flood>`_
|
||||
- `500 - InternalServerError <../api/errors#internalservererror>`_
|
||||
|
||||
Unknown Errors
|
||||
--------------
|
||||
|
||||
In case Pyrogram does not know anything yet about a specific error, it raises a special ``520 - UnknownError`` exception
|
||||
and logs it in the ``unknown_errors.txt`` file. Users are invited to report these unknown errors.
|
||||
|
||||
Errors with Values
|
||||
------------------
|
||||
|
||||
Exception objects may also contain some informative values. For example, ``FloodWait`` holds the amount of seconds you
|
||||
have to wait before you can try again. The value is always stored in the ``x`` field of the returned exception object:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -55,4 +61,4 @@ before you can try again. The value is always stored in the ``x`` field of the r
|
||||
try:
|
||||
...
|
||||
except FloodWait as e:
|
||||
time.sleep(e.x)
|
||||
time.sleep(e.x) # Wait before trying again
|
||||
|
@@ -1,9 +1,12 @@
|
||||
Calling Methods
|
||||
===============
|
||||
|
||||
At this point, we have successfully `installed Pyrogram`_ and authorized_ our account; we are now pointing towards the
|
||||
At this point, we have successfully `installed Pyrogram`_ and authorized_ our account; we are now aiming towards the
|
||||
core of the library. It's time to start playing with the API!
|
||||
|
||||
Basic Usage
|
||||
-----------
|
||||
|
||||
Making API method calls with Pyrogram is very simple. Here's an example we are going to examine:
|
||||
|
||||
.. code-block:: python
|
||||
@@ -60,8 +63,8 @@ Context Manager
|
||||
---------------
|
||||
|
||||
You can also use Pyrogram's Client 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. The example above can be therefore rewritten in a much nicer way, this way:
|
||||
:meth:`start() <pyrogram.Client.start>` and :meth:`stop() <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:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
@@ -8,7 +8,7 @@ Defining Updates
|
||||
----------------
|
||||
|
||||
First, let's define what are these updates. As hinted already, updates are simply events that happen in your Telegram
|
||||
account (incoming messages, new members join, button presses, etc...), which are meant to notify you about a new
|
||||
account (incoming messages, new members join, bot button presses, etc...), which are meant to notify you about a new
|
||||
specific state that changed. These updates are handled by registering one or more callback functions in your app using
|
||||
`Handlers <../api/handlers>`_.
|
||||
|
||||
@@ -70,7 +70,7 @@ begins.
|
||||
|
||||
app.add_handler(my_handler)
|
||||
|
||||
Last one, the :meth:`run() <pyrogram.Client.run>` method. What this does is simply calling
|
||||
Last one, the :meth:`run() <pyrogram.Client.run>` method. What this does is simply call
|
||||
:meth:`start() <pyrogram.Client.start>` and a special method :meth:`idle() <pyrogram.Client.idle>` that keeps your main
|
||||
scripts alive until you press ``CTRL+C``; the client will be automatically stopped after that.
|
||||
|
||||
|
@@ -101,9 +101,9 @@ sending messages with IDs only thanks to cached access hashes.
|
||||
There are three different InputPeer types, one for each kind of Telegram entity.
|
||||
Whenever an InputPeer is needed you must pass one of these:
|
||||
|
||||
- `InputPeerUser <https://docs.pyrogram.ml/types/InputPeerUser>`_ - Users
|
||||
- `InputPeerChat <https://docs.pyrogram.ml/types/InputPeerChat>`_ - Basic Chats
|
||||
- `InputPeerChannel <https://docs.pyrogram.ml/types/InputPeerChannel>`_ - Either Channels or Supergroups
|
||||
- :obj:`InputPeerUser <../telegram/types/InputPeerUser>` - Users
|
||||
- :obj:`InputPeerChat <../telegram/types/InputPeerChat>` - Basic Chats
|
||||
- :obj:`InputPeerChannel <../telegram/types/InputPeerChannel>` - Either Channels or Supergroups
|
||||
|
||||
But you don't necessarily have to manually instantiate each object because, luckily for you, Pyrogram already provides
|
||||
:meth:`resolve_peer() <pyrogram.Client.resolve_peer>` as a convenience utility method that returns the correct InputPeer
|
||||
|
@@ -17,21 +17,51 @@ C. It enables you to easily create custom applications for both user and bot ide
|
||||
.. _Telegram: https://telegram.org
|
||||
.. _MTProto API: https://core.telegram.org/api#telegram-api
|
||||
|
||||
What does "Pyrogram" mean?
|
||||
--------------------------
|
||||
What does the name mean?
|
||||
------------------------
|
||||
|
||||
The word "Pyrogram" is composed by **pyro**, which comes from the Greek word *πῦρ (pyr)*, meaning fire, and **gram**,
|
||||
from *Telegram*. The word *pyro* itself is built from *Python*, **py** for short, and the suffix **ro** to come up with
|
||||
the word *fire*, which also inspired the project logo.
|
||||
|
||||
How old is Pyrogram?
|
||||
--------------------
|
||||
How old is the project?
|
||||
-----------------------
|
||||
|
||||
Pyrogram was first released on December 12, 2017. The actual work on the framework began roughly three months prior the
|
||||
initial public release on `GitHub`_.
|
||||
|
||||
.. _GitHub: https://github.com/pyrogram/pyrogram
|
||||
|
||||
Why Pyrogram?
|
||||
-------------
|
||||
|
||||
- **Easy**: You can install Pyrogram with pip and start building your applications right away.
|
||||
- **Elegant**: Low-level details are abstracted and re-presented in a much nicer and easier way.
|
||||
- **Fast**: Crypto parts are boosted up by TgCrypto_, a high-performance library written in pure C.
|
||||
- **Documented**: Pyrogram API methods, types and public interfaces are well documented.
|
||||
- **Type-hinted**: Exposed Pyrogram types and method parameters are all type-hinted.
|
||||
- **Updated**, to make use of the latest Telegram API version and features.
|
||||
- **Pluggable**: The `Smart Plugin`_ system allows to write components with minimal boilerplate code.
|
||||
- **Comprehensive**: Execute any `advanced action`_ an official client is able to do, and even more.
|
||||
|
||||
.. _TgCrypto: https://github.com/pyrogram/tgcrypto
|
||||
.. _Smart Plugin: smart-plugins
|
||||
.. _advanced action: advanced-usage
|
||||
|
||||
What can MTProto do more than the Bot API?
|
||||
------------------------------------------
|
||||
|
||||
- Authorize both user and bot identities.
|
||||
- Upload & download any file, up to 1500 MB each.
|
||||
- Has less overhead due to direct connections to the actual Telegram servers.
|
||||
- Run multiple sessions at once, up to 10 per account (either bot or user).
|
||||
- Get information about any public chat by usernames, even if not a member.
|
||||
- Obtain information about any message existing in a chat using message ids.
|
||||
- retrieve the whole chat members list of either public or private chats.
|
||||
- Receive extra updates, such as the one about a user name change.
|
||||
- More meaningful errors in case something went wrong.
|
||||
- Get API version updates, and thus new features, sooner.
|
||||
|
||||
Why do I need an API key for bots?
|
||||
----------------------------------
|
||||
|
||||
@@ -46,6 +76,8 @@ Using MTProto is the only way to communicate with the actual Telegram servers, a
|
||||
identify applications by means of a unique key; the bot token identifies a bot as a user and replaces the user's phone
|
||||
number only.
|
||||
|
||||
Why is the main API (MTProto) superiod
|
||||
|
||||
I started a client but nothing happens!
|
||||
---------------------------------------
|
||||
|
||||
@@ -65,7 +97,7 @@ in a bunch of seconds:
|
||||
|
||||
.. _you need a proxy: proxy
|
||||
|
||||
I keep getting [400 PEER_ID_INVALID] error!
|
||||
I keep getting PEER_ID_INVALID error!
|
||||
-------------------------------------------
|
||||
|
||||
The error in question is **[400 PEER_ID_INVALID]: The id/access_hash combination is invalid**, and could mean several
|
||||
@@ -104,4 +136,18 @@ mistakes by either the automatic systems or a moderator. In such cases you can k
|
||||
recover@telegram.org, contact `@smstelegram`_ on Twitter or use `this form`_.
|
||||
|
||||
.. _@smstelegram: https://twitter.com/smstelegram
|
||||
.. _this form: https://telegram.org/support
|
||||
.. _this form: https://telegram.org/support
|
||||
|
||||
About the License
|
||||
-----------------
|
||||
|
||||
.. image:: https://www.gnu.org/graphics/lgplv3-with-text-154x68.png
|
||||
:align: left
|
||||
|
||||
Pyrogram is free software and is currently licensed under the terms of the GNU Lesser General Public License v3 or later
|
||||
(LGPLv3+). In short: you may use, redistribute and/or modify it provided that modifications are described and licensed
|
||||
for free under LGPLv3+.
|
||||
|
||||
In other words: you can use and integrate Pyrogram into your own code --- either open source, under the same or a
|
||||
different licence or even proprietary --- without being required to release the source code of your own applications.
|
||||
However, any modifications to the library itself are required to be published for free under the same LGPLv3+ license.
|
@@ -7,6 +7,9 @@ but there's much more than that to come.
|
||||
Here we'll discuss about :class:`Filters <pyrogram.Filters>`. Filters enable a fine-grain control over what kind of
|
||||
updates are allowed or not to be passed in your callback functions, based on their inner details.
|
||||
|
||||
Single Filters
|
||||
--------------
|
||||
|
||||
Let's start right away with a simple example:
|
||||
|
||||
- This example will show you how to **only** handle messages containing an :obj:`Audio <pyrogram.Audio>` object and
|
||||
|
53
docs/source/topics/glossary.rst
Normal file
53
docs/source/topics/glossary.rst
Normal file
@@ -0,0 +1,53 @@
|
||||
Pyrogram Glossary
|
||||
-----------------
|
||||
|
||||
This page contains a list of common words with brief explanations related to Pyrogram and, to some extent, Telegram in
|
||||
general.
|
||||
|
||||
.. glossary::
|
||||
|
||||
API
|
||||
Application Programming Interface: a set of methods, protocols and tools that make it easier to develop programs
|
||||
by providing useful building blocks to the developer.
|
||||
|
||||
API key
|
||||
A secret code used to authenticate and/or authorize a specific application to Telegram in order for it to
|
||||
control how the API is being used, for example, to prevent abuses of the API.
|
||||
|
||||
MTProto
|
||||
The name of the custom-made, open encryption protocol by Telegram, implemented in Pyrogram.
|
||||
|
||||
MTProto API
|
||||
The Telegram main API Pyrogram makes use of, which is able to connect both users and normal bots to Telegram
|
||||
using MTProto as application layer protocol and execute any method Telegram provides from its public schema.
|
||||
|
||||
Bot API
|
||||
The `Telegram Bot API`_ that is able to only connect normal bots to Telegram using HTTP as application layer
|
||||
protocol and allows to execute a subset of the main Telegram API.
|
||||
|
||||
Pyrogrammer
|
||||
A developer that uses Pyrogram to build Telegram applications.
|
||||
|
||||
Userbot
|
||||
Also known as *user bot* or *ubot* for short, is a user logged in by third-party Telegram libraries --- such as
|
||||
Pyrogram --- to automate some behaviours, like sending messages or reacting to text commands or any other event.
|
||||
|
||||
Session
|
||||
Also known as *login session*, is a strictly personal piece of information created and held by both parties
|
||||
(client and server) which is used to grant permission into a single account without having to start a new
|
||||
authorization process from scratch.
|
||||
|
||||
Callback
|
||||
Also known as *callback function*, is a user-defined generic function that *can be* registered to and then
|
||||
called-back by the framework when specific events occurs.
|
||||
|
||||
Handler
|
||||
An object that wraps around a callback function that is *actually meant* to be registered into the framework,
|
||||
which will then be able to handle a specific kind of events, such as a new incoming message, for example.
|
||||
|
||||
Decorator
|
||||
Also known as *function decorator*, in Python, is a callable object that is used to modify another function.
|
||||
Decorators in Pyrogram are used to automatically register callback functions for `handling updates`_.
|
||||
|
||||
.. _Telegram Bot API: https://core.telegram.org/bots/api
|
||||
.. _handling updates: ../start/updates
|
@@ -30,7 +30,7 @@ after importing your modules, like this:
|
||||
handlers.py
|
||||
main.py
|
||||
|
||||
- ``handlers.py``
|
||||
- ``handlers.py``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -41,7 +41,7 @@ after importing your modules, like this:
|
||||
def echo_reversed(client, message):
|
||||
message.reply(message.text[::-1])
|
||||
|
||||
- ``main.py``
|
||||
- ``main.py``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -91,7 +91,7 @@ Setting up your Pyrogram project to accommodate Smart Plugins is pretty straight
|
||||
config.ini
|
||||
main.py
|
||||
|
||||
- ``plugins/handlers.py``
|
||||
- ``plugins/handlers.py``
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 4, 9
|
||||
@@ -108,14 +108,14 @@ Setting up your Pyrogram project to accommodate Smart Plugins is pretty straight
|
||||
def echo_reversed(client, message):
|
||||
message.reply(message.text[::-1])
|
||||
|
||||
- ``config.ini``
|
||||
- ``config.ini``
|
||||
|
||||
.. code-block:: ini
|
||||
|
||||
[plugins]
|
||||
root = plugins
|
||||
|
||||
- ``main.py``
|
||||
- ``main.py``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -199,8 +199,8 @@ also organized in subfolders:
|
||||
...
|
||||
...
|
||||
|
||||
- Load every handler from every module, namely *plugins0.py*, *plugins1.py* and *plugins2.py* in alphabetical order
|
||||
(files) and definition order (handlers inside files):
|
||||
- Load every handler from every module, namely *plugins0.py*, *plugins1.py* and *plugins2.py* in alphabetical order
|
||||
(files) and definition order (handlers inside files):
|
||||
|
||||
Using *config.ini* file:
|
||||
|
||||
@@ -217,7 +217,7 @@ also organized in subfolders:
|
||||
|
||||
Client("my_account", plugins=plugins).run()
|
||||
|
||||
- Load only handlers defined inside *plugins2.py* and *plugins0.py*, in this order:
|
||||
- Load only handlers defined inside *plugins2.py* and *plugins0.py*, in this order:
|
||||
|
||||
Using *config.ini* file:
|
||||
|
||||
@@ -243,7 +243,7 @@ also organized in subfolders:
|
||||
|
||||
Client("my_account", plugins=plugins).run()
|
||||
|
||||
- Load everything except the handlers inside *plugins2.py*:
|
||||
- Load everything except the handlers inside *plugins2.py*:
|
||||
|
||||
Using *config.ini* file:
|
||||
|
||||
@@ -264,7 +264,7 @@ also organized in subfolders:
|
||||
|
||||
Client("my_account", plugins=plugins).run()
|
||||
|
||||
- Load only *fn3*, *fn1* and *fn2* (in this order) from *plugins1.py*:
|
||||
- Load only *fn3*, *fn1* and *fn2* (in this order) from *plugins1.py*:
|
||||
|
||||
Using *config.ini* file:
|
||||
|
||||
@@ -297,7 +297,7 @@ Each function decorated with the usual ``on_message`` decorator (or any other de
|
||||
*(handler: Handler, group: int)*. The actual callback function is therefore stored inside the handler's *callback*
|
||||
attribute. Here's an example:
|
||||
|
||||
- ``plugins/handlers.py``
|
||||
- ``plugins/handlers.py``
|
||||
|
||||
.. code-block:: python
|
||||
:emphasize-lines: 5, 6
|
||||
@@ -321,7 +321,7 @@ In order to unload a plugin, or any other handler, all you need to do is obtain
|
||||
relevant module and call :meth:`remove_handler() <pyrogram.Client.remove_handler>` Client's method with your function
|
||||
name preceded by the star ``*`` operator as argument. Example:
|
||||
|
||||
- ``main.py``
|
||||
- ``main.py``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -345,7 +345,7 @@ Loading
|
||||
Similarly to the unloading process, in order to load again a previously unloaded plugin you do the same, but this time
|
||||
using :meth:`add_handler() <pyrogram.Client.add_handler>` instead. Example:
|
||||
|
||||
- ``main.py``
|
||||
- ``main.py``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
Reference in New Issue
Block a user