From 8e5ec7165a580ca406c6c8c3db3e904c7766f771 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 14:22:50 +0200 Subject: [PATCH 01/14] Update README.rst --- README.rst | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/README.rst b/README.rst index b1015db2..e704d46a 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ |header| -Pyrogram |twitter| -================== +Pyrogram +======== .. code-block:: python @@ -26,8 +26,8 @@ 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 76 running on MTProto 2.0. -- **Documented**: Pyrogram API methods are documented and resemble the Telegram Bot API. +- **Updated** to the latest Telegram API version, currently Layer 79 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. Requirements @@ -54,7 +54,7 @@ Getting Started Contributing ------------ -Pyrogram is brand new! **You are welcome to try it and help make it better** by either submitting pull +Pyrogram is brand new, and **you are welcome to try it and help make it even better** by either submitting pull requests or reporting issues/bugs as well as suggesting best practices, ideas, enhancements on both code and documentation. Any help is appreciated! @@ -100,28 +100,25 @@ Copyright & License

- Scheme Layer 76 + Scheme Layer - TgCrypto

-.. |twitter| image:: https://media.pyrogram.ml/images/twitter.svg - :target: https://twitter.com/intent/tweet?text=Build%20custom%20Telegram%20applications%20with%20Pyrogram&url=https://github.com/pyrogram/pyrogram&hashtags=Telegram,MTProto,Python - .. |logo| image:: https://pyrogram.ml/images/logo.png :target: https://pyrogram.ml :alt: Pyrogram .. |description| replace:: **Telegram MTProto API Client Library for Python** -.. |scheme| image:: https://www.pyrogram.ml/images/scheme.svg +.. |scheme| image:: "https://img.shields.io/badge/SCHEME-LAYER%2079-eda738.svg?longCache=true&style=for-the-badge&colorA=262b30" :target: compiler/api/source/main_api.tl - :alt: Scheme Layer 76 + :alt: Scheme Layer -.. |tgcrypto| image:: https://www.pyrogram.ml/images/tgcrypto.svg +.. |tgcrypto| image:: "https://img.shields.io/badge/TGCRYPTO-V1.0.4-eda738.svg?longCache=true&style=for-the-badge&colorA=262b30" :target: https://github.com/pyrogram/tgcrypto :alt: TgCrypto From 24572fcb65a33567b9e0040e28e1b87d33142d0f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 14:26:52 +0200 Subject: [PATCH 02/14] Remove requirements_extras.txt from MANIFEST.in --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index a1d19d94..f818e13a 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,5 @@ ## Include -include COPYING COPYING.lesser NOTICE requirements.txt requirements_extras.txt +include COPYING COPYING.lesser NOTICE requirements.txt recursive-include compiler *.py *.tl *.tsv *.txt ## Exclude From 66f91b1b63ac54f1fd06fe7f869790315ea6fef8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 14:31:06 +0200 Subject: [PATCH 03/14] Update dev version --- pyrogram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 531da722..dce17c91 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -23,7 +23,7 @@ __copyright__ = "Copyright (C) 2017-2018 Dan Tès Date: Sun, 24 Jun 2018 16:11:22 +0200 Subject: [PATCH 04/14] Pass the phone number when calling the phone_code callback function --- pyrogram/client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 24bf09eb..91bdc1a6 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -469,7 +469,7 @@ class Client(Methods, BaseClient): self.phone_code = ( input("Enter phone code: ") if self.phone_code is None else self.phone_code if type(self.phone_code) is str - else str(self.phone_code()) + else str(self.phone_code(self.phone_number)) ) try: From 44f71f096fe9e90fffca098ad9b37d54d025bbd0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 16:53:07 +0200 Subject: [PATCH 05/14] Better docs for phone_code argument --- pyrogram/client/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 91bdc1a6..2312d6dd 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -93,8 +93,8 @@ class Client(Methods, BaseClient): entering it manually. Only applicable for new sessions. phone_code (``str`` | ``callable``, *optional*): - Pass the phone code as string (for test numbers only), or pass a callback function - which must return the correct phone code as string (e.g., "12345"). + Pass the phone code as string (for test numbers only), or pass a callback function which accepts + a single positional argument *(phone_number)* and must return the correct phone code (e.g., "12345"). Only applicable for new sessions. password (``str``, *optional*): From 50ab50eb529c88932aa2e3a1b1c0cfa62f7d65c6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:13:17 +0200 Subject: [PATCH 06/14] Update AutoAuthorization page --- docs/source/resources/AutoAuthorization.rst | 24 ++++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/source/resources/AutoAuthorization.rst b/docs/source/resources/AutoAuthorization.rst index 73504f80..d7a099fe 100644 --- a/docs/source/resources/AutoAuthorization.rst +++ b/docs/source/resources/AutoAuthorization.rst @@ -14,27 +14,31 @@ Log In To automate the **Log In** process, pass your ``phone_number`` and ``password`` (if you have one) in the Client parameters. If you want to retrieve the phone code programmatically, pass a callback function in the ``phone_code`` field — this -function must return the correct phone code as string (e.g., "12345") — otherwise, ignore this parameter, Pyrogram will -ask you to input the phone code manually. +function accepts a single positional argument (phone_number) and must return the correct phone code (e.g., "12345") +— otherwise, ignore this parameter, Pyrogram will ask you to input the phone code manually. + +Example: .. code-block:: python from pyrogram import Client - def phone_code_callback(): + def phone_code_callback(phone_number): code = ... # Get your code programmatically - return code # Must be string, e.g., "12345" + return code # e.g., "12345" app = Client( session_name="example", phone_number="39**********", - phone_code=phone_code_callback, + phone_code=phone_code_callback, # Note the missing parentheses password="password" # (if you have one) ) app.start() + print(app.get_me()) + app.stop() Sign Up @@ -44,23 +48,27 @@ To automate the **Sign Up** process (i.e., automatically create a new Telegram a ``first_name`` and ``last_name`` fields alongside the other parameters; they will be used to automatically create a new Telegram account in case the phone number you passed is not registered yet. +Example: + .. code-block:: python from pyrogram import Client - def phone_code_callback(): + def phone_code_callback(phone_number): code = ... # Get your code programmatically - return code # Must be string, e.g., "12345" + return code # e.g., "12345" app = Client( session_name="example", phone_number="39**********", - phone_code=phone_code_callback, + phone_code=phone_code_callback, # Note the missing parentheses first_name="Pyrogram", last_name="" # Can be an empty string ) app.start() + print(app.get_me()) + app.stop() \ No newline at end of file From 69d1432251c15a150d639d2a547729b5d3ee105a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:47:29 +0200 Subject: [PATCH 07/14] Allow user-defined values for retries and timeout --- pyrogram/client/client.py | 10 ++++++++-- pyrogram/client/ext/base_client.py | 3 ++- pyrogram/session/session.py | 10 +++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 2312d6dd..1897e8d3 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -817,7 +817,7 @@ class Client(Methods, BaseClient): log.debug("{} stopped".format(name)) - def send(self, data: Object): + def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): """Use this method to send Raw Function queries. This method makes possible to manually call every single Telegram API method in a low-level manner. @@ -828,13 +828,19 @@ class Client(Methods, BaseClient): data (``Object``): The API Scheme function filled with proper arguments. + retries (``int``): + Number of retries. + + timeout (``float``): + Timeout in seconds. + Raises: :class:`Error ` """ if not self.is_started: raise ConnectionError("Client has not been started") - r = self.session.send(data) + r = self.session.send(data, retries, timeout) self.fetch_peers(getattr(r, "users", [])) self.fetch_peers(getattr(r, "chats", [])) diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index 578cf168..d8e30283 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -23,6 +23,7 @@ from threading import Lock from ..style import Markdown, HTML from ...api.core import Object from ...session.internals import MsgId +from ...session import Session class BaseClient: @@ -77,7 +78,7 @@ class BaseClient: self.disconnect_handler = None - def send(self, data: Object): + def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): pass def resolve_peer(self, peer_id: int or str): diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 7e90cfff..d8c8651d 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -391,7 +391,7 @@ class Session: log.debug("RecvThread stopped") - def _send(self, data: Object, wait_response: bool = True): + def _send(self, data: Object, wait_response: bool = True, timeout: float = WAIT_TIMEOUT): message = self.msg_factory(data) msg_id = message.msg_id @@ -407,7 +407,7 @@ class Session: raise e if wait_response: - self.results[msg_id].event.wait(self.WAIT_TIMEOUT) + self.results[msg_id].event.wait(timeout) result = self.results.pop(msg_id).value if result is None: @@ -422,11 +422,11 @@ class Session: else: return result - def send(self, data: Object, retries: int = MAX_RETRIES): + def send(self, data: Object, retries: int = MAX_RETRIES, timeout: float = WAIT_TIMEOUT): self.is_connected.wait(self.WAIT_TIMEOUT) try: - return self._send(data) + return self._send(data, timeout=timeout) except (OSError, TimeoutError, InternalServerError) as e: if retries == 0: raise e from None @@ -437,4 +437,4 @@ class Session: datetime.now(), type(data))) time.sleep(0.5) - return self.send(data, retries - 1) + return self.send(data, retries - 1, timeout) From 166b3646ac5d862968e53ffb2608d1e01f8210f7 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:48:33 +0200 Subject: [PATCH 08/14] Make request_callback_answer try only once with a timeout of 10 seconds --- pyrogram/client/methods/bots/request_callback_answer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/methods/bots/request_callback_answer.py b/pyrogram/client/methods/bots/request_callback_answer.py index 5bc31efd..e5871dd8 100644 --- a/pyrogram/client/methods/bots/request_callback_answer.py +++ b/pyrogram/client/methods/bots/request_callback_answer.py @@ -47,5 +47,7 @@ class RequestCallbackAnswer(BaseClient): peer=self.resolve_peer(chat_id), msg_id=message_id, data=callback_data.encode() - ) + ), + retries=0, + timeout=10 ) From 56f616c753228653214c1a848348226182f66206 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:50:30 +0200 Subject: [PATCH 09/14] Mention timeout and retries in docs for request_callback_answer --- .../client/methods/bots/request_callback_answer.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/bots/request_callback_answer.py b/pyrogram/client/methods/bots/request_callback_answer.py index e5871dd8..52dab58c 100644 --- a/pyrogram/client/methods/bots/request_callback_answer.py +++ b/pyrogram/client/methods/bots/request_callback_answer.py @@ -25,9 +25,8 @@ class RequestCallbackAnswer(BaseClient): chat_id: int or str, message_id: int, callback_data: str): - """Use this method to request a callback answer from bots. This is the equivalent of clicking an inline button - containing callback data. The answer contains info useful for clients to display a notification at the top of - the chat screen or as an alert. + """Use this method to request a callback answer from bots. This is the equivalent of clicking an + inline button containing callback data. Args: chat_id (``int`` | ``str``): @@ -41,6 +40,14 @@ class RequestCallbackAnswer(BaseClient): callback_data (``str``): Callback data associated with the inline button you want to get the answer from. + + Returns: + The answer containing info useful for clients to display a notification at the top of the chat screen + or as an alert. + + Raises: + :class:`Error ` + ``TimeoutError``: If the bot fails to answer within 10 seconds """ return self.send( functions.messages.GetBotCallbackAnswer( From f4c0793a0b67713f74571d1971b26e1790f4b32f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 18:26:11 +0200 Subject: [PATCH 10/14] Make session leaner by removing some redundant parameters Related to #86 --- pyrogram/client/client.py | 38 ++++++++--------------- pyrogram/client/ext/base_client.py | 19 +++++++++++- pyrogram/session/session.py | 48 ++++++++++-------------------- 3 files changed, 47 insertions(+), 58 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 1897e8d3..dde583dc 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -187,12 +187,9 @@ class Client(Methods, BaseClient): self.load_session() self.session = Session( + self, self.dc_id, - self.test_mode, - self._proxy, - self.auth_key, - self.api_id, - client=self + self.auth_key ) self.session.start() @@ -372,12 +369,9 @@ class Client(Methods, BaseClient): self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.session = Session( + self, self.dc_id, - self.test_mode, - self._proxy, - self.auth_key, - self.api_id, - client=self + self.auth_key ) self.session.start() @@ -420,12 +414,9 @@ class Client(Methods, BaseClient): self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.session = Session( + self, self.dc_id, - self.test_mode, - self._proxy, - self.auth_key, - self.api_id, - client=self + self.auth_key ) self.session.start() @@ -1033,7 +1024,7 @@ class Client(Methods, BaseClient): file_id = file_id or self.rnd_id() md5_sum = md5() if not is_big and not is_missing_part else None - session = Session(self.dc_id, self.test_mode, self._proxy, self.auth_key, self.api_id) + session = Session(self, self.dc_id, self.auth_key, is_media=True) session.start() try: @@ -1117,11 +1108,10 @@ class Client(Methods, BaseClient): ) session = Session( + self, dc_id, - self.test_mode, - self._proxy, Auth(dc_id, self.test_mode, self._proxy).create(), - self.api_id + is_media=True ) session.start() @@ -1136,11 +1126,10 @@ class Client(Methods, BaseClient): ) else: session = Session( + self, dc_id, - self.test_mode, - self._proxy, self.auth_key, - self.api_id + is_media=True ) session.start() @@ -1206,11 +1195,10 @@ class Client(Methods, BaseClient): if cdn_session is None: cdn_session = Session( + self, r.dc_id, - self.test_mode, - self._proxy, Auth(r.dc_id, self.test_mode, self._proxy).create(), - self.api_id, + is_media=True, is_cdn=True ) diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index d8e30283..cd54570d 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -16,17 +16,34 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import platform import re from queue import Queue from threading import Lock +from pyrogram import __version__ from ..style import Markdown, HTML from ...api.core import Object -from ...session.internals import MsgId from ...session import Session +from ...session.internals import MsgId class BaseClient: + APP_VERSION = "Pyrogram \U0001f525 {}".format(__version__) + + DEVICE_MODEL = "{} {}".format( + platform.python_implementation(), + platform.python_version() + ) + + SYSTEM_VERSION = "{} {}".format( + platform.system(), + platform.release() + ) + + SYSTEM_LANG_CODE = "en" + LANG_CODE = "en" + INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/joinchat/)([\w-]+)$") BOT_TOKEN_RE = re.compile(r"^\d+:[\w-]+$") DIALOGS_AT_ONCE = 100 diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index d8c8651d..b643650b 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -17,7 +17,6 @@ # along with Pyrogram. If not, see . import logging -import platform import threading import time from datetime import timedelta, datetime @@ -47,19 +46,6 @@ class Result: class Session: - VERSION = __version__ - APP_VERSION = "Pyrogram \U0001f525 {}".format(VERSION) - - DEVICE_MODEL = "{} {}".format( - platform.python_implementation(), - platform.python_version() - ) - - SYSTEM_VERSION = "{} {}".format( - platform.system(), - platform.release() - ) - INITIAL_SALT = 0x616e67656c696361 NET_WORKERS = 1 WAIT_TIMEOUT = 15 @@ -84,28 +70,24 @@ class Session: } def __init__(self, + client: pyrogram, dc_id: int, - test_mode: bool, - proxy: dict, auth_key: bytes, - api_id: int, - is_cdn: bool = False, - client: pyrogram = None): + is_media: bool = False, + is_cdn: bool = False): if not Session.notice_displayed: print("Pyrogram v{}, {}".format(__version__, __copyright__)) print("Licensed under the terms of the " + __license__, end="\n\n") Session.notice_displayed = True - self.dc_id = dc_id - self.test_mode = test_mode - self.proxy = proxy - self.api_id = api_id - self.is_cdn = is_cdn self.client = client + self.dc_id = dc_id + self.auth_key = auth_key + self.is_media = is_media + self.is_cdn = is_cdn self.connection = None - self.auth_key = auth_key self.auth_key_id = sha1(auth_key).digest()[-8:] self.session_id = Long(MsgId()) @@ -130,7 +112,7 @@ class Session: def start(self): while True: - self.connection = Connection(DataCenter(self.dc_id, self.test_mode), self.proxy) + self.connection = Connection(DataCenter(self.dc_id, self.client.test_mode), self.client.proxy) try: self.connection.connect() @@ -159,12 +141,14 @@ class Session: functions.InvokeWithLayer( layer, functions.InitConnection( - self.api_id, - self.DEVICE_MODEL, - self.SYSTEM_VERSION, - self.APP_VERSION, - "en", "", "en", - functions.help.GetConfig(), + api_id=self.client.api_id, + device_model=self.client.DEVICE_MODEL, + system_version=self.client.SYSTEM_VERSION, + app_version=self.client.APP_VERSION, + system_lang_code=self.client.SYSTEM_LANG_CODE, + lang_code=self.client.LANG_CODE, + lang_pack="", + query=functions.help.GetConfig(), ) ) ) From 55ef4abb2737a3e829c7693d927cd6bcfb5075b5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 18:47:49 +0200 Subject: [PATCH 11/14] Allow user-defined session properties. Related to #86 This commit adds five 5 extra parameters to the Client class: app_version, device_model, system_version, system_lang_code, lang_code --- pyrogram/client/client.py | 72 +++++++++++++++++++++++++++++++++++++ pyrogram/session/session.py | 10 +++--- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index dde583dc..e2fe648e 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -129,6 +129,11 @@ class Client(Methods, BaseClient): session_name: str, api_id: int or str = None, api_hash: str = None, + app_version: str = None, + device_model: str = None, + system_version: str = None, + system_lang_code: str = None, + lang_code: str = None, proxy: dict = None, test_mode: bool = False, phone_number: str = None, @@ -145,6 +150,11 @@ class Client(Methods, BaseClient): self.session_name = session_name self.api_id = int(api_id) if api_id else None self.api_hash = api_hash + self.app_version = app_version + self.device_model = device_model + self.system_version = system_version + self.system_lang_code = system_lang_code + self.lang_code = lang_code # TODO: Make code consistent, use underscore for private/protected fields self._proxy = proxy self.test_mode = test_mode @@ -854,6 +864,68 @@ class Client(Methods, BaseClient): "More info: https://docs.pyrogram.ml/start/ProjectSetup#configuration" ) + # TODO: Woah! Some redundant code down here + + if self.app_version: + pass + else: + self.app_version = Client.APP_VERSION + + if parser.has_section("pyrogram"): + self.app_version = parser.get( + "pyrogram", + "app_version", + fallback=Client.APP_VERSION + ) + + if self.device_model: + pass + else: + self.device_model = Client.DEVICE_MODEL + + if parser.has_section("pyrogram"): + self.device_model = parser.get( + "pyrogram", + "device_model", + fallback=Client.DEVICE_MODEL + ) + + if self.system_version: + pass + else: + self.system_version = Client.SYSTEM_VERSION + + if parser.has_section("pyrogram"): + self.system_version = parser.get( + "pyrogram", + "system_version", + fallback=Client.SYSTEM_VERSION + ) + + if self.system_lang_code: + pass + else: + self.system_lang_code = Client.SYSTEM_LANG_CODE + + if parser.has_section("pyrogram"): + self.system_lang_code = parser.get( + "pyrogram", + "system_lang_code", + fallback=Client.SYSTEM_LANG_CODE + ) + + if self.lang_code: + pass + else: + self.lang_code = Client.LANG_CODE + + if parser.has_section("pyrogram"): + self.lang_code = parser.get( + "pyrogram", + "lang_code", + fallback=Client.LANG_CODE + ) + if self._proxy: self._proxy["enabled"] = True else: diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index b643650b..77188156 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -142,11 +142,11 @@ class Session: layer, functions.InitConnection( api_id=self.client.api_id, - device_model=self.client.DEVICE_MODEL, - system_version=self.client.SYSTEM_VERSION, - app_version=self.client.APP_VERSION, - system_lang_code=self.client.SYSTEM_LANG_CODE, - lang_code=self.client.LANG_CODE, + app_version=self.client.app_version, + device_model=self.client.device_model, + system_version=self.client.system_version, + system_lang_code=self.client.system_lang_code, + lang_code=self.client.lang_code, lang_pack="", query=functions.help.GetConfig(), ) From 9a012077cc11edaffa79680ae6cc5ac5f4db5f3a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 18:54:47 +0200 Subject: [PATCH 12/14] Remove some redundant code. Related to #86 --- pyrogram/client/client.py | 59 ++++++++------------------------------- 1 file changed, 11 insertions(+), 48 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index e2fe648e..be6d5385 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -864,55 +864,18 @@ class Client(Methods, BaseClient): "More info: https://docs.pyrogram.ml/start/ProjectSetup#configuration" ) - # TODO: Woah! Some redundant code down here + for option in {"app_version", "device_model", "system_version", "system_lang_code", "lang_code"}: + if getattr(self, option): + pass + else: + setattr(self, option, Client.APP_VERSION) - if self.app_version: - pass - else: - self.app_version = Client.APP_VERSION - - if parser.has_section("pyrogram"): - self.app_version = parser.get( - "pyrogram", - "app_version", - fallback=Client.APP_VERSION - ) - - if self.device_model: - pass - else: - self.device_model = Client.DEVICE_MODEL - - if parser.has_section("pyrogram"): - self.device_model = parser.get( - "pyrogram", - "device_model", - fallback=Client.DEVICE_MODEL - ) - - if self.system_version: - pass - else: - self.system_version = Client.SYSTEM_VERSION - - if parser.has_section("pyrogram"): - self.system_version = parser.get( - "pyrogram", - "system_version", - fallback=Client.SYSTEM_VERSION - ) - - if self.system_lang_code: - pass - else: - self.system_lang_code = Client.SYSTEM_LANG_CODE - - if parser.has_section("pyrogram"): - self.system_lang_code = parser.get( - "pyrogram", - "system_lang_code", - fallback=Client.SYSTEM_LANG_CODE - ) + if parser.has_section("pyrogram"): + setattr(self, option, parser.get( + "pyrogram", + option, + fallback=getattr(Client, option.upper()) + )) if self.lang_code: pass From 9c420bb73b4ac297377e8a94dff125e91cbb11cf Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 19:08:52 +0200 Subject: [PATCH 13/14] Document the new parameters. Closes #86 --- pyrogram/client/client.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index be6d5385..519823a0 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -77,6 +77,26 @@ class Client(Methods, BaseClient): The *api_hash* part of your Telegram API Key, as string. E.g.: "0123456789abcdef0123456789abcdef" This is an alternative way to pass it if you don't want to use the *config.ini* file. + app_version (``str``, *optional*): + Application version. Defaults to "Pyrogram \U0001f525 vX.Y.Z" + This is an alternative way to set it if you don't want to use the *config.ini* file. + + device_model (``str``, *optional*): + Device model. Defaults to *platform.python_implementation() + " " + platform.python_version()* + This is an alternative way to set it if you don't want to use the *config.ini* file. + + system_version (``str``, *optional*): + Operating System version. Defaults to *platform.system() + " " + platform.release()* + This is an alternative way to set it if you don't want to use the *config.ini* file. + + system_lang_code (``str``, *optional*): + Code of the language used on the system, in ISO 639-1 standard. Defaults to "en". + This is an alternative way to set it if you don't want to use the *config.ini* file. + + lang_code (``str``, *optional*): + Code of the language used on the client, in ISO 639-1 standard. Defaults to "en". + This is an alternative way to set it if you don't want to use the *config.ini* file. + proxy (``dict``, *optional*): Your SOCKS5 Proxy settings as dict, e.g.: *dict(hostname="11.22.33.44", port=1080, username="user", password="pass")*. From fa7b7d0ce353f6f66efd4da0ebd3b7bfa08714ee Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 19:09:22 +0200 Subject: [PATCH 14/14] Cleanup Client class --- pyrogram/client/client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 519823a0..366ab4b2 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -51,9 +51,6 @@ from .dispatcher import Dispatcher from .ext import utils, Syncer, BaseClient from .methods import Methods -# Custom format for nice looking log lines -LOG_FORMAT = "[%(asctime)s.%(msecs)03d] %(filename)s:%(lineno)s %(levelname)s: %(message)s" - log = logging.getLogger(__name__) @@ -316,6 +313,7 @@ class Client(Methods, BaseClient): Iterable containing signals the signal handler will listen to. Defaults to (SIGINT, SIGTERM, SIGABRT). """ + def signal_handler(*args): self.is_idle = False