From 3ec6b22d85da8ad905fe7de7e913f26dfe5b51bc Mon Sep 17 00:00:00 2001 From: Hearot Date: Wed, 8 Jul 2020 15:43:17 +0200 Subject: [PATCH 1/4] Add references to Pyrubrum (#429) --- docs/source/powered-by.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/source/powered-by.rst b/docs/source/powered-by.rst index a696cebc..77e593e7 100644 --- a/docs/source/powered-by.rst +++ b/docs/source/powered-by.rst @@ -71,5 +71,15 @@ Projects Showcase ----- +`Pyrubrum `_ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +| **An intuitive framework for creating Telegram bots** +| --- by `Hearot `_ + +- Source Code: https://github.com/hearot/pyrubrum + +----- + .. _Feature Request: https://github.com/pyrogram/pyrogram/issues/new?labels=enhancement&template=feature_request.md From 531069b1e2a202f5e922705c6515fea7595edec9 Mon Sep 17 00:00:00 2001 From: Alisson Lauffer Date: Sun, 12 Jul 2020 01:43:30 -0300 Subject: [PATCH 2/4] Some fixes and speed improvments (#439) * Use raw string for re pattern * Trim trailing whitespaces from docstrings and code * Use isinstance() instead of type() for typechecking * Remove unused imports --- compiler/api/compiler.py | 2 +- docs/README.md | 2 +- pyrogram/client/client.py | 2 +- pyrogram/client/filters/filters.py | 14 +++++++------- .../client/methods/chats/delete_user_history.py | 2 +- .../client/methods/chats/get_chat_members.py | 2 -- pyrogram/client/methods/chats/get_dialogs.py | 2 -- pyrogram/client/methods/chats/set_chat_photo.py | 2 +- .../client/methods/chats/update_chat_username.py | 2 +- pyrogram/client/methods/messages/get_history.py | 2 -- pyrogram/client/methods/messages/get_messages.py | 2 -- .../client/methods/users/get_common_chats.py | 4 ++-- pyrogram/client/methods/users/update_profile.py | 16 ++++++++-------- pyrogram/client/methods/users/update_username.py | 2 +- .../types/inline_mode/chosen_inline_result.py | 2 -- pyrogram/client/types/messages_and_media/dice.py | 5 ----- .../client/types/messages_and_media/message.py | 4 ++-- pyrogram/session/internals/msg_factory.py | 4 ++-- 18 files changed, 28 insertions(+), 43 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index f6cb5742..3b0876f9 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -26,7 +26,7 @@ NOTICE_PATH = "NOTICE" SECTION_RE = re.compile(r"---(\w+)---") LAYER_RE = re.compile(r"//\sLAYER\s(\d+)") COMBINATOR_RE = re.compile(r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);(?: // Docs: (.+))?$", re.MULTILINE) -ARGS_RE = re.compile("[^{](\w+):([\w?!.<>#]+)") +ARGS_RE = re.compile(r"[^{](\w+):([\w?!.<>#]+)") FLAGS_RE = re.compile(r"flags\.(\d+)\?") FLAGS_RE_2 = re.compile(r"flags\.(\d+)\?([\w<>.]+)") FLAGS_RE_3 = re.compile(r"flags:#") diff --git a/docs/README.md b/docs/README.md index 1c929447..680e2cf4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ - Install requirements. - Install `pandoc` and `latexmk`. -- HTML: `make html` +- HTML: `make html` - PDF: `make latexpdf` TODO: Explain better \ No newline at end of file diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index c46f119a..8d53e354 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1649,7 +1649,7 @@ class Client(Methods, BaseClient): try: return self.storage.get_peer_by_id(peer_id) except KeyError: - if type(peer_id) is str: + if isinstance(peer_id, str): if peer_id in ("self", "me"): return types.InputPeerSelf() diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 19218813..e872d724 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -214,7 +214,7 @@ class Filters: from_scheduled = create(lambda _, m: bool(m.from_scheduled), "FromScheduledFilter") """Filter new automatically sent messages that were previously scheduled.""" - + # Messages from linked channels are forwarded automatically by Telegram and have no sender (from_user is None). linked_channel = create(lambda _, m: bool(m.forward_from_chat and not m.from_user), "LinkedChannelFilter") """Filter messages that are automatically forwarded from the linked channel to the group chat.""" @@ -277,11 +277,11 @@ class Filters: return False - commands = commands if type(commands) is list else [commands] + commands = commands if isinstance(commands, list) else [commands] commands = {c if case_sensitive else c.lower() for c in commands} prefixes = [] if prefixes is None else prefixes - prefixes = prefixes if type(prefixes) is list else [prefixes] + prefixes = prefixes if isinstance(prefixes, list) else [prefixes] prefixes = set(prefixes) if prefixes else {""} return create( @@ -345,11 +345,11 @@ class Filters: """ def __init__(self, users: int or str or list = None): - users = [] if users is None else users if type(users) is list else [users] + users = [] if users is None else users if isinstance(users, list) else [users] super().__init__( "me" if u in ["me", "self"] - else u.lower().strip("@") if type(u) is str + else u.lower().strip("@") if isinstance(u, str) else u for u in users ) @@ -376,11 +376,11 @@ class Filters: """ def __init__(self, chats: int or str or list = None): - chats = [] if chats is None else chats if type(chats) is list else [chats] + chats = [] if chats is None else chats if isinstance(chats, list) else [chats] super().__init__( "me" if c in ["me", "self"] - else c.lower().strip("@") if type(c) is str + else c.lower().strip("@") if isinstance(c, str) else c for c in chats ) diff --git a/pyrogram/client/methods/chats/delete_user_history.py b/pyrogram/client/methods/chats/delete_user_history.py index 03d87ca4..a35bf10c 100644 --- a/pyrogram/client/methods/chats/delete_user_history.py +++ b/pyrogram/client/methods/chats/delete_user_history.py @@ -18,7 +18,7 @@ from typing import Union -from pyrogram.api import functions, types +from pyrogram.api import functions from pyrogram.client.ext import BaseClient diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index bf208944..da1954a4 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -17,12 +17,10 @@ # along with Pyrogram. If not, see . import logging -import time from typing import Union, List import pyrogram from pyrogram.api import functions, types -from pyrogram.errors import FloodWait from ...ext import BaseClient log = logging.getLogger(__name__) diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index f5d5f442..de03b046 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -17,12 +17,10 @@ # along with Pyrogram. If not, see . import logging -import time from typing import List import pyrogram from pyrogram.api import functions, types -from pyrogram.errors import FloodWait from ...ext import BaseClient, utils log = logging.getLogger(__name__) diff --git a/pyrogram/client/methods/chats/set_chat_photo.py b/pyrogram/client/methods/chats/set_chat_photo.py index d394322c..e22b6819 100644 --- a/pyrogram/client/methods/chats/set_chat_photo.py +++ b/pyrogram/client/methods/chats/set_chat_photo.py @@ -41,7 +41,7 @@ class SetChatPhoto(BaseClient): photo (``str``): New chat photo. You can pass a :obj:`Photo` file_id or a file path to upload a new photo from your local machine. - + file_ref (``str``, *optional*): A valid file reference obtained by a recently fetched media message. To be used in combination with a file id in case a file reference is needed. diff --git a/pyrogram/client/methods/chats/update_chat_username.py b/pyrogram/client/methods/chats/update_chat_username.py index ff4db61b..251d6832 100644 --- a/pyrogram/client/methods/chats/update_chat_username.py +++ b/pyrogram/client/methods/chats/update_chat_username.py @@ -29,7 +29,7 @@ class UpdateChatUsername(BaseClient): username: Union[str, None] ) -> bool: """Update a channel or a supergroup username. - + To update your own username (for users only, not bots) you can use :meth:`~Client.update_username`. Parameters: diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index 79a1dec6..ea630aee 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -17,13 +17,11 @@ # along with Pyrogram. If not, see . import logging -import time from typing import Union, List import pyrogram from pyrogram.api import functions from pyrogram.client.ext import utils -from pyrogram.errors import FloodWait from ...ext import BaseClient log = logging.getLogger(__name__) diff --git a/pyrogram/client/methods/messages/get_messages.py b/pyrogram/client/methods/messages/get_messages.py index e505b566..caf5bea0 100644 --- a/pyrogram/client/methods/messages/get_messages.py +++ b/pyrogram/client/methods/messages/get_messages.py @@ -17,12 +17,10 @@ # along with Pyrogram. If not, see . import logging -import time from typing import Union, Iterable, List import pyrogram from pyrogram.api import functions, types -from pyrogram.errors import FloodWait from ...ext import BaseClient, utils log = logging.getLogger(__name__) diff --git a/pyrogram/client/methods/users/get_common_chats.py b/pyrogram/client/methods/users/get_common_chats.py index 323c5e87..35d037fb 100644 --- a/pyrogram/client/methods/users/get_common_chats.py +++ b/pyrogram/client/methods/users/get_common_chats.py @@ -35,7 +35,7 @@ class GetCommonChats(BaseClient): Returns: List of :obj:`Chat`: On success, a list of the common chats is returned. - + Raises: ValueError: If the user_id doesn't belong to a user. @@ -58,5 +58,5 @@ class GetCommonChats(BaseClient): ) return pyrogram.List([pyrogram.Chat._parse_chat(self, x) for x in r.chats]) - + raise ValueError('The user_id "{}" doesn\'t belong to a user'.format(user_id)) diff --git a/pyrogram/client/methods/users/update_profile.py b/pyrogram/client/methods/users/update_profile.py index 91a7950c..da7d62f4 100644 --- a/pyrogram/client/methods/users/update_profile.py +++ b/pyrogram/client/methods/users/update_profile.py @@ -28,13 +28,13 @@ class UpdateProfile(BaseClient): bio: str = None ) -> bool: """Update your profile details such as first name, last name and bio. - + You can omit the parameters you don't want to change. - + Parameters: first_name (``str``, *optional*): The new first name. - + last_name (``str``, *optional*): The new last name. Pass "" (empty string) to remove it. @@ -42,19 +42,19 @@ class UpdateProfile(BaseClient): bio (``str``, *optional*): The new bio, also known as "about". Max 70 characters. Pass "" (empty string) to remove it. - + Returns: ``bool``: True on success. - + Example: .. code-block:: python - + # Update your first name only app.update_profile(first_name="Pyrogram") - + # Update first name and bio app.update_profile(first_name="Pyrogram", bio="https://docs.pyrogram.org/") - + # Remove the last name app.update_profile(last_name="") """ diff --git a/pyrogram/client/methods/users/update_username.py b/pyrogram/client/methods/users/update_username.py index 88536842..24a12a8e 100644 --- a/pyrogram/client/methods/users/update_username.py +++ b/pyrogram/client/methods/users/update_username.py @@ -28,7 +28,7 @@ class UpdateUsername(BaseClient): username: Union[str, None] ) -> bool: """Update your own username. - + This method only works for users, not bots. Bot usernames must be changed via Bot Support or by recreating them from scratch using BotFather. To update a channel or supergroup username you can use :meth:`~Client.update_chat_username`. diff --git a/pyrogram/client/types/inline_mode/chosen_inline_result.py b/pyrogram/client/types/inline_mode/chosen_inline_result.py index cdc5afc9..55ef270d 100644 --- a/pyrogram/client/types/inline_mode/chosen_inline_result.py +++ b/pyrogram/client/types/inline_mode/chosen_inline_result.py @@ -33,7 +33,6 @@ from base64 import b64encode from struct import pack -from typing import Union import pyrogram from pyrogram.api import types @@ -41,7 +40,6 @@ from pyrogram.client.types.object import Object from pyrogram.client.types.update import Update from pyrogram.client.types.user_and_chats import User from pyrogram.client.types.messages_and_media import Location -from pyrogram.client.ext import utils class ChosenInlineResult(Object, Update): diff --git a/pyrogram/client/types/messages_and_media/dice.py b/pyrogram/client/types/messages_and_media/dice.py index 7552f083..e76cad1e 100644 --- a/pyrogram/client/types/messages_and_media/dice.py +++ b/pyrogram/client/types/messages_and_media/dice.py @@ -16,14 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from struct import pack -from typing import List - import pyrogram from pyrogram.api import types -from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode_file_id, encode_file_ref class Dice(Object): diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 215f86d0..47606715 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -2964,7 +2964,7 @@ class Message(Object, Update): chat_id=message.chat.id, message_id=message_id, ) - + Example: .. code-block:: python @@ -3074,7 +3074,7 @@ class Message(Object, Update): Parameters: option (``int``): Index of the poll option you want to vote for (0 to 9). - + Returns: :obj:`Poll`: On success, the poll with the chosen option is returned. diff --git a/pyrogram/session/internals/msg_factory.py b/pyrogram/session/internals/msg_factory.py index e5144fe4..3f4c3026 100644 --- a/pyrogram/session/internals/msg_factory.py +++ b/pyrogram/session/internals/msg_factory.py @@ -23,7 +23,7 @@ from pyrogram.api.core import Message, MsgContainer, TLObject from .msg_id import MsgId from .seq_no import SeqNo -not_content_related = [Ping, HttpWait, MsgsAck, MsgContainer] +not_content_related = (Ping, HttpWait, MsgsAck, MsgContainer) class MsgFactory: @@ -34,6 +34,6 @@ class MsgFactory: return Message( body, MsgId(), - self.seq_no(type(body) not in not_content_related), + self.seq_no(not isinstance(body, not_content_related)), len(body) ) From 5a0a0b67bfa5d4111d12bef19cb26feec93269bc Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 15 Jul 2020 10:58:15 +0200 Subject: [PATCH 3/4] Use f-string in the main example --- docs/source/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 6a8ae4f5..fd227079 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -106,7 +106,7 @@ Welcome to Pyrogram @app.on_message(Filters.private) def hello(client, message): - message.reply_text("Hello {}".format(message.from_user.first_name)) + message.reply_text(f"Hello {message.from_user.first_name}") app.run() From 2504286365c602f893d9ac5d5137e6a145d5234a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 17 Jul 2020 13:41:24 +0200 Subject: [PATCH 4/4] Do not reload plugins from disk Reloading from disk causes modules to be re-evaluated, and this is often not desirable. This will break the ability to hot reload plugins --- pyrogram/client/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 8d53e354..af67cc1a 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -27,7 +27,7 @@ import threading import time from configparser import ConfigParser from hashlib import sha256, md5 -from importlib import import_module, reload +from importlib import import_module from pathlib import Path from signal import signal, SIGINT, SIGTERM, SIGABRT from threading import Thread @@ -1523,7 +1523,7 @@ class Client(Methods, BaseClient): if not include: for path in sorted(Path(root.replace(".", "/")).rglob("*.py")): module_path = '.'.join(path.parent.parts + (path.stem,)) - module = reload(import_module(module_path)) + module = import_module(module_path) for name in vars(module).keys(): # noinspection PyBroadException @@ -1545,7 +1545,7 @@ class Client(Methods, BaseClient): warn_non_existent_functions = True try: - module = reload(import_module(module_path)) + module = import_module(module_path) except ImportError: log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format( self.session_name, module_path))