From 357253b63e02ca923197a9fd98bdeb1d0948ced2 Mon Sep 17 00:00:00 2001 From: YoilyL Date: Sun, 1 Jul 2018 21:34:05 +0300 Subject: [PATCH 01/10] added option to reverse get_history order added an argument `reverse` to get_history which if set to True returns the messages from first to last instead of from newest to oldest. --- pyrogram/client/methods/messages/get_history.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index 4089dde9..f55a64ed 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -27,7 +27,8 @@ class GetHistory(BaseClient): offset: int = 0, limit: int = 100, offset_id: int = 0, - offset_date: int = 0): + offset_date: int = 0, + reverse: bool = False): """Use this method to retrieve the history of a chat. You can get up to 100 messages at once. @@ -52,6 +53,9 @@ class GetHistory(BaseClient): offset_date (``int``, *optional*): Pass a date in Unix time as offset to retrieve only older messages starting from that date. + + reverse (``bool``, *optional*): + get the messages in order from first to last (instead of newest to oldest). Returns: On success, a :obj:`Messages ` object is returned. @@ -59,7 +63,9 @@ class GetHistory(BaseClient): Raises: :class:`Error ` """ - + if reverse: + offset = -limit*(offset + 1) + if offset_id: offset_id +=1 r = self.send( functions.messages.GetHistory( peer=self.resolve_peer(chat_id), @@ -107,5 +113,5 @@ class GetHistory(BaseClient): return pyrogram.Messages( total_count=getattr(r, "count", len(r.messages)), - messages=messages + messages=messages if not reverse else messages[::-1] ) From f7aae28ae9a7d4bf3bbcd9c15c22bea57b10c4bc Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 2 Jul 2018 02:48:58 +0200 Subject: [PATCH 02/10] Improve get_history --- pyrogram/client/methods/messages/get_history.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index f55a64ed..48619e9b 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -24,11 +24,12 @@ from ...ext import BaseClient, utils class GetHistory(BaseClient): def get_history(self, chat_id: int or str, - offset: int = 0, limit: int = 100, + offset: int = 0, offset_id: int = 0, offset_date: int = 0, - reverse: bool = False): + add_offset: int = 0, + downwards: bool = False): """Use this method to retrieve the history of a chat. You can get up to 100 messages at once. @@ -53,9 +54,6 @@ class GetHistory(BaseClient): offset_date (``int``, *optional*): Pass a date in Unix time as offset to retrieve only older messages starting from that date. - - reverse (``bool``, *optional*): - get the messages in order from first to last (instead of newest to oldest). Returns: On success, a :obj:`Messages ` object is returned. @@ -63,15 +61,13 @@ class GetHistory(BaseClient): Raises: :class:`Error ` """ - if reverse: - offset = -limit*(offset + 1) - if offset_id: offset_id +=1 + # TODO: Docstrings r = self.send( functions.messages.GetHistory( peer=self.resolve_peer(chat_id), offset_id=offset_id, offset_date=offset_date, - add_offset=offset, + add_offset=offset + add_offset - (limit if downwards else 0), limit=limit, max_id=0, min_id=0, @@ -113,5 +109,5 @@ class GetHistory(BaseClient): return pyrogram.Messages( total_count=getattr(r, "count", len(r.messages)), - messages=messages if not reverse else messages[::-1] + messages=messages[::-1] ) From 74477f2cac9aae5566915659887a5dae7f863e7b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 26 Dec 2018 09:18:48 +0100 Subject: [PATCH 03/10] Add PHONE_NUMBER_FLOOD error --- compiler/error/source/400_BAD_REQUEST.tsv | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/error/source/400_BAD_REQUEST.tsv b/compiler/error/source/400_BAD_REQUEST.tsv index c1a02761..c0a5da73 100644 --- a/compiler/error/source/400_BAD_REQUEST.tsv +++ b/compiler/error/source/400_BAD_REQUEST.tsv @@ -79,4 +79,5 @@ BOTS_TOO_MUCH The chat has too many bots USER_ADMIN_INVALID The action requires admin privileges INPUT_USER_DEACTIVATED The target user has been deactivated PASSWORD_RECOVERY_NA The password recovery e-mail is not available -PASSWORD_EMPTY The password entered is empty \ No newline at end of file +PASSWORD_EMPTY The password entered is empty +PHONE_NUMBER_FLOOD This number has tried to login too many times \ No newline at end of file From 127123006f97eda979241c40427393f49fc391a1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 26 Dec 2018 19:44:22 +0100 Subject: [PATCH 04/10] Add TestServers documentation page --- docs/source/index.rst | 5 ++-- docs/source/resources/TestServers.rst | 39 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 docs/source/resources/TestServers.rst diff --git a/docs/source/index.rst b/docs/source/index.rst index c6ff71b8..34456b21 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -25,7 +25,7 @@ Welcome to Pyrogram
- Scheme Layer @@ -64,7 +64,7 @@ Features - **Easy to use**: You can easily install Pyrogram using pip and start building your app right away. - **High-level**: The low-level details of MTProto are abstracted and automatically handled. - **Fast**: Crypto parts are boosted up by TgCrypto_, a high-performance library written in pure C. -- **Updated** to the latest Telegram API version, currently Layer 82 on top of MTProto 2.0. +- **Updated** to the latest Telegram API version, currently Layer 91 on top of MTProto 2.0. - **Documented**: The Pyrogram API is well documented and resembles the Telegram Bot API. - **Full API**, allowing to execute any advanced action an official client is able to do, and more. @@ -89,6 +89,7 @@ To get started, press the Next button. resources/CustomizeSessions resources/TgCrypto resources/TextFormatting + resources/TestServers resources/SOCKS5Proxy resources/BotsInteraction resources/ErrorHandling diff --git a/docs/source/resources/TestServers.rst b/docs/source/resources/TestServers.rst new file mode 100644 index 00000000..2f82f24c --- /dev/null +++ b/docs/source/resources/TestServers.rst @@ -0,0 +1,39 @@ +Test Servers +============ + +If you wish to test your application in a separate environment, Pyrogram is able to authorize your account into +Telegram's test servers without hassle. All you need to do is start a new session (e.g.: "my_account_test") using +``test_mode=True``: + +.. code-block:: python + + from pyrogram import Client + + with Client("my_account_test", test_mode=True) as app: + print(app.get_me()) + +.. note:: + + If this is the first time you login into test servers, you will be asked to register your account first. + Don't worry about your contacts and chats, they will be kept untouched inside the production environment; + accounts authorized on test servers reside in a different, parallel instance of a Telegram database. + +Test Mode in Official Apps +-------------------------- + +You can also login yourself into test servers using official desktop apps, such as Webogram and TDesktop: + +- **Webogram**: Login here: https://web.telegram.org/?test=1 +- **TDesktop**: Open settings and type ``testmode``. + +Test Numbers +------------ + +Beside normal numbers, the test environment allows you to login with reserved test numbers. +Valid phone numbers follow the pattern ``99966XYYYY``, where ``X`` is the DC number (1 to 3) and ``YYYY`` are random +numbers. Users with such numbers always get ``XXXXX`` as the confirmation code (the DC number, repeated five times). + +.. important:: + + Do not store any important or private information in such test users' accounts; anyone can make use of the + simplified authorization mechanism and login at any time. From 39a458a5f85f21afe90439547b3743a94c195cdb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 26 Dec 2018 20:08:11 +0100 Subject: [PATCH 05/10] Add Changelog documentation page --- docs/source/index.rst | 3 ++- docs/source/resources/Changelog.rst | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 docs/source/resources/Changelog.rst diff --git a/docs/source/index.rst b/docs/source/index.rst index 34456b21..8234374c 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -89,10 +89,11 @@ To get started, press the Next button. resources/CustomizeSessions resources/TgCrypto resources/TextFormatting - resources/TestServers resources/SOCKS5Proxy resources/BotsInteraction resources/ErrorHandling + resources/TestServers + resources/Changelog .. toctree:: :hidden: diff --git a/docs/source/resources/Changelog.rst b/docs/source/resources/Changelog.rst new file mode 100644 index 00000000..77daa21e --- /dev/null +++ b/docs/source/resources/Changelog.rst @@ -0,0 +1,5 @@ +Changelog +========= + +Currently, all Pyrogram release notes live inside the GitHub repository web page: +https://github.com/pyrogram/pyrogram/releases \ No newline at end of file From e4bead530cfb330b8c78c0ed86d620e019e8b00b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 27 Dec 2018 14:20:59 +0100 Subject: [PATCH 06/10] Automatically redirect to GitHub release notes in 10 seconds --- docs/source/resources/Changelog.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/source/resources/Changelog.rst b/docs/source/resources/Changelog.rst index 77daa21e..732a1311 100644 --- a/docs/source/resources/Changelog.rst +++ b/docs/source/resources/Changelog.rst @@ -2,4 +2,10 @@ Changelog ========= Currently, all Pyrogram release notes live inside the GitHub repository web page: -https://github.com/pyrogram/pyrogram/releases \ No newline at end of file +https://github.com/pyrogram/pyrogram/releases + +(You will be automatically redirected in 10 seconds.) + +.. raw:: html + + \ No newline at end of file From 4a01745c68c825bddfdb9bed9a20d4d2cfb904f5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 27 Dec 2018 22:58:02 +0100 Subject: [PATCH 07/10] Add more error classes to docs --- docs/source/errors/Forbidden.rst | 8 ++++++++ docs/source/errors/NotAcceptable.rst | 8 ++++++++ docs/source/pyrogram/Error.rst | 2 ++ docs/source/resources/ErrorHandling.rst | 9 ++++----- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 docs/source/errors/Forbidden.rst create mode 100644 docs/source/errors/NotAcceptable.rst diff --git a/docs/source/errors/Forbidden.rst b/docs/source/errors/Forbidden.rst new file mode 100644 index 00000000..fd2e08ce --- /dev/null +++ b/docs/source/errors/Forbidden.rst @@ -0,0 +1,8 @@ +Forbidden +========= + +.. module:: pyrogram.api.errors.Forbidden + +.. automodule:: pyrogram.api.errors.exceptions.forbidden_403 + :members: + :show-inheritance: diff --git a/docs/source/errors/NotAcceptable.rst b/docs/source/errors/NotAcceptable.rst new file mode 100644 index 00000000..66ebaf72 --- /dev/null +++ b/docs/source/errors/NotAcceptable.rst @@ -0,0 +1,8 @@ +Not Acceptable +============== + +.. module:: pyrogram.api.errors.NotAcceptable + +.. automodule:: pyrogram.api.errors.exceptions.not_acceptable_406 + :members: + :show-inheritance: diff --git a/docs/source/pyrogram/Error.rst b/docs/source/pyrogram/Error.rst index b5474e73..2ec1159d 100644 --- a/docs/source/pyrogram/Error.rst +++ b/docs/source/pyrogram/Error.rst @@ -9,6 +9,8 @@ Error ../errors/SeeOther ../errors/BadRequest ../errors/Unauthorized + ../errors/Forbidden + ../errors/NotAcceptable ../errors/Flood ../errors/InternalServerError ../errors/UnknownError diff --git a/docs/source/resources/ErrorHandling.rst b/docs/source/resources/ErrorHandling.rst index 0d5cf6f9..f2a77b7a 100644 --- a/docs/source/resources/ErrorHandling.rst +++ b/docs/source/resources/ErrorHandling.rst @@ -1,15 +1,16 @@ Error Handling ============== -Errors are inevitable when working with the API, and they must be correctly handled by -the use of ``try..except`` blocks. +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 five exception categories +There are many errors that Telegram could return, but they all fall in one of these categories (which are in turn children of the :obj:`pyrogram.Error` superclass) - :obj:`303 See Other ` - :obj:`400 Bad Request ` - :obj:`401 Unauthorized ` +- :obj:`403 Forbidden ` +- :obj:`406 Not Acceptable ` - :obj:`420 Flood ` - :obj:`500 Internal Server Error ` @@ -56,5 +57,3 @@ before you can try again. The value is always stored in the ``x`` field of the r ... except FloodWait as e: time.sleep(e.x) - -**TODO: Better explanation on how to deal with exceptions** \ No newline at end of file From decaa9e7f03aadc24d725dd800e781c555219d53 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 27 Dec 2018 23:55:56 +0100 Subject: [PATCH 08/10] Rename downwards to reversed --- .../client/methods/messages/get_history.py | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index 845b9066..ce12e9b0 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -26,13 +26,11 @@ from ...ext import BaseClient class GetHistory(BaseClient): def get_history(self, chat_id: Union[int, str], - offset: int = 0, limit: int = 100, offset: int = 0, offset_id: int = 0, offset_date: int = 0, - add_offset: int = 0, - downwards: bool = False): + reversed: bool = False): """Use this method to retrieve the history of a chat. You can get up to 100 messages at once. @@ -43,34 +41,38 @@ class GetHistory(BaseClient): For your personal cloud (Saved Messages) you can simply use "me" or "self". For a contact that exists in your Telegram address book you can use his phone number (str). - offset (``int``, *optional*) - Sequential number of the first message to be returned. - Defaults to 0 (most recent message). - limit (``int``, *optional*): Limits the number of messages to be retrieved. By default, the first 100 messages are returned. + offset (``int``, *optional*) + Sequential number of the first message to be returned. Defaults to 0 (most recent message). + Negative values are also accepted and become useful in case you set offset_id or offset_date. + offset_id (``int``, *optional*): Pass a message identifier as offset to retrieve only older messages starting from that message. offset_date (``int``, *optional*): Pass a date in Unix time as offset to retrieve only older messages starting from that date. + reversed (``bool``, *optional*): + Pass True to retrieve the messages in reversed order (from older to most recent). + Returns: On success, a :obj:`Messages ` object is returned. Raises: :class:`Error ` in case of a Telegram RPC error. """ - return pyrogram.Messages._parse( + + messages = pyrogram.Messages._parse( self, self.send( functions.messages.GetHistory( peer=self.resolve_peer(chat_id), offset_id=offset_id, offset_date=offset_date, - add_offset=offset, + add_offset=offset - (limit if reversed else 0), limit=limit, max_id=0, min_id=0, @@ -78,3 +80,8 @@ class GetHistory(BaseClient): ) ) ) + + if reversed: + messages.messages.reverse() + + return messages From 3814471af409836cc4855ec402f5aa9dd2aa014d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 27 Dec 2018 23:56:45 +0100 Subject: [PATCH 09/10] Change get_dialogs behaviour It now accepts an offset_date instead of an offset_dialog --- pyrogram/client/methods/chats/get_dialogs.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index 0e04423c..757cf5bc 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -23,7 +23,7 @@ from ...ext import BaseClient class GetDialogs(BaseClient): def get_dialogs(self, - offset_dialog: "pyrogram.Dialog" = None, + offset_date: int = 0, limit: int = 100, pinned_only: bool = False) -> "pyrogram.Dialogs": """Use this method to get the user's dialogs @@ -31,13 +31,13 @@ class GetDialogs(BaseClient): You can get up to 100 dialogs at once. Args: - offset_dialog (:obj:`Dialog`): - Sequential Dialog of the first dialog to be returned. - Defaults to None (start from the beginning). + offset_date (``int``): + The offset date in Unix time taken from the top message of a :obj:`Dialog`. + Defaults to 0. Valid for non-pinned dialogs only. limit (``str``, *optional*): Limits the number of dialogs to be retrieved. - Defaults to 100. + Defaults to 100. Valid for non-pinned dialogs only. pinned_only (``bool``, *optional*): Pass True if you want to get only pinned dialogs. @@ -55,7 +55,7 @@ class GetDialogs(BaseClient): else: r = self.send( functions.messages.GetDialogs( - offset_date=offset_dialog.top_message.date if offset_dialog else 0, + offset_date=offset_date, offset_id=0, offset_peer=types.InputPeerEmpty(), limit=limit, From 9fadbbd7286fec739e9a120f576cf708aeb0188e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 28 Dec 2018 00:37:20 +0100 Subject: [PATCH 10/10] Update develop version --- pyrogram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 6400abc1..e3628fe4 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -23,7 +23,7 @@ __copyright__ = "Copyright (C) 2017-2018 Dan Tès