From 9023d999b8a92bf50829d8f1cf82e4ce37348af8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 29 Sep 2018 11:38:58 +0200 Subject: [PATCH 1/9] Fix unsorted Telegram API entries --- compiler/docs/compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 73b5a578..57f4827f 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -88,7 +88,7 @@ def generate(source_path, base): inner_path = base + "/" + k + "/index" + ".rst" module = "pyrogram.api.{}.{}".format(base, k) else: - for i in list(all_entities)[::-1]: + for i in sorted(list(all_entities), reverse=True): if i != base: entities.insert(0, "{0}/index".format(i)) From 3cbffd93f25d533f879cd702836654ca083dd7a3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 09:25:20 +0200 Subject: [PATCH 2/9] Revert "Log unknown constructors" This reverts commit 0b6b598 This fixes #128 --- pyrogram/api/core/object.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/pyrogram/api/core/object.py b/pyrogram/api/core/object.py index 3cf12329..a1e20726 100644 --- a/pyrogram/api/core/object.py +++ b/pyrogram/api/core/object.py @@ -16,7 +16,6 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -import logging from collections import OrderedDict from datetime import datetime from io import BytesIO @@ -24,23 +23,13 @@ from json import JSONEncoder, dumps from ..all import objects -log = logging.getLogger(__name__) - class Object: all = {} @staticmethod def read(b: BytesIO, *args): - constructor_id = int.from_bytes(b.read(4), "little") - - try: - return Object.all[constructor_id].read(b, *args) - except KeyError: - log.error("Unknown constructor found: {}. Full data: {}".format( - hex(constructor_id), - b.getvalue().hex()) - ) + return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args) def write(self, *args) -> bytes: pass From f608899c25c6f50e587e202f4bdd63de41a46e4e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 09:38:25 +0200 Subject: [PATCH 3/9] Add CHANNEL_PRIVATE error #129 --- 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 ed332838..3ef57b83 100644 --- a/compiler/error/source/400_BAD_REQUEST.tsv +++ b/compiler/error/source/400_BAD_REQUEST.tsv @@ -66,4 +66,5 @@ MEDIA_CAPTION_TOO_LONG The media caption is longer than 200 characters USER_NOT_MUTUAL_CONTACT The user is not a mutual contact USER_CHANNELS_TOO_MUCH The user is already in too many channels or supergroups API_ID_PUBLISHED_FLOOD You are using an API key that is limited on the server side -USER_NOT_PARTICIPANT The user is not a member of this chat \ No newline at end of file +USER_NOT_PARTICIPANT The user is not a member of this chat +CHANNEL_PRIVATE The channel/supergroup is not accessible \ No newline at end of file From 12c61fb4312d64017d95b75bbd3d30458b9cbb65 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 09:40:34 +0200 Subject: [PATCH 4/9] Fix unwanted CHANNEL_PRIVATE errors. Fixes #129 --- pyrogram/client/client.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index ade4fc50..8e37cfae 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -43,7 +43,7 @@ from pyrogram.api.errors import ( PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty, PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded, PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned, - VolumeLocNotFound, UserMigrate, FileIdInvalid) + VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate) from pyrogram.client.handlers import DisconnectHandler from pyrogram.crypto import AES from pyrogram.session import Auth, Session @@ -803,19 +803,22 @@ class Client(Methods, BaseClient): message = update.message if not isinstance(message, types.MessageEmpty): - diff = self.send( - functions.updates.GetChannelDifference( - channel=self.resolve_peer(int("-100" + str(channel_id))), - filter=types.ChannelMessagesFilter( - ranges=[types.MessageRange( - min_id=update.message.id, - max_id=update.message.id - )] - ), - pts=pts - pts_count, - limit=pts + try: + diff = self.send( + functions.updates.GetChannelDifference( + channel=self.resolve_peer(int("-100" + str(channel_id))), + filter=types.ChannelMessagesFilter( + ranges=[types.MessageRange( + min_id=update.message.id, + max_id=update.message.id + )] + ), + pts=pts - pts_count, + limit=pts + ) ) - ) + except ChannelPrivate: + continue if not isinstance(diff, types.updates.ChannelDifferenceEmpty): updates.users += diff.users From b79bd1ea8346e09225322c60db3ffe5fd25d94b0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 09:45:32 +0200 Subject: [PATCH 5/9] Don't swallow left_chat_member updates Even though they are only relevant for supergroups with <50 members --- pyrogram/client/client.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 8e37cfae..ee40d2bc 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -818,11 +818,11 @@ class Client(Methods, BaseClient): ) ) except ChannelPrivate: - continue - - if not isinstance(diff, types.updates.ChannelDifferenceEmpty): - updates.users += diff.users - updates.chats += diff.chats + pass + else: + if not isinstance(diff, types.updates.ChannelDifferenceEmpty): + updates.users += diff.users + updates.chats += diff.chats if channel_id and pts: if channel_id not in self.channels_pts: From 0162cf48c10084ffc9e5f0c891fe1418493665c0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 09:55:09 +0200 Subject: [PATCH 6/9] Don't break groups in case one handler raises an unhandled exception The error is logged instead. Fixes #126 --- pyrogram/client/dispatcher/dispatcher.py | 69 ++++++++++++------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index ffff16bd..ea10a201 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -90,43 +90,46 @@ class Dispatcher: def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool = False): for group in self.groups.values(): - for handler in group: - if is_raw: - if not isinstance(handler, RawUpdateHandler): - continue - - args = (self.client, update, users, chats) - else: - message = (update.message - or update.channel_post - or update.edited_message - or update.edited_channel_post) - - deleted_messages = (update.deleted_channel_posts - or update.deleted_messages) - - callback_query = update.callback_query - - if message and isinstance(handler, MessageHandler): - if not handler.check(message): + try: + for handler in group: + if is_raw: + if not isinstance(handler, RawUpdateHandler): continue - args = (self.client, message) - elif deleted_messages and isinstance(handler, DeletedMessagesHandler): - if not handler.check(deleted_messages): - continue - - args = (self.client, deleted_messages) - elif callback_query and isinstance(handler, CallbackQueryHandler): - if not handler.check(callback_query): - continue - - args = (self.client, callback_query) + args = (self.client, update, users, chats) else: - continue + message = (update.message + or update.channel_post + or update.edited_message + or update.edited_channel_post) - handler.callback(*args) - break + deleted_messages = (update.deleted_channel_posts + or update.deleted_messages) + + callback_query = update.callback_query + + if message and isinstance(handler, MessageHandler): + if not handler.check(message): + continue + + args = (self.client, message) + elif deleted_messages and isinstance(handler, DeletedMessagesHandler): + if not handler.check(deleted_messages): + continue + + args = (self.client, deleted_messages) + elif callback_query and isinstance(handler, CallbackQueryHandler): + if not handler.check(callback_query): + continue + + args = (self.client, callback_query) + else: + continue + + handler.callback(*args) + break + except Exception as e: + log.error(e, exc_info=True) def update_worker(self): name = threading.current_thread().name From 1bf8b01151e63c194a3b5836676ddfc58033b9c6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 10:47:10 +0200 Subject: [PATCH 7/9] Add RIGHT_FORBIDDEN error --- compiler/error/source/403_FORBIDDEN.tsv | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/error/source/403_FORBIDDEN.tsv b/compiler/error/source/403_FORBIDDEN.tsv index 7bacbe7d..b8348d2e 100644 --- a/compiler/error/source/403_FORBIDDEN.tsv +++ b/compiler/error/source/403_FORBIDDEN.tsv @@ -1,2 +1,3 @@ id message -CHAT_WRITE_FORBIDDEN You don't have rights to send messages in this chat \ No newline at end of file +CHAT_WRITE_FORBIDDEN You don't have rights to send messages in this chat +RIGHT_FORBIDDEN One or more admin rights can't be applied to this kind of chat (channel/supergroup) \ No newline at end of file From 0a2892691c81d14f91618d2ec6a8b4e024984390 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 10:48:49 +0200 Subject: [PATCH 8/9] Add CHAT_ADMIN_INVITE_REQUIRED error --- compiler/error/source/403_FORBIDDEN.tsv | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/error/source/403_FORBIDDEN.tsv b/compiler/error/source/403_FORBIDDEN.tsv index b8348d2e..a2d06832 100644 --- a/compiler/error/source/403_FORBIDDEN.tsv +++ b/compiler/error/source/403_FORBIDDEN.tsv @@ -1,3 +1,4 @@ id message CHAT_WRITE_FORBIDDEN You don't have rights to send messages in this chat -RIGHT_FORBIDDEN One or more admin rights can't be applied to this kind of chat (channel/supergroup) \ No newline at end of file +RIGHT_FORBIDDEN One or more admin rights can't be applied to this kind of chat (channel/supergroup) +CHAT_ADMIN_INVITE_REQUIRED You don't have rights to invite other users \ No newline at end of file From 89a167b51fcd1d7825dedf160cf75217e59efff5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 1 Oct 2018 10:52:09 +0200 Subject: [PATCH 9/9] Update default rights to the least common between supergroups & channels Fixes #120 --- pyrogram/client/methods/chats/promote_chat_member.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/chats/promote_chat_member.py b/pyrogram/client/methods/chats/promote_chat_member.py index 134d1ba3..adff4605 100644 --- a/pyrogram/client/methods/chats/promote_chat_member.py +++ b/pyrogram/client/methods/chats/promote_chat_member.py @@ -25,12 +25,12 @@ class PromoteChatMember(BaseClient): chat_id: int or str, user_id: int or str, can_change_info: bool = True, - can_post_messages: bool = True, - can_edit_messages: bool = True, + can_post_messages: bool = False, + can_edit_messages: bool = False, can_delete_messages: bool = True, can_invite_users: bool = True, can_restrict_members: bool = True, - can_pin_messages: bool = True, + can_pin_messages: bool = False, can_promote_members: bool = False): """Use this method to promote or demote a user in a supergroup or a channel. You must be an administrator in the chat for this to work and must have the appropriate admin rights.