From 37d063e569c6f451e9f7be45c65ff4bec9a6f30b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 12:35:09 +0100 Subject: [PATCH 01/62] Reorganize Message __init__ --- .../types/messages_and_media/message.py | 69 ++++--------------- 1 file changed, 13 insertions(+), 56 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 02d31590..e8e587d1 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -15,7 +15,7 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . - +from pyrogram.api import types from pyrogram.api.core import Object from ..bots import InlineKeyboardMarkup, ReplyKeyboardMarkup @@ -217,62 +217,19 @@ class Message(Object): # TODO: Add game missing field. Also invoice, successful_payment, connected_website ID = 0xb0700003 - def __init__( - self, - message_id: int, - client=None, - date: int = None, - chat=None, - from_user=None, - forward_from=None, - forward_from_chat=None, - forward_from_message_id: int = None, - forward_signature: str = None, - forward_date: int = None, - reply_to_message=None, - mentioned=None, - empty=None, - service=None, - media=None, - edit_date: int = None, - media_group_id: str = None, - author_signature: str = None, - text: str = None, - entities: list = None, - caption_entities: list = None, - audio=None, - document=None, - photo=None, - sticker=None, - animation=None, - video=None, - voice=None, - video_note=None, - caption: str = None, - contact=None, - location=None, - venue=None, - web_page=None, - new_chat_members: list = None, - left_chat_member=None, - new_chat_title: str = None, - new_chat_photo=None, - delete_chat_photo: bool = None, - group_chat_created: bool = None, - supergroup_chat_created: bool = None, - channel_chat_created: bool = None, - migrate_to_chat_id: int = None, - migrate_from_chat_id: int = None, - pinned_message=None, - views: int = None, - via_bot=None, - outgoing: bool = None, - matches: list = None, - command: list = None, - reply_markup=None, - ): + def __init__(self, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, + forward_from_chat=None, forward_from_message_id: int = None, forward_signature: str = None, + forward_date: int = None, reply_to_message=None, mentioned=None, empty=None, service=None, media=None, + edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, + entities: list = None, caption_entities: list = None, audio=None, document=None, photo=None, + sticker=None, animation=None, video=None, voice=None, video_note=None, caption: str = None, + contact=None, location=None, venue=None, web_page=None, new_chat_members: list = None, + left_chat_member=None, new_chat_title: str = None, new_chat_photo=None, delete_chat_photo: bool = None, + group_chat_created: bool = None, supergroup_chat_created: bool = None, + channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, + pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, + command: list = None, reply_markup=None, client=None, raw=None): self.message_id = message_id # int - self._client = client self.date = date # int self.chat = chat # Chat self.from_user = from_user # flags.0?User From 9ac492e6447deab5e19d143992456b3eaada240d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 15:32:50 +0100 Subject: [PATCH 02/62] Remove Message generated comments --- .../types/messages_and_media/message.py | 88 ++++++++++--------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index e8e587d1..36c35837 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -15,9 +15,14 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . + from pyrogram.api import types from pyrogram.api.core import Object +from pyrogram.api.errors import MessageIdsEmpty from ..bots import InlineKeyboardMarkup, ReplyKeyboardMarkup +from ..messages_and_media.photo import Photo +from ..user_and_chats.chat import Chat +from ..user_and_chats.user import User class Message(Object): @@ -228,53 +233,54 @@ class Message(Object): group_chat_created: bool = None, supergroup_chat_created: bool = None, channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, - command: list = None, reply_markup=None, client=None, raw=None): - self.message_id = message_id # int - self.date = date # int - self.chat = chat # Chat - self.from_user = from_user # flags.0?User - self.forward_from = forward_from # flags.1?User - self.forward_from_chat = forward_from_chat # flags.2?Chat - self.forward_from_message_id = forward_from_message_id # flags.3?int - self.forward_signature = forward_signature # flags.4?string - self.forward_date = forward_date # flags.5?int - self.reply_to_message = reply_to_message # flags.6?Message + command: list = None, reply_markup=None, + client=None, raw=None): + self.message_id = message_id + self.date = date + self.chat = chat + self.from_user = from_user + self.forward_from = forward_from + self.forward_from_chat = forward_from_chat + self.forward_from_message_id = forward_from_message_id + self.forward_signature = forward_signature + self.forward_date = forward_date + self.reply_to_message = reply_to_message self.mentioned = mentioned self.empty = empty self.service = service self.media = media - self.edit_date = edit_date # flags.7?int - self.media_group_id = media_group_id # flags.8?string - self.author_signature = author_signature # flags.9?string - self.text = text # flags.10?string - self.entities = entities # flags.11?Vector - self.caption_entities = caption_entities # flags.12?Vector - self.audio = audio # flags.13?Audio - self.document = document # flags.14?Document - self.photo = photo # flags.16?Vector - self.sticker = sticker # flags.17?Sticker + self.edit_date = edit_date + self.media_group_id = media_group_id + self.author_signature = author_signature + self.text = text + self.entities = entities + self.caption_entities = caption_entities + self.audio = audio + self.document = document + self.photo = photo + self.sticker = sticker self.animation = animation - self.video = video # flags.18?Video - self.voice = voice # flags.19?Voice - self.video_note = video_note # flags.20?VideoNote - self.caption = caption # flags.21?string - self.contact = contact # flags.22?Contact - self.location = location # flags.23?Location - self.venue = venue # flags.24?Venue + self.video = video + self.voice = voice + self.video_note = video_note + self.caption = caption + self.contact = contact + self.location = location + self.venue = venue self.web_page = web_page - self.new_chat_members = new_chat_members # flags.25?Vector - self.left_chat_member = left_chat_member # flags.26?User - self.new_chat_title = new_chat_title # flags.27?string - self.new_chat_photo = new_chat_photo # flags.28?Vector - self.delete_chat_photo = delete_chat_photo # flags.29?true - self.group_chat_created = group_chat_created # flags.30?true - self.supergroup_chat_created = supergroup_chat_created # flags.31?true - self.channel_chat_created = channel_chat_created # flags.32?true - self.migrate_to_chat_id = migrate_to_chat_id # flags.33?int - self.migrate_from_chat_id = migrate_from_chat_id # flags.34?int - self.pinned_message = pinned_message # flags.35?Message - self.views = views # flags.39?int - self.via_bot = via_bot # flags.40?User + self.new_chat_members = new_chat_members + self.left_chat_member = left_chat_member + self.new_chat_title = new_chat_title + self.new_chat_photo = new_chat_photo + self.delete_chat_photo = delete_chat_photo + self.group_chat_created = group_chat_created + self.supergroup_chat_created = supergroup_chat_created + self.channel_chat_created = channel_chat_created + self.migrate_to_chat_id = migrate_to_chat_id + self.migrate_from_chat_id = migrate_from_chat_id + self.pinned_message = pinned_message + self.views = views + self.via_bot = via_bot self.outgoing = outgoing self.matches = matches self.command = command From b2f3e79ff7e53ac4970671b5dd9dc9f70c66e887 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 17:25:34 +0100 Subject: [PATCH 03/62] Refactor UserStatus --- .../types/user_and_chats/user_status.py | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index cc96df52..1a0ab872 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object @@ -63,17 +64,10 @@ class UserStatus(Object): ID = 0xb0700031 - def __init__( - self, - user_id: int = None, - online: bool = None, - offline: bool = None, - date: int = None, - recently: bool = None, - within_week: bool = None, - within_month: bool = None, - long_time_ago: bool = None - ): + def __init__(self, user_id: int, *, online: bool = None, offline: bool = None, date: int = None, + recently: bool = None, within_week: bool = None, within_month: bool = None, + long_time_ago: bool = None, + client=None, raw=None): self.user_id = user_id self.online = online self.offline = offline @@ -82,3 +76,31 @@ class UserStatus(Object): self.within_week = within_week self.within_month = within_month self.long_time_ago = long_time_ago + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, user: types.User): + if user.bot: + return None + + raw_status = user.status + status = UserStatus(user_id=user.id, client=client, raw=raw_status) + + if isinstance(raw_status, types.UserStatusOnline): + status.online = True + status.date = raw_status.expires + elif isinstance(raw_status, types.UserStatusOffline): + status.offline = True + status.date = raw_status.was_online + elif isinstance(raw_status, types.UserStatusRecently): + status.recently = True + elif isinstance(raw_status, types.UserStatusLastWeek): + status.within_week = True + elif isinstance(raw_status, types.UserStatusLastMonth): + status.within_month = True + else: + status.long_time_ago = True + + return status From 26b15f384d3669abcee84e4cc240692e20418551 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 17:26:24 +0100 Subject: [PATCH 04/62] Update UserStatus docs --- pyrogram/client/types/user_and_chats/user_status.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 1a0ab872..920168b5 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -29,8 +29,8 @@ class UserStatus(Object): "recently", "within_week", "within_month" or "long_time_ago" fields set. Args: - user_id (``int``, *optional*): - User's id. Only available for incoming UserStatus updates. + user_id (``int``): + User's id. online (``bool``, *optional*): True if the user is online in this very moment, None otherwise. From 0d0fbdad272718a3c08c467965e8dd4e0425b46c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 17:28:52 +0100 Subject: [PATCH 05/62] Reformat UserStatus style --- pyrogram/client/types/user_and_chats/user_status.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 920168b5..651d8db6 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -64,11 +64,12 @@ class UserStatus(Object): ID = 0xb0700031 - def __init__(self, user_id: int, *, online: bool = None, offline: bool = None, date: int = None, - recently: bool = None, within_week: bool = None, within_month: bool = None, - long_time_ago: bool = None, + def __init__(self, user_id: int, *, + online: bool = None, offline: bool = None, date: int = None, recently: bool = None, + within_week: bool = None, within_month: bool = None, long_time_ago: bool = None, client=None, raw=None): self.user_id = user_id + self.online = online self.offline = offline self.date = date From bf3609ec0a7b34342e48cbaaa97b4078798126ab Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 17:29:51 +0100 Subject: [PATCH 06/62] Refactor User --- pyrogram/client/types/user_and_chats/user.py | 54 +++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 06045b00..e460522b 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -16,7 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object +from .user_status import UserStatus +from .chat_photo import ChatPhoto class User(Object): @@ -68,23 +71,11 @@ class User(Object): ID = 0xb0700001 - def __init__( - self, - id: int, - is_self: bool, - is_contact: bool, - is_mutual_contact: bool, - is_deleted: bool, - is_bot: bool, - first_name: str, - status=None, - last_name: str = None, - username: str = None, - language_code: str = None, - phone_number: str = None, - photo=None, - restriction_reason: str = None - ): + def __init__(self, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool, + is_bot: bool, first_name: str, *, + last_name: str = None, status=None, username: str = None, language_code: str = None, + phone_number: str = None, photo=None, restriction_reason: str = None, + client=None, raw=None): self.id = id self.is_self = is_self self.is_contact = is_contact @@ -92,10 +83,37 @@ class User(Object): self.is_deleted = is_deleted self.is_bot = is_bot self.first_name = first_name - self.status = status + self.last_name = last_name + self.status = status self.username = username self.language_code = language_code self.phone_number = phone_number self.photo = photo self.restriction_reason = restriction_reason + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, user: types.User) -> "User" or None: + if user is None: + return None + + return User( + id=user.id, + is_self=user.is_self, + is_contact=user.contact, + is_mutual_contact=user.mutual_contact, + is_deleted=user.deleted, + is_bot=user.bot, + first_name=user.first_name, + last_name=user.last_name, + status=UserStatus.parse(client, user), + username=user.username, + language_code=user.lang_code, + phone_number=user.phone, + photo=ChatPhoto.parse(client, user.photo), + restriction_reason=user.restriction_reason, + client=client, raw=user + ) From 5962f8dedc8438f58cbb2caf3c0a627fa1c304cb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 17:30:24 +0100 Subject: [PATCH 07/62] Refactor ChatPhoto --- .../client/types/user_and_chats/chat_photo.py | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index e5877309..485777e5 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -16,7 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from ...ext.utils import encode class ChatPhoto(Object): @@ -32,6 +36,42 @@ class ChatPhoto(Object): ID = 0xb0700015 - def __init__(self, small_file_id: str, big_file_id: str): - self.small_file_id = small_file_id # string - self.big_file_id = big_file_id # string + def __init__(self, small_file_id: str, big_file_id: str, *, + client=None, raw=None): + self.small_file_id = small_file_id + self.big_file_id = big_file_id + + self._client = client + self.raw = raw + + @staticmethod + def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): + if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)): + return None + + if not isinstance(chat_photo.photo_small, types.FileLocation): + return None + + if not isinstance(chat_photo.photo_big, types.FileLocation): + return None + + photo_id = getattr(chat_photo, "photo_id", 0) + loc_small = chat_photo.photo_small + loc_big = chat_photo.photo_big + + return ChatPhoto( + small_file_id=encode( + pack( + " Date: Sat, 15 Dec 2018 17:31:22 +0100 Subject: [PATCH 08/62] Refactor Chat --- pyrogram/client/types/user_and_chats/chat.py | 84 +++++++++++++++----- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 68eaa775..75aa5eac 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object +from .chat_photo import ChatPhoto class Chat(Object): @@ -76,26 +78,15 @@ class Chat(Object): ID = 0xb0700002 - def __init__( - self, - id: int, - type: str, - title: str = None, - username: str = None, - first_name: str = None, - last_name: str = None, - all_members_are_administrators: bool = None, - photo=None, - description: str = None, - invite_link: str = None, - pinned_message=None, - sticker_set_name: str = None, - can_set_sticker_set: bool = None, - members_count: int = None, - restriction_reason: str = None - ): + def __init__(self, id: int, type: str, *, + title: str = None, username: str = None, first_name: str = None, last_name: str = None, + all_members_are_administrators: bool = None, photo=None, description: str = None, + invite_link: str = None, pinned_message=None, sticker_set_name: str = None, + can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None, + client=None, raw=None): self.id = id self.type = type + self.title = title self.username = username self.first_name = first_name @@ -109,3 +100,60 @@ class Chat(Object): self.can_set_sticker_set = can_set_sticker_set self.members_count = members_count self.restriction_reason = restriction_reason + + self._client = client + self._raw = raw + + @staticmethod + def parse_user_chat(client, user: types.User) -> "Chat": + return Chat( + id=user.id, + type="private", + username=user.username, + first_name=user.first_name, + last_name=user.last_name, + photo=ChatPhoto.parse(client, user.photo), + restriction_reason=user.restriction_reason, + client=client, + raw=user + ) + + @staticmethod + def parse_chat_chat(client, chat: types.Chat) -> "Chat": + admins_enabled = getattr(chat, "admins_enabled", None) + + if admins_enabled is not None: + admins_enabled = not admins_enabled + + return Chat( + id=-chat.id, + type="group", + title=chat.title, + all_members_are_administrators=admins_enabled, + photo=ChatPhoto.parse(client, getattr(chat, "photo", None)), + client=client, + raw=chat + ) + + @staticmethod + def parse_channel_chat(client, channel: types.Channel) -> "Chat": + return Chat( + id=int("-100" + str(channel.id)), + type="supergroup" if channel.megagroup else "channel", + title=channel.title, + username=getattr(channel, "username", None), + photo=ChatPhoto.parse(client, getattr(channel, "photo", None)), + restriction_reason=getattr(channel, "restriction_reason", None), + client=client, + raw=channel + ) + + @staticmethod + def parse(client, message: types.Message or types.MessageService, users: dict, chats: dict) -> "Chat": + if isinstance(message.to_id, types.PeerUser): + return Chat.parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id]) + + if isinstance(message.to_id, types.PeerChat): + return Chat.parse_chat_chat(chats[message.to_id.chat_id]) + + return Chat.parse_channel_chat(client, chats[message.to_id.channel_id]) From 905f4b8e62fde7bbf9cad7159ffe180b5bba4340 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 09/62] Refactor Voice and Audio --- .../client/types/messages_and_media/audio.py | 48 ++++++++++++++----- .../client/types/messages_and_media/voice.py | 38 +++++++++++---- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 37f91992..3f2b3c20 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -16,7 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Audio(Object): @@ -53,18 +58,10 @@ class Audio(Object): ID = 0xb0700006 - def __init__( - self, - file_id: str, - duration: int, - thumb=None, - file_name: str = None, - mime_type: str = None, - file_size: int = None, - date: int = None, - performer: str = None, - title: str = None - ): + def __init__(self, file_id: str, duration: int, *, + thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, + performer: str = None, title: str = None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -74,3 +71,30 @@ class Audio(Object): self.duration = duration self.performer = performer self.title = title + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio": + return Audio( + file_id=encode( + pack( + ". +from pyrogram.api import types from pyrogram.api.core import Object +from ...ext.utils import encode +from struct import pack class Voice(Object): @@ -44,17 +47,36 @@ class Voice(Object): ID = 0xb0700009 - def __init__( - self, - file_id: str, - duration: int, - waveform: bytes = None, - mime_type: str = None, - file_size: int = None, - date: int = None): + def __init__(self, file_id: str, duration: int, *, + waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None, + client=None, raw=None): self.file_id = file_id self.duration = duration self.waveform = waveform self.mime_type = mime_type self.file_size = file_size self.date = date + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice": + return Voice( + file_id=encode( + pack( + " Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 10/62] Refactor Animation, Video and VideoNote --- .../types/messages_and_media/animation.py | 48 ++++++++++++++----- .../client/types/messages_and_media/video.py | 45 ++++++++++++----- .../types/messages_and_media/video_note.py | 41 ++++++++++++---- 3 files changed, 100 insertions(+), 34 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index a8641e9e..0e36d5cf 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -16,7 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Animation(Object): @@ -53,18 +58,9 @@ class Animation(Object): ID = 0xb0700025 - def __init__( - self, - file_id: str, - width: int, - height: int, - duration: int, - thumb=None, - file_name: str = None, - mime_type: str = None, - file_size: int = None, - date: int = None - ): + def __init__(self, file_id: str, width: int, height: int, duration: int, *, + thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -74,3 +70,31 @@ class Animation(Object): self.width = width self.height = height self.duration = duration + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo, + file_name: str) -> "Animation": + return Animation( + file_id=encode( + pack( + ". +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Video(Object): @@ -53,18 +58,9 @@ class Video(Object): ID = 0xb0700008 - def __init__( - self, - file_id: str, - width: int, - height: int, - duration: int, - thumb=None, - file_name: str = None, - mime_type: str = None, - file_size: int = None, - date: int = None - ): + def __init__(self, file_id: str, width: int, height: int, duration: int, *, + thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -74,3 +70,28 @@ class Video(Object): self.width = width self.height = height self.duration = duration + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video": + return Video( + file_id=encode( + pack( + ". +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class VideoNote(Object): @@ -47,16 +52,9 @@ class VideoNote(Object): ID = 0xb0700010 - def __init__( - self, - file_id: str, - length: int, - duration: int, - thumb=None, - mime_type: str = None, - file_size: int = None, - date: int = None - ): + def __init__(self, file_id: str, length: int, duration: int, *, + thumb=None, mime_type: str = None, file_size: int = None, date: int = None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.mime_type = mime_type @@ -64,3 +62,26 @@ class VideoNote(Object): self.date = date self.length = length self.duration = duration + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote": + return VideoNote( + file_id=encode( + pack( + " Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 11/62] Refactor Photo and PhotoSize --- .../client/types/messages_and_media/photo.py | 61 ++++++++++++++++++- .../types/messages_and_media/photo_size.py | 37 ++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 4037025b..b1021d93 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -16,7 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from base64 import b64encode +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Photo(Object): @@ -35,7 +41,60 @@ class Photo(Object): ID = 0xb0700027 - def __init__(self, id: str, date: int, sizes: list): + def __init__(self, id: str, date: int, sizes: list, *, + client=None, raw=None): self.id = id self.date = date self.sizes = sizes + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, photo: types.Photo): + if isinstance(photo, types.Photo): + raw_sizes = photo.sizes + sizes = [] + + for raw_size in raw_sizes: + if isinstance(raw_size, (types.PhotoSize, types.PhotoCachedSize)): + + if isinstance(raw_size, types.PhotoSize): + file_size = raw_size.size + elif isinstance(raw_size, types.PhotoCachedSize): + file_size = len(raw_size.bytes) + else: + file_size = 0 + + loc = raw_size.location + + if isinstance(loc, types.FileLocation): + size = PhotoSize( + file_id=encode( + pack( + " Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 12/62] Refactor Venue and Location --- .../types/messages_and_media/location.py | 17 ++++++++++- .../client/types/messages_and_media/venue.py | 28 +++++++++++++------ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index be8c839f..3ab9bfa7 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object @@ -32,6 +33,20 @@ class Location(Object): ID = 0xb0700012 - def __init__(self, longitude: float, latitude: float): + def __init__(self, longitude: float, latitude: float, *, + client=None, raw=None): self.longitude = longitude self.latitude = latitude + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, geo_point: types.GeoPoint) -> "Location": + if isinstance(geo_point, types.GeoPoint): + return Location( + longitude=geo_point.long, + latitude=geo_point.lat, + client=client, + raw=geo_point + ) diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 3c5b2b05..8225c36b 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object +from .location import Location class Venue(Object): @@ -43,16 +45,26 @@ class Venue(Object): ID = 0xb0700013 - def __init__( - self, - location, - title: str, - address: str, - foursquare_id: str = None, - foursquare_type: str = None - ): + def __init__(self, location, title: str, address: str, *, + foursquare_id: str = None, foursquare_type: str = None, + client=None, raw=None): self.location = location self.title = title self.address = address self.foursquare_id = foursquare_id self.foursquare_type = foursquare_type + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, venue: types.MessageMediaVenue): + return Venue( + location=Location.parse(client, venue.geo), + title=venue.title, + address=venue.address, + foursquare_id=venue.venue_id or None, + foursquare_type=venue.venue_type, + client=client, + raw=venue + ) From 334fb8d0ba63d72fc6f9cf4c6683c86072af837a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 13/62] Refactor Sticker, Contact and Document --- .../types/messages_and_media/contact.py | 27 +++++++--- .../types/messages_and_media/document.py | 41 ++++++++++---- .../types/messages_and_media/sticker.py | 53 ++++++++++++++----- 3 files changed, 90 insertions(+), 31 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 2569f03a..2c686a5f 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object @@ -41,16 +42,26 @@ class Contact(Object): ID = 0xb0700011 - def __init__( - self, - phone_number: str, - first_name: str, - last_name: str = None, - user_id: int = None, - vcard: str = None - ): + def __init__(self, phone_number: str, first_name: str, *, + last_name: str = None, user_id: int = None, vcard: str = None, + client=None, raw=None): self.phone_number = phone_number self.first_name = first_name self.last_name = last_name self.user_id = user_id self.vcard = vcard + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, contact: types.MessageMediaContact) -> "Contact": + return Contact( + phone_number=contact.phone_number, + first_name=contact.first_name, + last_name=contact.last_name or None, + vcard=contact.vcard or None, + user_id=contact.user_id or None, + client=client, + raw=contact + ) diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index d87fa666..5aa047cf 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -16,7 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Document(Object): @@ -44,18 +49,36 @@ class Document(Object): ID = 0xb0700007 - def __init__( - self, - file_id: str, - thumb=None, - file_name: str = None, - mime_type: str = None, - file_size: int = None, - date: int = None - ): + def __init__(self, file_id: str, *, + thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.file_name = file_name self.mime_type = mime_type self.file_size = file_size self.date = date + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, document: types.Document, file_name: str) -> "Document": + return Document( + file_id=encode( + pack( + ". +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Sticker(Object): @@ -57,20 +62,10 @@ class Sticker(Object): # TODO: Add mask position ID = 0xb0700017 - def __init__( - self, - file_id: str, - width: int, - height: int, - thumb=None, - file_name: str = None, - mime_type: str = None, - file_size: int = None, - date: int = None, - emoji: str = None, - set_name: str = None, - mask_position=None - ): + def __init__(self, file_id: str, width: int, height: int, *, + thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, + emoji: str = None, set_name: str = None, mask_position=None, + client=None, raw=None): self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -82,3 +77,33 @@ class Sticker(Object): self.emoji = emoji self.set_name = set_name self.mask_position = mask_position + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, set_name: str, + sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": + return Sticker( + file_id=encode( + pack( + " Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH 14/62] Refactor Message and MessageEntity --- .../types/messages_and_media/message.py | 288 ++++++++++++++++-- .../messages_and_media/message_entity.py | 47 ++- 2 files changed, 310 insertions(+), 25 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 36c35837..0829c2db 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -16,13 +16,17 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api import types +import pyrogram +from pyrogram.api import types, functions from pyrogram.api.core import Object -from pyrogram.api.errors import MessageIdsEmpty -from ..bots import InlineKeyboardMarkup, ReplyKeyboardMarkup +from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid +from .contact import Contact +from .location import Location +from .message_entity import MessageEntity from ..messages_and_media.photo import Photo from ..user_and_chats.chat import Chat from ..user_and_chats.user import User +from ...ext.utils import Str class Message(Object): @@ -222,18 +226,19 @@ class Message(Object): # TODO: Add game missing field. Also invoice, successful_payment, connected_website ID = 0xb0700003 - def __init__(self, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, - forward_from_chat=None, forward_from_message_id: int = None, forward_signature: str = None, - forward_date: int = None, reply_to_message=None, mentioned=None, empty=None, service=None, media=None, - edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, - entities: list = None, caption_entities: list = None, audio=None, document=None, photo=None, - sticker=None, animation=None, video=None, voice=None, video_note=None, caption: str = None, - contact=None, location=None, venue=None, web_page=None, new_chat_members: list = None, - left_chat_member=None, new_chat_title: str = None, new_chat_photo=None, delete_chat_photo: bool = None, - group_chat_created: bool = None, supergroup_chat_created: bool = None, - channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, - pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, - command: list = None, reply_markup=None, + def __init__(self, message_id: int, *, + date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, + forward_from_message_id: int = None, forward_signature: str = None, forward_date: int = None, + reply_to_message=None, mentioned=None, empty=None, service=None, media=None, edit_date: int = None, + media_group_id: str = None, author_signature: str = None, text: str = None, entities: list = None, + caption_entities: list = None, audio=None, document=None, photo=None, sticker=None, animation=None, + video=None, voice=None, video_note=None, caption: str = None, contact=None, location=None, venue=None, + web_page=None, new_chat_members: list = None, left_chat_member=None, new_chat_title: str = None, + new_chat_photo=None, delete_chat_photo: bool = None, group_chat_created: bool = None, + supergroup_chat_created: bool = None, channel_chat_created: bool = None, + migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, pinned_message=None, + views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, command: list = None, + reply_markup=None, client=None, raw=None): self.message_id = message_id self.date = date @@ -286,6 +291,255 @@ class Message(Object): self.command = command self.reply_markup = reply_markup + self._client = client + self._raw = raw + + @staticmethod + def parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, + replies: int = 1): + if isinstance(message, types.MessageEmpty): + return Message( + message_id=message.id, + client=client, + raw=message + ) + + if isinstance(message, types.MessageService): + action = message.action + + new_chat_members = None + left_chat_member = None + new_chat_title = None + delete_chat_photo = None + migrate_to_chat_id = None + migrate_from_chat_id = None + group_chat_created = None + channel_chat_created = None + new_chat_photo = None + + if isinstance(action, types.MessageActionChatAddUser): + new_chat_members = [User.parse(client, users[i]) for i in action.users] + elif isinstance(action, types.MessageActionChatJoinedByLink): + new_chat_members = [User.parse(client, users[message.from_id])] + elif isinstance(action, types.MessageActionChatDeleteUser): + left_chat_member = User.parse(client, users[action.user_id]) + elif isinstance(action, types.MessageActionChatEditTitle): + new_chat_title = action.title + elif isinstance(action, types.MessageActionChatDeletePhoto): + delete_chat_photo = True + elif isinstance(action, types.MessageActionChatMigrateTo): + migrate_to_chat_id = action.channel_id + elif isinstance(action, types.MessageActionChannelMigrateFrom): + migrate_from_chat_id = action.chat_id + elif isinstance(action, types.MessageActionChatCreate): + group_chat_created = True + elif isinstance(action, types.MessageActionChannelCreate): + channel_chat_created = True + elif isinstance(action, types.MessageActionChatEditPhoto): + new_chat_photo = Photo.parse(client, action.photo) + + parsed_message = Message( + message_id=message.id, + date=message.date, + chat=Chat.parse(client, message, users, chats), + from_user=User.parse(client, users.get(message.from_id, None)), + service=True, + new_chat_members=new_chat_members, + left_chat_member=left_chat_member, + new_chat_title=new_chat_title, + new_chat_photo=new_chat_photo, + delete_chat_photo=delete_chat_photo, + migrate_to_chat_id=int("-100" + str(migrate_to_chat_id)) if migrate_to_chat_id else None, + migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None, + group_chat_created=group_chat_created, + channel_chat_created=channel_chat_created, + client=client, + raw=message + # TODO: supergroup_chat_created + ) + + if isinstance(action, types.MessageActionPinMessage): + try: + parsed_message.pinned_message = client.get_messages( + parsed_message.chat.id, + reply_to_message_ids=message.id, + replies=0 + ) + except MessageIdsEmpty: + pass + + return parsed_message + + if isinstance(message, types.Message): + entities = [MessageEntity.parse(client, entity, users) for entity in message.entities] + entities = list(filter(lambda x: x is not None, entities)) + + forward_from = None + forward_from_chat = None + forward_from_message_id = None + forward_signature = None + forward_date = None + + forward_header = message.fwd_from + + if forward_header: + forward_date = forward_header.date + + if forward_header.from_id: + forward_from = User.parse(client, users[forward_header.from_id]) + else: + forward_from_chat = Chat.parse_channel_chat(client, chats[forward_header.channel_id]) + forward_from_message_id = forward_header.channel_post + forward_signature = forward_header.post_author + + photo = None + location = None + contact = None + venue = None + audio = None + voice = None + animation = None + video = None + video_note = None + sticker = None + document = None + web_page = None + + media = message.media + + if media: + if isinstance(media, types.MessageMediaPhoto): + photo = Photo.parse(client, media.photo) + elif isinstance(media, types.MessageMediaGeo): + location = Location.parse(client, media.geo) + elif isinstance(media, types.MessageMediaContact): + contact = Contact.parse(client, media) + elif isinstance(media, types.MessageMediaVenue): + venue = pyrogram.Venue.parse(client, media) + elif isinstance(media, types.MessageMediaDocument): + doc = media.document + + if isinstance(doc, types.Document): + attributes = {type(i): i for i in doc.attributes} + + file_name = getattr( + attributes.get( + types.DocumentAttributeFilename, None + ), "file_name", None + ) + + if types.DocumentAttributeAudio in attributes: + audio_attributes = attributes[types.DocumentAttributeAudio] + + if audio_attributes.voice: + voice = pyrogram.Voice.parse(client, doc, audio_attributes) + else: + audio = pyrogram.Audio.parse(client, doc, audio_attributes, file_name) + elif types.DocumentAttributeAnimated in attributes: + video_attributes = attributes.get(types.DocumentAttributeVideo, None) + + animation = pyrogram.Animation.parse(client, doc, video_attributes, file_name) + elif types.DocumentAttributeVideo in attributes: + video_attributes = attributes[types.DocumentAttributeVideo] + + if video_attributes.round_message: + video_note = pyrogram.VideoNote.parse(client, doc, video_attributes) + else: + video = pyrogram.Video.parse(client, doc, video_attributes, file_name) + elif types.DocumentAttributeSticker in attributes: + image_size_attributes = attributes.get(types.DocumentAttributeImageSize, None) + sticker_attribute = attributes[types.DocumentAttributeSticker] + + if isinstance(sticker_attribute.stickerset, types.InputStickerSetID): + try: + set_name = client.send( + functions.messages.GetStickerSet(sticker_attribute.stickerset) + ).set.short_name + except StickersetInvalid: + set_name = None + else: + set_name = None + + sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes, + set_name, sticker_attribute, file_name) + else: + document = pyrogram.Document.parse(client, doc, file_name) + elif isinstance(media, types.MessageMediaWebPage): + web_page = True + else: + media = None + + reply_markup = message.reply_markup + + if reply_markup: + if isinstance(reply_markup, types.ReplyKeyboardForceReply): + reply_markup = pyrogram.ForceReply.read(reply_markup) + elif isinstance(reply_markup, types.ReplyKeyboardMarkup): + reply_markup = pyrogram.ReplyKeyboardMarkup.read(reply_markup) + elif isinstance(reply_markup, types.ReplyInlineMarkup): + reply_markup = pyrogram.InlineKeyboardMarkup.read(reply_markup) + elif isinstance(reply_markup, types.ReplyKeyboardHide): + reply_markup = pyrogram.ReplyKeyboardRemove.read(reply_markup) + else: + reply_markup = None + + parsed_message = Message( + message_id=message.id, + date=message.date, + chat=Chat.parse(client, message, users, chats), + from_user=User.parse(client, users.get(message.from_id, None)), + text=Str(message.message) or None if media is None else None, + caption=Str(message.message) or None if media is not None else None, + entities=entities or None if media is None else None, + caption_entities=entities or None if media is not None else None, + author_signature=message.post_author, + forward_from=forward_from, + forward_from_chat=forward_from_chat, + forward_from_message_id=forward_from_message_id, + forward_signature=forward_signature, + forward_date=forward_date, + mentioned=message.mentioned, + media=bool(media) or None, + edit_date=message.edit_date, + media_group_id=message.grouped_id, + photo=photo, + location=location, + contact=contact, + venue=venue, + audio=audio, + voice=voice, + animation=animation, + video=video, + video_note=video_note, + sticker=sticker, + document=document, + web_page=web_page, + views=message.views, + via_bot=User.parse(client, users.get(message.via_bot_id, None)), + outgoing=message.out, + reply_markup=reply_markup, + client=client, + raw=message + ) + + if parsed_message.text: + parsed_message.text.init(parsed_message._client, parsed_message.entities or []) + + if parsed_message.caption: + parsed_message.caption.init(parsed_message._client, parsed_message.caption_entities or []) + + if message.reply_to_msg_id and replies: + try: + parsed_message.reply_to_message = client.get_messages( + parsed_message.chat.id, + reply_to_message_ids=message.id, + replies=replies - 1 + ) + except MessageIdsEmpty: + pass + + return parsed_message + def reply(self, text: str, quote: bool = None, @@ -549,9 +803,9 @@ class Message(Object): ``ValueError``: If the provided index or position is out of range or the button label was not found ``TimeoutError``: If, after clicking an inline button, the bot fails to answer within 10 seconds """ - if isinstance(self.reply_markup, ReplyKeyboardMarkup): + if isinstance(self.reply_markup, pyrogram.ReplyKeyboardMarkup): return self.reply(x) - elif isinstance(self.reply_markup, InlineKeyboardMarkup): + elif isinstance(self.reply_markup, pyrogram.InlineKeyboardMarkup): if isinstance(x, int) and y is None: try: button = [ diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index f8f41734..9752bd28 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from pyrogram.api.core import Object +from ..user_and_chats.user import User class MessageEntity(Object): @@ -45,16 +47,45 @@ class MessageEntity(Object): ID = 0xb0700004 - def __init__( - self, - type: str, - offset: int, - length: int, - url: str = None, - user=None - ): + ENTITIES = { + types.MessageEntityMention.ID: "mention", + types.MessageEntityHashtag.ID: "hashtag", + types.MessageEntityCashtag.ID: "cashtag", + types.MessageEntityBotCommand.ID: "bot_command", + types.MessageEntityUrl.ID: "url", + types.MessageEntityEmail.ID: "email", + types.MessageEntityBold.ID: "bold", + types.MessageEntityItalic.ID: "italic", + types.MessageEntityCode.ID: "code", + types.MessageEntityPre.ID: "pre", + types.MessageEntityTextUrl.ID: "text_link", + types.MessageEntityMentionName.ID: "text_mention", + types.MessageEntityPhone.ID: "phone_number" + } + + def __init__(self, type: str, offset: int, length: int, *, + url: str = None, user=None, + client=None, raw=None): self.type = type self.offset = offset self.length = length self.url = url self.user = user + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, entity, users: dict) -> "MessageEntity" or None: + type = MessageEntity.ENTITIES.get(entity.ID, None) + + if type is None: + return None + + return MessageEntity( + type=type, + offset=entity.offset, + length=entity.length, + url=getattr(entity, "url", None), + user=User.parse(client, users.get(getattr(entity, "user_id", None), None)) + ) From 6ab1c87050b990fee7250486c5fe190fbcd161ca Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 15:24:51 +0100 Subject: [PATCH 15/62] Add PyrogramType --- .../types/messages_and_media/animation.py | 6 +-- .../client/types/messages_and_media/audio.py | 6 +-- .../types/messages_and_media/contact.py | 6 +-- .../types/messages_and_media/document.py | 6 +-- .../types/messages_and_media/location.py | 6 +-- .../types/messages_and_media/message.py | 7 ++- .../messages_and_media/message_entity.py | 6 +-- .../types/messages_and_media/messages.py | 6 +-- .../client/types/messages_and_media/photo.py | 6 +-- .../types/messages_and_media/photo_size.py | 6 +-- .../types/messages_and_media/sticker.py | 5 +-- .../messages_and_media/user_profile_photos.py | 6 +-- .../client/types/messages_and_media/venue.py | 6 +-- .../client/types/messages_and_media/video.py | 6 +-- .../types/messages_and_media/video_note.py | 6 +-- .../client/types/messages_and_media/voice.py | 11 +++-- pyrogram/client/types/pyrogram_type.py | 44 +++++++++++++++++++ pyrogram/client/types/user_and_chats/chat.py | 6 +-- .../client/types/user_and_chats/chat_photo.py | 2 +- pyrogram/client/types/user_and_chats/user.py | 8 ++-- .../types/user_and_chats/user_status.py | 6 +-- 21 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 pyrogram/client/types/pyrogram_type.py diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 0e36d5cf..7447f4ab 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Animation(Object): +class Animation(PyrogramType): """This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound). Args: @@ -56,8 +56,6 @@ class Animation(Object): Date the animation was sent in Unix time. """ - ID = 0xb0700025 - def __init__(self, file_id: str, width: int, height: int, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 3f2b3c20..2cd3a1bd 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Audio(Object): +class Audio(PyrogramType): """This object represents an audio file to be treated as music by the Telegram clients. Args: @@ -56,8 +56,6 @@ class Audio(Object): Title of the audio as defined by sender or by audio tags. """ - ID = 0xb0700006 - def __init__(self, file_id: str, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, performer: str = None, title: str = None, diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 2c686a5f..2a3f8a21 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Contact(Object): +class Contact(PyrogramType): """This object represents a phone contact. Args: @@ -40,8 +40,6 @@ class Contact(Object): Additional data about the contact in the form of a vCard. """ - ID = 0xb0700011 - def __init__(self, phone_number: str, first_name: str, *, last_name: str = None, user_id: int = None, vcard: str = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 5aa047cf..412954ef 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Document(Object): +class Document(PyrogramType): """This object represents a general file (as opposed to photos, voice messages, audio files, ...). Args: @@ -47,8 +47,6 @@ class Document(Object): Date the document was sent in Unix time. """ - ID = 0xb0700007 - def __init__(self, file_id: str, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 3ab9bfa7..1276a783 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Location(Object): +class Location(PyrogramType): """This object represents a point on the map. Args: @@ -31,8 +31,6 @@ class Location(Object): Latitude as defined by sender. """ - ID = 0xb0700012 - def __init__(self, longitude: float, latitude: float, *, client=None, raw=None): self.longitude = longitude diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 0829c2db..8d84a513 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -18,18 +18,18 @@ import pyrogram from pyrogram.api import types, functions -from pyrogram.api.core import Object from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid from .contact import Contact from .location import Location from .message_entity import MessageEntity from ..messages_and_media.photo import Photo +from ..pyrogram_type import PyrogramType from ..user_and_chats.chat import Chat from ..user_and_chats.user import User from ...ext.utils import Str -class Message(Object): +class Message(PyrogramType): """This object represents a message. Args: @@ -224,7 +224,6 @@ class Message(Object): """ # TODO: Add game missing field. Also invoice, successful_payment, connected_website - ID = 0xb0700003 def __init__(self, message_id: int, *, date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, @@ -461,7 +460,7 @@ class Message(Object): set_name = None sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes, - set_name, sticker_attribute, file_name) + set_name, sticker_attribute, file_name) else: document = pyrogram.Document.parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 9752bd28..67f32806 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType from ..user_and_chats.user import User -class MessageEntity(Object): +class MessageEntity(PyrogramType): """This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc. @@ -45,8 +45,6 @@ class MessageEntity(Object): For "text_mention" only, the mentioned user. """ - ID = 0xb0700004 - ENTITIES = { types.MessageEntityMention.ID: "mention", types.MessageEntityHashtag.ID: "hashtag", diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 7a2546a9..ef32f0b2 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Messages(Object): +class Messages(PyrogramType): """This object represents a chat's messages. Args: @@ -30,8 +30,6 @@ class Messages(Object): Requested messages. """ - ID = 0xb0700026 - def __init__(self, total_count: int, messages: list): self.total_count = total_count self.messages = messages diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index b1021d93..970579af 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -20,12 +20,12 @@ from base64 import b64encode from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Photo(Object): +class Photo(PyrogramType): """This object represents a Photo. Args: @@ -39,8 +39,6 @@ class Photo(Object): Available sizes of this photo. """ - ID = 0xb0700027 - def __init__(self, id: str, date: int, sizes: list, *, client=None, raw=None): self.id = id diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index 39b9f756..cb94a6e6 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -19,11 +19,11 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from pyrogram.client.ext.utils import encode +from ..pyrogram_type import PyrogramType -class PhotoSize(Object): +class PhotoSize(PyrogramType): """This object represents one size of a photo or a file/sticker thumbnail. Args: @@ -40,8 +40,6 @@ class PhotoSize(Object): File size. """ - ID = 0xb0700005 - def __init__(self, file_id: str, width: int, height: int, file_size: int, *, client=None, raw=None): self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index a3dda2d5..7b5bfa70 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Sticker(Object): +class Sticker(PyrogramType): """This object represents a sticker. Args: @@ -60,7 +60,6 @@ class Sticker(Object): """ # TODO: Add mask position - ID = 0xb0700017 def __init__(self, file_id: str, width: int, height: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index c8ca9e39..bf53b13e 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class UserProfilePhotos(Object): +class UserProfilePhotos(PyrogramType): """This object represents a user's profile pictures. Args: @@ -30,8 +30,6 @@ class UserProfilePhotos(Object): Requested profile pictures. """ - ID = 0xb0700014 - def __init__(self, total_count: int, photos: list): self.total_count = total_count self.photos = photos diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 8225c36b..662a416f 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object from .location import Location +from ..pyrogram_type import PyrogramType -class Venue(Object): +class Venue(PyrogramType): """This object represents a venue. Args: @@ -43,8 +43,6 @@ class Venue(Object): """ - ID = 0xb0700013 - def __init__(self, location, title: str, address: str, *, foursquare_id: str = None, foursquare_type: str = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 4873730c..05d28f45 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Video(Object): +class Video(PyrogramType): """This object represents a video file. Args: @@ -56,8 +56,6 @@ class Video(Object): Date the video was sent in Unix time. """ - ID = 0xb0700008 - def __init__(self, file_id: str, width: int, height: int, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 0b07c00b..ddcd74f3 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class VideoNote(Object): +class VideoNote(PyrogramType): """This object represents a video message (available in Telegram apps as of v.4.0). Args: @@ -50,8 +50,6 @@ class VideoNote(Object): Date the video note was sent in Unix time. """ - ID = 0xb0700010 - def __init__(self, file_id: str, length: int, duration: int, *, thumb=None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index da9d6d51..c993ea5c 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -16,13 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api import types -from pyrogram.api.core import Object -from ...ext.utils import encode from struct import pack +from pyrogram.api import types +from ..pyrogram_type import PyrogramType +from ...ext.utils import encode -class Voice(Object): + +class Voice(PyrogramType): """This object represents a voice note. Args: @@ -45,8 +46,6 @@ class Voice(Object): Date the voice was sent in Unix time. """ - ID = 0xb0700009 - def __init__(self, file_id: str, duration: int, *, waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py new file mode 100644 index 00000000..20b476a2 --- /dev/null +++ b/pyrogram/client/types/pyrogram_type.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from collections import OrderedDict +from json import dumps, JSONEncoder + + +class PyrogramType: + def __str__(self): + return dumps(self, cls=Encoder, indent=4) + + +def remove_none(obj): + if isinstance(obj, (list, tuple, set)): + return type(obj)(remove_none(x) for x in obj if x is not None) + elif isinstance(obj, dict): + return type(obj)((remove_none(k), remove_none(v)) for k, v in obj.items() if k is not None and v is not None) + else: + return obj + + +class Encoder(JSONEncoder): + def default(self, o: PyrogramType): + content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} + + return OrderedDict( + [("_", "pyrogram:{}".format(o.__class__.__name__))] + + [i for i in remove_none(content).items()] + ) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 75aa5eac..6e1c675e 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object from .chat_photo import ChatPhoto +from ..pyrogram_type import PyrogramType -class Chat(Object): +class Chat(PyrogramType): """This object represents a chat. Args: @@ -76,8 +76,6 @@ class Chat(Object): The reason why this chat might be unavailable to some users. """ - ID = 0xb0700002 - def __init__(self, id: int, type: str, *, title: str = None, username: str = None, first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, photo=None, description: str = None, diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index 485777e5..c7ef4c80 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -42,7 +42,7 @@ class ChatPhoto(Object): self.big_file_id = big_file_id self._client = client - self.raw = raw + self._raw = raw @staticmethod def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index e460522b..80660bcc 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -17,12 +17,12 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object -from .user_status import UserStatus from .chat_photo import ChatPhoto +from .user_status import UserStatus +from ..pyrogram_type import PyrogramType -class User(Object): +class User(PyrogramType): """This object represents a Telegram user or bot. Args: @@ -69,8 +69,6 @@ class User(Object): The reason why this bot might be unavailable to some users. """ - ID = 0xb0700001 - def __init__(self, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool, is_bot: bool, first_name: str, *, last_name: str = None, status=None, username: str = None, language_code: str = None, diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 651d8db6..f99d356d 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class UserStatus(Object): +class UserStatus(PyrogramType): """This object represents a User status (Last Seen privacy). .. note:: @@ -62,8 +62,6 @@ class UserStatus(Object): always shown to blocked users), None otherwise. """ - ID = 0xb0700031 - def __init__(self, user_id: int, *, online: bool = None, offline: bool = None, date: int = None, recently: bool = None, within_week: bool = None, within_month: bool = None, long_time_ago: bool = None, From 7a8873029d3000a7572502381e09425af47a02c0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 15:25:56 +0100 Subject: [PATCH 16/62] Don't parse custom types in the code generator --- compiler/api/compiler.py | 65 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index cacd9342..ac507537 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -475,38 +475,39 @@ def start(): f.write("\n 0x3072cfa1: \"pyrogram.api.core.GzipPacked\",") f.write("\n 0x5bb8e511: \"pyrogram.api.core.Message\",") - f.write("\n 0xb0700000: \"pyrogram.client.types.Update\",") - f.write("\n 0xb0700001: \"pyrogram.client.types.User\",") - f.write("\n 0xb0700002: \"pyrogram.client.types.Chat\",") - f.write("\n 0xb0700003: \"pyrogram.client.types.Message\",") - f.write("\n 0xb0700004: \"pyrogram.client.types.MessageEntity\",") - f.write("\n 0xb0700005: \"pyrogram.client.types.PhotoSize\",") - f.write("\n 0xb0700006: \"pyrogram.client.types.Audio\",") - f.write("\n 0xb0700007: \"pyrogram.client.types.Document\",") - f.write("\n 0xb0700008: \"pyrogram.client.types.Video\",") - f.write("\n 0xb0700009: \"pyrogram.client.types.Voice\",") - f.write("\n 0xb0700010: \"pyrogram.client.types.VideoNote\",") - f.write("\n 0xb0700011: \"pyrogram.client.types.Contact\",") - f.write("\n 0xb0700012: \"pyrogram.client.types.Location\",") - f.write("\n 0xb0700013: \"pyrogram.client.types.Venue\",") - f.write("\n 0xb0700014: \"pyrogram.client.types.UserProfilePhotos\",") - f.write("\n 0xb0700015: \"pyrogram.client.types.ChatPhoto\",") - f.write("\n 0xb0700016: \"pyrogram.client.types.ChatMember\",") - f.write("\n 0xb0700017: \"pyrogram.client.types.Sticker\",") - f.write("\n 0xb0700018: \"pyrogram.client.types.bots.ForceReply\",") - f.write("\n 0xb0700019: \"pyrogram.client.types.bots.InlineKeyboardButton\",") - f.write("\n 0xb0700020: \"pyrogram.client.types.bots.InlineKeyboardMarkup\",") - f.write("\n 0xb0700021: \"pyrogram.client.types.bots.KeyboardButton\",") - f.write("\n 0xb0700022: \"pyrogram.client.types.bots.ReplyKeyboardMarkup\",") - f.write("\n 0xb0700023: \"pyrogram.client.types.bots.ReplyKeyboardRemove\",") - f.write("\n 0xb0700024: \"pyrogram.client.types.CallbackQuery\",") - f.write("\n 0xb0700025: \"pyrogram.client.types.Animation\",") - f.write("\n 0xb0700026: \"pyrogram.client.types.Messages\",") - f.write("\n 0xb0700027: \"pyrogram.client.types.Photo\",") - f.write("\n 0xb0700028: \"pyrogram.client.types.Dialog\",") - f.write("\n 0xb0700029: \"pyrogram.client.types.Dialogs\",") - f.write("\n 0xb0700030: \"pyrogram.client.types.ChatMembers\",") - f.write("\n 0xb0700031: \"pyrogram.client.types.UserStatus\"") + # TODO Remove completely + # f.write("\n 0xb0700000: \"pyrogram.client.types.Update\",") + # f.write("\n 0xb0700001: \"pyrogram.client.types.User\",") + # f.write("\n 0xb0700002: \"pyrogram.client.types.Chat\",") + # f.write("\n 0xb0700003: \"pyrogram.client.types.Message\",") + # f.write("\n 0xb0700004: \"pyrogram.client.types.MessageEntity\",") + # f.write("\n 0xb0700005: \"pyrogram.client.types.PhotoSize\",") + # f.write("\n 0xb0700006: \"pyrogram.client.types.Audio\",") + # f.write("\n 0xb0700007: \"pyrogram.client.types.Document\",") + # f.write("\n 0xb0700008: \"pyrogram.client.types.Video\",") + # f.write("\n 0xb0700009: \"pyrogram.client.types.Voice\",") + # f.write("\n 0xb0700010: \"pyrogram.client.types.VideoNote\",") + # f.write("\n 0xb0700011: \"pyrogram.client.types.Contact\",") + # f.write("\n 0xb0700012: \"pyrogram.client.types.Location\",") + # f.write("\n 0xb0700013: \"pyrogram.client.types.Venue\",") + # f.write("\n 0xb0700014: \"pyrogram.client.types.UserProfilePhotos\",") + # f.write("\n 0xb0700015: \"pyrogram.client.types.ChatPhoto\",") + # f.write("\n 0xb0700016: \"pyrogram.client.types.ChatMember\",") + # f.write("\n 0xb0700017: \"pyrogram.client.types.Sticker\",") + # f.write("\n 0xb0700018: \"pyrogram.client.types.bots.ForceReply\",") + # f.write("\n 0xb0700019: \"pyrogram.client.types.bots.InlineKeyboardButton\",") + # f.write("\n 0xb0700020: \"pyrogram.client.types.bots.InlineKeyboardMarkup\",") + # f.write("\n 0xb0700021: \"pyrogram.client.types.bots.KeyboardButton\",") + # f.write("\n 0xb0700022: \"pyrogram.client.types.bots.ReplyKeyboardMarkup\",") + # f.write("\n 0xb0700023: \"pyrogram.client.types.bots.ReplyKeyboardRemove\",") + # f.write("\n 0xb0700024: \"pyrogram.client.types.CallbackQuery\",") + # f.write("\n 0xb0700025: \"pyrogram.client.types.Animation\",") + # f.write("\n 0xb0700026: \"pyrogram.client.types.Messages\",") + # f.write("\n 0xb0700027: \"pyrogram.client.types.Photo\",") + # f.write("\n 0xb0700028: \"pyrogram.client.types.Dialog\",") + # f.write("\n 0xb0700029: \"pyrogram.client.types.Dialogs\",") + # f.write("\n 0xb0700030: \"pyrogram.client.types.ChatMembers\",") + # f.write("\n 0xb0700031: \"pyrogram.client.types.UserStatus\"") f.write("\n}\n") From 3ecbb35470c9de9cc2ef46236cc495559971a467 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 15:26:56 +0100 Subject: [PATCH 17/62] Delete Update --- pyrogram/__init__.py | 2 +- pyrogram/client/types/__init__.py | 1 - pyrogram/client/types/update.py | 94 ------------------------------- 3 files changed, 1 insertion(+), 96 deletions(-) delete mode 100644 pyrogram/client/types/update.py diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index b1a5430c..6ea6aa5c 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -29,7 +29,7 @@ from .api.errors import Error from .client.types import ( Audio, Chat, ChatMember, ChatMembers, ChatPhoto, Contact, Document, InputMediaPhoto, InputMediaVideo, InputMediaDocument, InputMediaAudio, InputMediaAnimation, InputPhoneContact, - Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, Update, User, UserStatus, + Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus, UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove ) diff --git a/pyrogram/client/types/__init__.py b/pyrogram/client/types/__init__.py index 74c97ca1..8289a947 100644 --- a/pyrogram/client/types/__init__.py +++ b/pyrogram/client/types/__init__.py @@ -33,7 +33,6 @@ from .messages_and_media import ( Sticker, Venue, Video, VideoNote, Voice, UserProfilePhotos, Message, Messages, MessageEntity ) -from .update import Update from .user_and_chats import ( Chat, ChatMember, ChatMembers, ChatPhoto, Dialog, Dialogs, User, UserStatus diff --git a/pyrogram/client/types/update.py b/pyrogram/client/types/update.py deleted file mode 100644 index 748108de..00000000 --- a/pyrogram/client/types/update.py +++ /dev/null @@ -1,94 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -from pyrogram.api.core import Object - - -class Update(Object): - """This object represents an incoming update. - At most one of the optional parameters can be present in any given update. - - Args: - message (:obj:`Message `, *optional*): - New incoming message of any kind — text, photo, sticker, etc. - - edited_message (:obj:`Message `, *optional*): - New version of a message that is known to the bot and was edited. - - deleted_messages (:obj:`Messages `, *optional*): - Deleted messages. - - channel_post (:obj:`Message `, *optional*): - New incoming channel post of any kind — text, photo, sticker, etc. - - edited_channel_post (:obj:`Message `, *optional*): - New version of a channel post that is known to the bot and was edited. - - deleted_channel_posts (:obj:`Messages `, *optional*): - Deleted channel posts. - - inline_query (:obj:`InlineQuery `, *optional*): - New incoming inline query. - - chosen_inline_result (:obj:`ChosenInlineResult `, *optional*): - The result of an inline query that was chosen by a user and sent to their chat partner. - Please see our documentation on the feedback collecting for details on how to enable these updates - for your bot. - - callback_query (:obj:`CallbackQuery `, *optional*): - New incoming callback query. - - shipping_query (:obj:`ShippingQuery `, *optional*): - New incoming shipping query. Only for invoices with flexible price. - - pre_checkout_query (:obj:`PreCheckoutQuery `, *optional*): - New incoming pre-checkout query. Contains full information about checkout. - - user_status (:obj:`UserStatus `, *optional*): - User status (last seen date) update. - """ - - ID = 0xb0700000 - - def __init__( - self, - message=None, - edited_message=None, - deleted_messages=None, - channel_post=None, - edited_channel_post=None, - deleted_channel_posts=None, - inline_query=None, - chosen_inline_result=None, - callback_query=None, - shipping_query=None, - pre_checkout_query=None, - user_status=None - ): - self.message = message - self.edited_message = edited_message - self.deleted_messages = deleted_messages - self.channel_post = channel_post - self.edited_channel_post = edited_channel_post - self.deleted_channel_posts = deleted_channel_posts - self.inline_query = inline_query - self.chosen_inline_result = chosen_inline_result - self.callback_query = callback_query - self.shipping_query = shipping_query - self.pre_checkout_query = pre_checkout_query - self.user_status = user_status From d62bc1d8ba80519f924c4813adaada833952c841 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 16:22:46 +0100 Subject: [PATCH 18/62] Refactor ChatMember and ChatMembers --- .../types/user_and_chats/chat_member.py | 114 ++++++++++++------ .../types/user_and_chats/chat_members.py | 33 ++++- 2 files changed, 104 insertions(+), 43 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 71267d27..b05630da 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -16,10 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from pyrogram.api import types +from ..pyrogram_type import PyrogramType -class ChatMember(Object): +class ChatMember(PyrogramType): """This object contains information about one member of a chat. Args: @@ -78,40 +79,77 @@ class ChatMember(Object): Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages. """ - ID = 0xb0700016 + def __init__(self, *, user, status: str, until_date: int = None, can_be_edited: bool = None, + can_change_info: bool = None, can_post_messages: bool = None, can_edit_messages: bool = None, + can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None, + can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None, + can_send_media_messages: bool = None, can_send_other_messages: bool = None, + can_add_web_page_previews: bool = None, client=None): + self.user = user + self.status = status + self.until_date = until_date + self.can_be_edited = can_be_edited + self.can_change_info = can_change_info + self.can_post_messages = can_post_messages + self.can_edit_messages = can_edit_messages + self.can_delete_messages = can_delete_messages + self.can_invite_users = can_invite_users + self.can_restrict_members = can_restrict_members + self.can_pin_messages = can_pin_messages + self.can_promote_members = can_promote_members + self.can_send_messages = can_send_messages + self.can_send_media_messages = can_send_media_messages + self.can_send_other_messages = can_send_other_messages + self.can_add_web_page_previews = can_add_web_page_previews - def __init__( - self, - user, - status: str, - until_date: int = None, - can_be_edited: bool = None, - can_change_info: bool = None, - can_post_messages: bool = None, - can_edit_messages: bool = None, - can_delete_messages: bool = None, - can_invite_users: bool = None, - can_restrict_members: bool = None, - can_pin_messages: bool = None, - can_promote_members: bool = None, - can_send_messages: bool = None, - can_send_media_messages: bool = None, - can_send_other_messages: bool = None, - can_add_web_page_previews: bool = None - ): - self.user = user # User - self.status = status # string - self.until_date = until_date # flags.0?int - self.can_be_edited = can_be_edited # flags.1?Bool - self.can_change_info = can_change_info # flags.2?Bool - self.can_post_messages = can_post_messages # flags.3?Bool - self.can_edit_messages = can_edit_messages # flags.4?Bool - self.can_delete_messages = can_delete_messages # flags.5?Bool - self.can_invite_users = can_invite_users # flags.6?Bool - self.can_restrict_members = can_restrict_members # flags.7?Bool - self.can_pin_messages = can_pin_messages # flags.8?Bool - self.can_promote_members = can_promote_members # flags.9?Bool - self.can_send_messages = can_send_messages # flags.10?Bool - self.can_send_media_messages = can_send_media_messages # flags.11?Bool - self.can_send_other_messages = can_send_other_messages # flags.12?Bool - self.can_add_web_page_previews = can_add_web_page_previews # flags.13?Bool + self.client = client + + @staticmethod + def parse(client, member, user) -> "ChatMember": + if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): + return ChatMember(user=user, status="member", client=client) + + if isinstance(member, (types.ChannelParticipantCreator, types.ChatParticipantCreator)): + return ChatMember(user=user, status="creator", client=client) + + if isinstance(member, types.ChatParticipantAdmin): + return ChatMember(user=user, status="administrator", client=client) + + if isinstance(member, types.ChannelParticipantAdmin): + rights = member.admin_rights + + return ChatMember( + user=user, + status="administrator", + can_be_edited=member.can_edit, + can_change_info=rights.change_info, + can_post_messages=rights.post_messages, + can_edit_messages=rights.edit_messages, + can_delete_messages=rights.delete_messages, + can_invite_users=rights.invite_users or rights.invite_link, + can_restrict_members=rights.ban_users, + can_pin_messages=rights.pin_messages, + can_promote_members=rights.add_admins, + client=client + ) + + if isinstance(member, types.ChannelParticipantBanned): + rights = member.banned_rights + + chat_member = ChatMember( + user=user, + status="kicked" if rights.view_messages else "restricted", + until_date=0 if rights.until_date == (1 << 31) - 1 else rights.until_date, + client=client + ) + + if chat_member.status == "restricted": + chat_member.can_send_messages = not rights.send_messages + chat_member.can_send_media_messages = not rights.send_media + chat_member.can_send_other_messages = ( + not rights.send_stickers or not rights.send_gifs or + not rights.send_games or not rights.send_inline + ) + chat_member.can_add_web_page_previews = not rights.embed_links + + return chat_member diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index 622f8abc..e58a4168 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -16,10 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from pyrogram.api import types +from ..pyrogram_type import PyrogramType +from ..user_and_chats import ChatMember, User -class ChatMembers(Object): +class ChatMembers(PyrogramType): """This object contains information about the members list of a chat. Args: @@ -30,8 +32,29 @@ class ChatMembers(Object): Requested chat members. """ - ID = 0xb0700030 - - def __init__(self, total_count: int, chat_members: list): + def __init__(self, *, total_count: int, chat_members: list, client=None): self.total_count = total_count self.chat_members = chat_members + + self.client = client + + @staticmethod + def parse(client, members, users: dict): + if isinstance(members, types.channels.ChannelParticipants): + total_count = members.count + members = members.participants + else: + members = members.full_chat.participants.participants + total_count = len(members) + + chat_members = [] + + for member in members: + user = User.parse(client, users[member.user_id]) + chat_members.append(ChatMember.parse(client, member, user)) + + return ChatMembers( + total_count=total_count, + chat_members=chat_members, + client=client + ) From cffafa8c611db20469d81c641bf1eb18d43811b3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:10:08 +0100 Subject: [PATCH 19/62] Small fixed to PyrogramType --- pyrogram/client/types/pyrogram_type.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index 20b476a2..ee0cfea4 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -24,6 +24,9 @@ class PyrogramType: def __str__(self): return dumps(self, cls=Encoder, indent=4) + def __getitem__(self, item): + return getattr(self, item) + def remove_none(obj): if isinstance(obj, (list, tuple, set)): @@ -37,8 +40,4 @@ def remove_none(obj): class Encoder(JSONEncoder): def default(self, o: PyrogramType): content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} - - return OrderedDict( - [("_", "pyrogram:{}".format(o.__class__.__name__))] - + [i for i in remove_none(content).items()] - ) + return remove_none(OrderedDict([("_", "pyrogram:" + o.__class__.__name__)] + [i for i in content.items()])) From 66cd896a998b8748e140d00162c0819bccf20d89 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:10:55 +0100 Subject: [PATCH 20/62] Allow passing raw --- .../client/types/user_and_chats/chat_member.py | 15 ++++++++------- .../client/types/user_and_chats/chat_members.py | 17 ++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index b05630da..b3e2916b 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -84,7 +84,7 @@ class ChatMember(PyrogramType): can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None, can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None, can_send_media_messages: bool = None, can_send_other_messages: bool = None, - can_add_web_page_previews: bool = None, client=None): + can_add_web_page_previews: bool = None, client, raw): self.user = user self.status = status self.until_date = until_date @@ -102,18 +102,19 @@ class ChatMember(PyrogramType): self.can_send_other_messages = can_send_other_messages self.can_add_web_page_previews = can_add_web_page_previews - self.client = client + self._client = client + self._raw = raw @staticmethod def parse(client, member, user) -> "ChatMember": if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): - return ChatMember(user=user, status="member", client=client) + return ChatMember(user=user, status="member", client=client, raw=member) if isinstance(member, (types.ChannelParticipantCreator, types.ChatParticipantCreator)): - return ChatMember(user=user, status="creator", client=client) + return ChatMember(user=user, status="creator", client=client, raw=member) if isinstance(member, types.ChatParticipantAdmin): - return ChatMember(user=user, status="administrator", client=client) + return ChatMember(user=user, status="administrator", client=client, raw=member) if isinstance(member, types.ChannelParticipantAdmin): rights = member.admin_rights @@ -130,7 +131,7 @@ class ChatMember(PyrogramType): can_restrict_members=rights.ban_users, can_pin_messages=rights.pin_messages, can_promote_members=rights.add_admins, - client=client + client=client, raw=member ) if isinstance(member, types.ChannelParticipantBanned): @@ -140,7 +141,7 @@ class ChatMember(PyrogramType): user=user, status="kicked" if rights.view_messages else "restricted", until_date=0 if rights.until_date == (1 << 31) - 1 else rights.until_date, - client=client + client=client, raw=member ) if chat_member.status == "restricted": diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index e58a4168..4f057309 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -17,8 +17,9 @@ # along with Pyrogram. If not, see . from pyrogram.api import types +from .chat_member import ChatMember +from .user import User from ..pyrogram_type import PyrogramType -from ..user_and_chats import ChatMember, User class ChatMembers(PyrogramType): @@ -32,14 +33,18 @@ class ChatMembers(PyrogramType): Requested chat members. """ - def __init__(self, *, total_count: int, chat_members: list, client=None): + def __init__(self, *, total_count: int, chat_members: list, client, raw): self.total_count = total_count self.chat_members = chat_members - self.client = client + self._client = client + self._raw = raw @staticmethod - def parse(client, members, users: dict): + def parse(client, members): + users = {i.id: i for i in members.users} + chat_members = [] + if isinstance(members, types.channels.ChannelParticipants): total_count = members.count members = members.participants @@ -47,8 +52,6 @@ class ChatMembers(PyrogramType): members = members.full_chat.participants.participants total_count = len(members) - chat_members = [] - for member in members: user = User.parse(client, users[member.user_id]) chat_members.append(ChatMember.parse(client, member, user)) @@ -56,5 +59,5 @@ class ChatMembers(PyrogramType): return ChatMembers( total_count=total_count, chat_members=chat_members, - client=client + client=client, raw=members ) From 31b046e5cc436fa223f604bae004997c1fe582f8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:58:05 +0100 Subject: [PATCH 21/62] Use the refactored types in get_chat_member(s) --- pyrogram/client/methods/chats/get_chat_member.py | 8 +++++--- pyrogram/client/methods/chats/get_chat_members.py | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/methods/chats/get_chat_member.py b/pyrogram/client/methods/chats/get_chat_member.py index b9b23e54..b5e7cec7 100644 --- a/pyrogram/client/methods/chats/get_chat_member.py +++ b/pyrogram/client/methods/chats/get_chat_member.py @@ -17,7 +17,8 @@ # along with Pyrogram. If not, see . from pyrogram.api import functions, types, errors -from ...ext import BaseClient, utils +from ...ext import BaseClient +import pyrogram class GetChatMember(BaseClient): @@ -51,7 +52,7 @@ class GetChatMember(BaseClient): ) ) - for member in utils.parse_chat_members(full_chat).chat_members: + for member in pyrogram.ChatMembers.parse(self, full_chat).chat_members: if member.user.id == user_id.user_id: return member else: @@ -64,7 +65,8 @@ class GetChatMember(BaseClient): ) ) - return utils.parse_chat_members( + return pyrogram.ChatMembers.parse( + self, types.channels.ChannelParticipants( count=1, participants=[r.participant], diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index 5952a39d..df1ca8b1 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -18,6 +18,7 @@ from pyrogram.api import functions, types from ...ext import BaseClient, utils +import pyrogram class Filters: @@ -83,7 +84,8 @@ class GetChatMembers(BaseClient): peer = self.resolve_peer(chat_id) if isinstance(peer, types.InputPeerChat): - return utils.parse_chat_members( + return pyrogram.ChatMembers.parse( + self, self.send( functions.messages.GetFullChat( peer.chat_id @@ -108,7 +110,8 @@ class GetChatMembers(BaseClient): else: raise ValueError("Invalid filter \"{}\"".format(filter)) - return utils.parse_chat_members( + return pyrogram.ChatMembers.parse( + self, self.send( functions.channels.GetParticipants( channel=peer, From 5d64de10dc661aebac245c4bb9a64e88f36480fd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:58:32 +0100 Subject: [PATCH 22/62] Add Chat.parse_full --- pyrogram/client/types/user_and_chats/chat.py | 48 +++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 6e1c675e..2169f1d7 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -112,8 +112,7 @@ class Chat(PyrogramType): last_name=user.last_name, photo=ChatPhoto.parse(client, user.photo), restriction_reason=user.restriction_reason, - client=client, - raw=user + client=client, raw=user ) @staticmethod @@ -129,8 +128,7 @@ class Chat(PyrogramType): title=chat.title, all_members_are_administrators=admins_enabled, photo=ChatPhoto.parse(client, getattr(chat, "photo", None)), - client=client, - raw=chat + client=client, raw=chat ) @staticmethod @@ -142,8 +140,7 @@ class Chat(PyrogramType): username=getattr(channel, "username", None), photo=ChatPhoto.parse(client, getattr(channel, "photo", None)), restriction_reason=getattr(channel, "restriction_reason", None), - client=client, - raw=channel + client=client, raw=channel ) @staticmethod @@ -152,6 +149,43 @@ class Chat(PyrogramType): return Chat.parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id]) if isinstance(message.to_id, types.PeerChat): - return Chat.parse_chat_chat(chats[message.to_id.chat_id]) + return Chat.parse_chat_chat(client, chats[message.to_id.chat_id]) return Chat.parse_channel_chat(client, chats[message.to_id.channel_id]) + + @staticmethod + def parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat": + if isinstance(chat_full, types.UserFull): + parsed_chat = Chat.parse_user_chat(client, chat_full.user) + parsed_chat.description = chat_full.about + else: + full_chat = chat_full.full_chat + chat = None + + for i in chat_full.chats: + if full_chat.id == i.id: + chat = i + + if isinstance(full_chat, types.ChatFull): + parsed_chat = Chat.parse_chat_chat(client, chat) + + if isinstance(full_chat.participants, types.ChatParticipants): + parsed_chat.members_count = len(full_chat.participants.participants) + else: + parsed_chat = Chat.parse_channel_chat(client, chat) + parsed_chat.members_count = full_chat.participants_count + parsed_chat.description = full_chat.about or None + # TODO: Add StickerSet type + parsed_chat.can_set_sticker_set = full_chat.can_set_stickers + parsed_chat.sticker_set_name = full_chat.stickerset + + if full_chat.pinned_msg_id: + parsed_chat.pinned_message = client.get_messages( + parsed_chat.id, + message_ids=full_chat.pinned_msg_id + ) + + if isinstance(full_chat.exported_invite, types.ChatInviteExported): + parsed_chat.invite_link = full_chat.exported_invite.link + + return parsed_chat From 7430529646164e09d84ddff862ffc92af86a6692 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:59:34 +0100 Subject: [PATCH 23/62] Make use of the refactored Chat type in get_chat --- pyrogram/client/methods/chats/get_chat.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/methods/chats/get_chat.py b/pyrogram/client/methods/chats/get_chat.py index 8e41695a..7c191bf3 100644 --- a/pyrogram/client/methods/chats/get_chat.py +++ b/pyrogram/client/methods/chats/get_chat.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetChat(BaseClient): @@ -44,4 +45,4 @@ class GetChat(BaseClient): else: r = self.send(functions.messages.GetFullChat(peer.chat_id)) - return utils.parse_chat_full(self, r) + return pyrogram.Chat.parse_full(self, r) From c6a0bf0791697b08b193d2664630ad45be72a4fa Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 22:34:23 +0100 Subject: [PATCH 24/62] Force named arguments on Pyrogram types --- .../types/messages_and_media/animation.py | 10 +++--- .../client/types/messages_and_media/audio.py | 12 +++---- .../types/messages_and_media/contact.py | 10 +++--- .../types/messages_and_media/document.py | 10 +++--- .../types/messages_and_media/location.py | 8 ++--- .../types/messages_and_media/message.py | 31 +++++++++---------- .../messages_and_media/message_entity.py | 13 ++++---- .../types/messages_and_media/messages.py | 4 ++- .../client/types/messages_and_media/photo.py | 8 ++--- .../types/messages_and_media/photo_size.py | 8 ++--- .../types/messages_and_media/sticker.py | 12 +++---- .../messages_and_media/user_profile_photos.py | 4 ++- .../client/types/messages_and_media/venue.py | 10 +++--- .../client/types/messages_and_media/video.py | 10 +++--- .../types/messages_and_media/video_note.py | 10 +++--- .../client/types/messages_and_media/voice.py | 10 +++--- pyrogram/client/types/pyrogram_type.py | 4 +++ pyrogram/client/types/user_and_chats/chat.py | 16 ++++------ .../types/user_and_chats/chat_member.py | 9 +++--- .../types/user_and_chats/chat_members.py | 10 +++--- .../client/types/user_and_chats/chat_photo.py | 6 +--- .../client/types/user_and_chats/dialog.py | 18 +++++------ .../client/types/user_and_chats/dialogs.py | 9 +++--- pyrogram/client/types/user_and_chats/user.py | 18 +++++------ .../types/user_and_chats/user_status.py | 13 +++----- 25 files changed, 118 insertions(+), 155 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 7447f4ab..c270dfa9 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -56,9 +56,10 @@ class Animation(PyrogramType): Date the animation was sent in Unix time. """ - def __init__(self, file_id: str, width: int, height: int, duration: int, *, - thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, width: int, height: int, duration: int, thumb=None, + file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -69,9 +70,6 @@ class Animation(PyrogramType): self.height = height self.duration = duration - self._client = client - self._raw = raw - @staticmethod def parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Animation": diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 2cd3a1bd..bc5cf166 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -56,10 +56,11 @@ class Audio(PyrogramType): Title of the audio as defined by sender or by audio tags. """ - def __init__(self, file_id: str, duration: int, *, - thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, - performer: str = None, title: str = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, duration: int, thumb=None, file_name: str = None, + mime_type: str = None, file_size: int = None, date: int = None, performer: str = None, + title: str = None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -70,9 +71,6 @@ class Audio(PyrogramType): self.performer = performer self.title = title - self._client = client - self._raw = raw - @staticmethod def parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio": return Audio( diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 2a3f8a21..0525c463 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -40,18 +40,16 @@ class Contact(PyrogramType): Additional data about the contact in the form of a vCard. """ - def __init__(self, phone_number: str, first_name: str, *, - last_name: str = None, user_id: int = None, vcard: str = None, - client=None, raw=None): + def __init__(self, *, client, raw, phone_number: str, first_name: str, last_name: str = None, user_id: int = None, + vcard: str = None): + super().__init__(client, raw) + self.phone_number = phone_number self.first_name = first_name self.last_name = last_name self.user_id = user_id self.vcard = vcard - self._client = client - self._raw = raw - @staticmethod def parse(client, contact: types.MessageMediaContact) -> "Contact": return Contact( diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 412954ef..0526a190 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -47,9 +47,10 @@ class Document(PyrogramType): Date the document was sent in Unix time. """ - def __init__(self, file_id: str, *, - thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, thumb=None, file_name: str = None, mime_type: str = None, + file_size: int = None, date: int = None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -57,9 +58,6 @@ class Document(PyrogramType): self.file_size = file_size self.date = date - self._client = client - self._raw = raw - @staticmethod def parse(client, document: types.Document, file_name: str) -> "Document": return Document( diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 1276a783..853a4c90 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -31,14 +31,12 @@ class Location(PyrogramType): Latitude as defined by sender. """ - def __init__(self, longitude: float, latitude: float, *, - client=None, raw=None): + def __init__(self, *, client, raw, longitude: float, latitude: float, ): + super().__init__(client, raw) + self.longitude = longitude self.latitude = latitude - self._client = client - self._raw = raw - @staticmethod def parse(client, geo_point: types.GeoPoint) -> "Location": if isinstance(geo_point, types.GeoPoint): diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 8d84a513..f50dd30b 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -225,20 +225,20 @@ class Message(PyrogramType): # TODO: Add game missing field. Also invoice, successful_payment, connected_website - def __init__(self, message_id: int, *, - date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, - forward_from_message_id: int = None, forward_signature: str = None, forward_date: int = None, - reply_to_message=None, mentioned=None, empty=None, service=None, media=None, edit_date: int = None, - media_group_id: str = None, author_signature: str = None, text: str = None, entities: list = None, - caption_entities: list = None, audio=None, document=None, photo=None, sticker=None, animation=None, - video=None, voice=None, video_note=None, caption: str = None, contact=None, location=None, venue=None, - web_page=None, new_chat_members: list = None, left_chat_member=None, new_chat_title: str = None, - new_chat_photo=None, delete_chat_photo: bool = None, group_chat_created: bool = None, - supergroup_chat_created: bool = None, channel_chat_created: bool = None, - migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, pinned_message=None, - views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, command: list = None, - reply_markup=None, - client=None, raw=None): + def __init__(self, *, client, raw, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, + forward_from_chat=None, forward_from_message_id: int = None, forward_signature: str = None, + forward_date: int = None, reply_to_message=None, mentioned=None, empty=None, service=None, media=None, + edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, + entities: list = None, caption_entities: list = None, audio=None, document=None, photo=None, + sticker=None, animation=None, video=None, voice=None, video_note=None, caption: str = None, + contact=None, location=None, venue=None, web_page=None, new_chat_members: list = None, + left_chat_member=None, new_chat_title: str = None, new_chat_photo=None, delete_chat_photo: bool = None, + group_chat_created: bool = None, supergroup_chat_created: bool = None, + channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, + pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, + command: list = None, reply_markup=None): + super().__init__(client, raw) + self.message_id = message_id self.date = date self.chat = chat @@ -290,9 +290,6 @@ class Message(PyrogramType): self.command = command self.reply_markup = reply_markup - self._client = client - self._raw = raw - @staticmethod def parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, replies: int = 1): diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 67f32806..c72df95d 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -61,18 +61,15 @@ class MessageEntity(PyrogramType): types.MessageEntityPhone.ID: "phone_number" } - def __init__(self, type: str, offset: int, length: int, *, - url: str = None, user=None, - client=None, raw=None): + def __init__(self, *, client, raw, type: str, offset: int, length: int, url: str = None, user=None): + super().__init__(client, raw) + self.type = type self.offset = offset self.length = length self.url = url self.user = user - self._client = client - self._raw = raw - @staticmethod def parse(client, entity, users: dict) -> "MessageEntity" or None: type = MessageEntity.ENTITIES.get(entity.ID, None) @@ -85,5 +82,7 @@ class MessageEntity(PyrogramType): offset=entity.offset, length=entity.length, url=getattr(entity, "url", None), - user=User.parse(client, users.get(getattr(entity, "user_id", None), None)) + user=User.parse(client, users.get(getattr(entity, "user_id", None), None)), + client=client, + raw=entity ) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index ef32f0b2..48907940 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -30,6 +30,8 @@ class Messages(PyrogramType): Requested messages. """ - def __init__(self, total_count: int, messages: list): + def __init__(self, *, client, raw, total_count: int, messages: list): + super().__init__(client, raw) + self.total_count = total_count self.messages = messages diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 970579af..96a1e19a 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -39,15 +39,13 @@ class Photo(PyrogramType): Available sizes of this photo. """ - def __init__(self, id: str, date: int, sizes: list, *, - client=None, raw=None): + def __init__(self, *, client, raw, id: str, date: int, sizes: list): + super().__init__(client, raw) + self.id = id self.date = date self.sizes = sizes - self._client = client - self._raw = raw - @staticmethod def parse(client, photo: types.Photo): if isinstance(photo, types.Photo): diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index cb94a6e6..1e37399c 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -40,16 +40,14 @@ class PhotoSize(PyrogramType): File size. """ - def __init__(self, file_id: str, width: int, height: int, file_size: int, *, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, width: int, height: int, file_size: int): + super().__init__(client, raw) + self.file_id = file_id self.width = width self.height = height self.file_size = file_size - self._client = client - self._raw = raw - @staticmethod def parse(client, photo_size: types.PhotoSize or types.PhotoCachedSize): if isinstance(photo_size, (types.PhotoSize, types.PhotoCachedSize)): diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index 7b5bfa70..e6285895 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -61,10 +61,11 @@ class Sticker(PyrogramType): # TODO: Add mask position - def __init__(self, file_id: str, width: int, height: int, *, - thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, - emoji: str = None, set_name: str = None, mask_position=None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, width: int, height: int, thumb=None, file_name: str = None, + mime_type: str = None, file_size: int = None, date: int = None, emoji: str = None, + set_name: str = None, mask_position=None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -77,9 +78,6 @@ class Sticker(PyrogramType): self.set_name = set_name self.mask_position = mask_position - self._client = client - self._raw = raw - @staticmethod def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, set_name: str, sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index bf53b13e..8133cae4 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -30,6 +30,8 @@ class UserProfilePhotos(PyrogramType): Requested profile pictures. """ - def __init__(self, total_count: int, photos: list): + def __init__(self, *, client, raw, total_count: int, photos: list): + super().__init__(client, raw) + self.total_count = total_count self.photos = photos diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 662a416f..ee533d59 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -43,18 +43,16 @@ class Venue(PyrogramType): """ - def __init__(self, location, title: str, address: str, *, - foursquare_id: str = None, foursquare_type: str = None, - client=None, raw=None): + def __init__(self, *, client, raw, location, title: str, address: str, foursquare_id: str = None, + foursquare_type: str = None): + super().__init__(client, raw) + self.location = location self.title = title self.address = address self.foursquare_id = foursquare_id self.foursquare_type = foursquare_type - self._client = client - self._raw = raw - @staticmethod def parse(client, venue: types.MessageMediaVenue): return Venue( diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 05d28f45..5115e651 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -56,9 +56,10 @@ class Video(PyrogramType): Date the video was sent in Unix time. """ - def __init__(self, file_id: str, width: int, height: int, duration: int, *, - thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, width: int, height: int, duration: int, thumb=None, + file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.file_name = file_name @@ -69,9 +70,6 @@ class Video(PyrogramType): self.height = height self.duration = duration - self._client = client - self._raw = raw - @staticmethod def parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video": return Video( diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index ddcd74f3..61a86454 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -50,9 +50,10 @@ class VideoNote(PyrogramType): Date the video note was sent in Unix time. """ - def __init__(self, file_id: str, length: int, duration: int, *, - thumb=None, mime_type: str = None, file_size: int = None, date: int = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, length: int, duration: int, thumb=None, mime_type: str = None, + file_size: int = None, date: int = None): + super().__init__(client, raw) + self.file_id = file_id self.thumb = thumb self.mime_type = mime_type @@ -61,9 +62,6 @@ class VideoNote(PyrogramType): self.length = length self.duration = duration - self._client = client - self._raw = raw - @staticmethod def parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote": return VideoNote( diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index c993ea5c..88cc269e 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -46,9 +46,10 @@ class Voice(PyrogramType): Date the voice was sent in Unix time. """ - def __init__(self, file_id: str, duration: int, *, - waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None, - client=None, raw=None): + def __init__(self, *, client, raw, file_id: str, duration: int, waveform: bytes = None, mime_type: str = None, + file_size: int = None, date: int = None): + super().__init__(client, raw) + self.file_id = file_id self.duration = duration self.waveform = waveform @@ -56,9 +57,6 @@ class Voice(PyrogramType): self.file_size = file_size self.date = date - self._client = client - self._raw = raw - @staticmethod def parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice": return Voice( diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index ee0cfea4..f438a4ba 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -21,6 +21,10 @@ from json import dumps, JSONEncoder class PyrogramType: + def __init__(self, client, raw): + self._client = client + self._raw = raw + def __str__(self): return dumps(self, cls=Encoder, indent=4) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 2169f1d7..d98d57aa 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -76,15 +76,14 @@ class Chat(PyrogramType): The reason why this chat might be unavailable to some users. """ - def __init__(self, id: int, type: str, *, - title: str = None, username: str = None, first_name: str = None, last_name: str = None, - all_members_are_administrators: bool = None, photo=None, description: str = None, - invite_link: str = None, pinned_message=None, sticker_set_name: str = None, - can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None, - client=None, raw=None): + def __init__(self, *, client, raw, id: int, type: str, title: str = None, username: str = None, + first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, photo=None, + description: str = None, invite_link: str = None, pinned_message=None, sticker_set_name: str = None, + can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None): + super().__init__(client, raw) + self.id = id self.type = type - self.title = title self.username = username self.first_name = first_name @@ -99,9 +98,6 @@ class Chat(PyrogramType): self.members_count = members_count self.restriction_reason = restriction_reason - self._client = client - self._raw = raw - @staticmethod def parse_user_chat(client, user: types.User) -> "Chat": return Chat( diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index b3e2916b..11b2c9a0 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -79,12 +79,14 @@ class ChatMember(PyrogramType): Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages. """ - def __init__(self, *, user, status: str, until_date: int = None, can_be_edited: bool = None, + def __init__(self, *, client, raw, user, status: str, until_date: int = None, can_be_edited: bool = None, can_change_info: bool = None, can_post_messages: bool = None, can_edit_messages: bool = None, can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None, can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None, can_send_media_messages: bool = None, can_send_other_messages: bool = None, - can_add_web_page_previews: bool = None, client, raw): + can_add_web_page_previews: bool = None): + super().__init__(client, raw) + self.user = user self.status = status self.until_date = until_date @@ -102,9 +104,6 @@ class ChatMember(PyrogramType): self.can_send_other_messages = can_send_other_messages self.can_add_web_page_previews = can_add_web_page_previews - self._client = client - self._raw = raw - @staticmethod def parse(client, member, user) -> "ChatMember": if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index 4f057309..5fa8275d 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -33,13 +33,12 @@ class ChatMembers(PyrogramType): Requested chat members. """ - def __init__(self, *, total_count: int, chat_members: list, client, raw): + def __init__(self, *, client, raw, total_count: int, chat_members: list): + super().__init__(client, raw) + self.total_count = total_count self.chat_members = chat_members - self._client = client - self._raw = raw - @staticmethod def parse(client, members): users = {i.id: i for i in members.users} @@ -59,5 +58,6 @@ class ChatMembers(PyrogramType): return ChatMembers( total_count=total_count, chat_members=chat_members, - client=client, raw=members + client=client, + raw=members ) diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index c7ef4c80..b998cf90 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -36,14 +36,10 @@ class ChatPhoto(Object): ID = 0xb0700015 - def __init__(self, small_file_id: str, big_file_id: str, *, - client=None, raw=None): + def __init__(self, *, client, raw, small_file_id: str, big_file_id: str): self.small_file_id = small_file_id self.big_file_id = big_file_id - self._client = client - self._raw = raw - @staticmethod def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)): diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 8107d363..3d4d0faf 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Dialog(Object): +class Dialog(PyrogramType): """This object represents a dialog. Args: @@ -41,18 +41,14 @@ class Dialog(Object): is_pinned (``bool``): True, if the dialog is pinned. """ - ID = 0xb0700028 - def __init__(self, - chat, - top_message, - unread_messages_count: int, - unread_mentions_count: int, - unread_mark: bool, - is_pinned: bool): + def __init__(self, *, client, raw, chat, top_message, unread_messages_count: int, unread_mentions_count: int, + unread_mark: bool, is_pinned: bool): + super().__init__(client, raw) + self.chat = chat self.top_message = top_message self.unread_messages_count = unread_messages_count self.unread_mentions_count = unread_mentions_count self.unread_mark = unread_mark - self.is_pinned = is_pinned + self.is_pinned = is_pinned \ No newline at end of file diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index cdf1d951..030018e4 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Dialogs(Object): +class Dialogs(PyrogramType): """This object represents a user's dialogs chunk Args: @@ -29,8 +29,9 @@ class Dialogs(Object): dialogs (List of :obj:`Dialog `): Requested dialogs. """ - ID = 0xb0700029 - def __init__(self, total_count: int, dialogs: list): + def __init__(self, *, client, raw, total_count: int, dialogs: list): + super().__init__(client, raw) + self.total_count = total_count self.dialogs = dialogs diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 80660bcc..1c1d41fe 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -69,11 +69,12 @@ class User(PyrogramType): The reason why this bot might be unavailable to some users. """ - def __init__(self, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool, - is_bot: bool, first_name: str, *, - last_name: str = None, status=None, username: str = None, language_code: str = None, - phone_number: str = None, photo=None, restriction_reason: str = None, - client=None, raw=None): + def __init__(self, *, client, raw, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, + is_deleted: bool, is_bot: bool, first_name: str, last_name: str = None, status=None, + username: str = None, language_code: str = None, phone_number: str = None, photo=None, + restriction_reason: str = None): + super().__init__(client, raw) + self.id = id self.is_self = is_self self.is_contact = is_contact @@ -81,7 +82,6 @@ class User(PyrogramType): self.is_deleted = is_deleted self.is_bot = is_bot self.first_name = first_name - self.last_name = last_name self.status = status self.username = username @@ -90,9 +90,6 @@ class User(PyrogramType): self.photo = photo self.restriction_reason = restriction_reason - self._client = client - self._raw = raw - @staticmethod def parse(client, user: types.User) -> "User" or None: if user is None: @@ -113,5 +110,6 @@ class User(PyrogramType): phone_number=user.phone, photo=ChatPhoto.parse(client, user.photo), restriction_reason=user.restriction_reason, - client=client, raw=user + client=client, + raw=user ) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index f99d356d..d6bfa13d 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -62,12 +62,12 @@ class UserStatus(PyrogramType): always shown to blocked users), None otherwise. """ - def __init__(self, user_id: int, *, - online: bool = None, offline: bool = None, date: int = None, recently: bool = None, - within_week: bool = None, within_month: bool = None, long_time_ago: bool = None, - client=None, raw=None): - self.user_id = user_id + def __init__(self, *, client, raw, user_id: int, online: bool = None, offline: bool = None, date: int = None, + recently: bool = None, within_week: bool = None, within_month: bool = None, + long_time_ago: bool = None): + super().__init__(client, raw) + self.user_id = user_id self.online = online self.offline = offline self.date = date @@ -76,9 +76,6 @@ class UserStatus(PyrogramType): self.within_month = within_month self.long_time_ago = long_time_ago - self._client = client - self._raw = raw - @staticmethod def parse(client, user: types.User): if user.bot: From ac8258f4513a59c6501baf0eae01e1bbea3fb386 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 12:40:32 +0100 Subject: [PATCH 25/62] Move Str into message.py --- .../types/messages_and_media/message.py | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index f50dd30b..41766332 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -26,7 +26,6 @@ from ..messages_and_media.photo import Photo from ..pyrogram_type import PyrogramType from ..user_and_chats.chat import Chat from ..user_and_chats.user import User -from ...ext.utils import Str class Message(PyrogramType): @@ -484,8 +483,8 @@ class Message(PyrogramType): date=message.date, chat=Chat.parse(client, message, users, chats), from_user=User.parse(client, users.get(message.from_id, None)), - text=Str(message.message) or None if media is None else None, - caption=Str(message.message) or None if media is not None else None, + text=Str(message.message).init(client, entities) or None if media is None else None, + caption=Str(message.message).init(client, entities) or None if media is not None else None, entities=entities or None if media is None else None, caption_entities=entities or None if media is not None else None, author_signature=message.post_author, @@ -900,3 +899,29 @@ class Message(PyrogramType): progress=progress, progress_args=progress_args, ) + + +class Str(str): + def __init__(self, *args): + super().__init__() + + self.client = None + self.entities = None + + def init(self, client, entities): + self.client = client + self.entities = entities + + return self + + @property + def text(self): + return self + + @property + def markdown(self): + return self.client.markdown.unparse(self, self.entities) + + @property + def html(self): + return self.client.html.unparse(self, self.entities) From e9a362923f849bc80ce4dc8cb69efe1c19f56e36 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 12:42:29 +0100 Subject: [PATCH 26/62] Cache stickers --- .../types/messages_and_media/message.py | 31 +++++-------------- .../types/messages_and_media/sticker.py | 26 ++++++++++++++-- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 41766332..66072d22 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -17,8 +17,8 @@ # along with Pyrogram. If not, see . import pyrogram -from pyrogram.api import types, functions -from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid +from pyrogram.api import types +from pyrogram.api.errors import MessageIdsEmpty from .contact import Contact from .location import Location from .message_entity import MessageEntity @@ -442,21 +442,12 @@ class Message(PyrogramType): else: video = pyrogram.Video.parse(client, doc, video_attributes, file_name) elif types.DocumentAttributeSticker in attributes: - image_size_attributes = attributes.get(types.DocumentAttributeImageSize, None) - sticker_attribute = attributes[types.DocumentAttributeSticker] - - if isinstance(sticker_attribute.stickerset, types.InputStickerSetID): - try: - set_name = client.send( - functions.messages.GetStickerSet(sticker_attribute.stickerset) - ).set.short_name - except StickersetInvalid: - set_name = None - else: - set_name = None - - sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes, - set_name, sticker_attribute, file_name) + sticker = pyrogram.Sticker.parse( + client, doc, + attributes.get(types.DocumentAttributeImageSize, None), + attributes[types.DocumentAttributeSticker], + file_name + ) else: document = pyrogram.Document.parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): @@ -517,12 +508,6 @@ class Message(PyrogramType): raw=message ) - if parsed_message.text: - parsed_message.text.init(parsed_message._client, parsed_message.entities or []) - - if parsed_message.caption: - parsed_message.caption.init(parsed_message._client, parsed_message.caption_entities or []) - if message.reply_to_msg_id and replies: try: parsed_message.reply_to_message = client.get_messages( diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index e6285895..f2414311 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -16,9 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from functools import lru_cache from struct import pack -from pyrogram.api import types +from pyrogram.api import types, functions +from pyrogram.api.errors import StickersetInvalid from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType from ...ext.utils import encode @@ -79,8 +81,28 @@ class Sticker(PyrogramType): self.mask_position = mask_position @staticmethod - def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, set_name: str, + @lru_cache(maxsize=256) + def get_sticker_set_name(send, input_sticker_set_id): + try: + return send( + functions.messages.GetStickerSet( + types.InputStickerSetID(*input_sticker_set_id) + ) + ).set.short_name + except StickersetInvalid: + return None + + @staticmethod + def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": + sticker_set = sticker_attributes.stickerset + + if isinstance(sticker_set, types.InputStickerSetID): + input_sticker_set_id = (sticker_set.id, sticker_set.access_hash) + set_name = Sticker.get_sticker_set_name(client.send, input_sticker_set_id) + else: + set_name = None + return Sticker( file_id=encode( pack( From 92118e36088e88e452671d427e52ab0d5b21ccfb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 12:45:35 +0100 Subject: [PATCH 27/62] Reformat __init__ parameters --- .../client/types/input_media/input_media_animation.py | 10 ++-------- .../client/types/input_media/input_media_audio.py | 10 ++-------- .../client/types/input_media/input_media_document.py | 6 +----- .../client/types/input_media/input_media_photo.py | 5 +---- .../client/types/input_media/input_media_video.py | 11 ++--------- 5 files changed, 8 insertions(+), 34 deletions(-) diff --git a/pyrogram/client/types/input_media/input_media_animation.py b/pyrogram/client/types/input_media/input_media_animation.py index 0e7b2433..cc3d9ad0 100644 --- a/pyrogram/client/types/input_media/input_media_animation.py +++ b/pyrogram/client/types/input_media/input_media_animation.py @@ -52,14 +52,8 @@ class InputMediaAnimation(InputMedia): Animation duration. """ - def __init__(self, - media: str, - thumb: str = None, - caption: str = "", - parse_mode: str = "", - width: int = 0, - height: int = 0, - duration: int = 0): + def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", width: int = 0, + height: int = 0, duration: int = 0): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_audio.py b/pyrogram/client/types/input_media/input_media_audio.py index 455c2292..7c1b6072 100644 --- a/pyrogram/client/types/input_media/input_media_audio.py +++ b/pyrogram/client/types/input_media/input_media_audio.py @@ -53,14 +53,8 @@ class InputMediaAudio(InputMedia): Title of the audio """ - def __init__(self, - media: str, - thumb: str = None, - caption: str = "", - parse_mode: str = "", - duration: int = 0, - performer: int = "", - title: str = ""): + def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", duration: int = 0, + performer: int = "", title: str = ""): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_document.py b/pyrogram/client/types/input_media/input_media_document.py index 08fcae5b..267abbde 100644 --- a/pyrogram/client/types/input_media/input_media_document.py +++ b/pyrogram/client/types/input_media/input_media_document.py @@ -43,11 +43,7 @@ class InputMediaDocument(InputMedia): Defaults to Markdown. """ - def __init__(self, - media: str, - thumb: str = None, - caption: str = "", - parse_mode: str = ""): + def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = ""): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_photo.py b/pyrogram/client/types/input_media/input_media_photo.py index c8cdccb8..f5af9c89 100644 --- a/pyrogram/client/types/input_media/input_media_photo.py +++ b/pyrogram/client/types/input_media/input_media_photo.py @@ -39,8 +39,5 @@ class InputMediaPhoto(InputMedia): Defaults to Markdown. """ - def __init__(self, - media: str, - caption: str = "", - parse_mode: str = ""): + def __init__(self, media: str, caption: str = "", parse_mode: str = ""): super().__init__(media, caption, parse_mode) diff --git a/pyrogram/client/types/input_media/input_media_video.py b/pyrogram/client/types/input_media/input_media_video.py index 955cf633..1884ea8d 100644 --- a/pyrogram/client/types/input_media/input_media_video.py +++ b/pyrogram/client/types/input_media/input_media_video.py @@ -57,15 +57,8 @@ class InputMediaVideo(InputMedia): Pass True, if the uploaded video is suitable for streaming. """ - def __init__(self, - media: str, - thumb: str = None, - caption: str = "", - parse_mode: str = "", - width: int = 0, - height: int = 0, - duration: int = 0, - supports_streaming: bool = True): + def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", width: int = 0, + height: int = 0, duration: int = 0, supports_streaming: bool = True): super().__init__(media, caption, parse_mode) self.thumb = thumb From 52b9319734021f3d95df6502d166178c5c50453b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:01:41 +0100 Subject: [PATCH 28/62] Refactor CallbackQuery --- pyrogram/client/types/bots/callback_query.py | 79 ++++++++++++++------ 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index 046981b7..61ece31e 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -16,10 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from base64 import b64encode +from struct import pack + +from pyrogram.api import types +from ..pyrogram_type import PyrogramType +from ..user_and_chats import User -class CallbackQuery(Object): +class CallbackQuery(PyrogramType): """This object represents an incoming callback query from a callback button in an inline keyboard. If the button that originated the query was attached to a message sent by the bot, the field message will be present. If the button was attached to a message sent via the bot (in inline mode), @@ -50,27 +55,57 @@ class CallbackQuery(Object): Short name of a Game to be returned, serves as the unique identifier for the game. """ - ID = 0xb0700024 - def __init__( - self, - id: str, - from_user, - chat_instance: str, - client=None, - message=None, - inline_message_id: str = None, - data: bytes = None, - game_short_name: str = None - ): - self._client = client - self.id = id # string - self.from_user = from_user # User - self.message = message # flags.0?Message - self.inline_message_id = inline_message_id # flags.1?string - self.chat_instance = chat_instance # string - self.data = data # flags.2?string - self.game_short_name = game_short_name # flags.3?string + def __init__(self, *, client, raw, id: str, from_user, chat_instance: str, message=None, + inline_message_id: str = None, data: bytes = None, game_short_name: str = None): + super().__init__(client, raw) + + self.id = id + self.from_user = from_user + self.message = message + self.inline_message_id = inline_message_id + self.chat_instance = chat_instance + self.data = data + self.game_short_name = game_short_name + + @staticmethod + def parse(client, callback_query, users) -> "CallbackQuery": + message = None + inline_message_id = None + + if isinstance(callback_query, types.UpdateBotCallbackQuery): + peer = callback_query.peer + + if isinstance(peer, types.PeerUser): + peer_id = peer.user_id + elif isinstance(peer, types.PeerChat): + peer_id = -peer.chat_id + else: + peer_id = int("-100" + str(peer.channel_id)) + + message = client.get_messages(peer_id, callback_query.msg_id) + elif isinstance(callback_query, types.UpdateInlineBotCallbackQuery): + inline_message_id = b64encode( + pack( + "`. From d31a696f324b02e606d965590ad02b2e87e5d16f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:02:22 +0100 Subject: [PATCH 29/62] Fix encoder breaking with bytes --- pyrogram/client/types/pyrogram_type.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index f438a4ba..18faa9e8 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -43,5 +43,9 @@ def remove_none(obj): class Encoder(JSONEncoder): def default(self, o: PyrogramType): - content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} + try: + content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} + except AttributeError: + return repr(o) + return remove_none(OrderedDict([("_", "pyrogram:" + o.__class__.__name__)] + [i for i in content.items()])) From 3f643242b1e3f039c161a6312137532226f102c0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:03:08 +0100 Subject: [PATCH 30/62] Reformat encoder style --- pyrogram/client/types/pyrogram_type.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index 18faa9e8..880c452a 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -44,8 +44,16 @@ def remove_none(obj): class Encoder(JSONEncoder): def default(self, o: PyrogramType): try: - content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} + content = { + i: getattr(o, i) + for i in filter(lambda x: not x.startswith("_"), o.__dict__) + } except AttributeError: return repr(o) - return remove_none(OrderedDict([("_", "pyrogram:" + o.__class__.__name__)] + [i for i in content.items()])) + return remove_none( + OrderedDict( + [("_", "pyrogram:" + o.__class__.__name__)] + + [i for i in content.items()] + ) + ) From a683e3e917dd2f911fdb76ea321b6f07aa38de21 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:16:05 +0100 Subject: [PATCH 31/62] Refactor bot keyboard types --- pyrogram/client/types/bots/force_reply.py | 8 ++++---- .../types/bots/inline_keyboard_button.py | 19 +++++-------------- .../types/bots/inline_keyboard_markup.py | 9 ++++----- pyrogram/client/types/bots/keyboard_button.py | 9 ++++----- .../types/bots/reply_keyboard_markup.py | 17 +++++------------ .../types/bots/reply_keyboard_remove.py | 8 ++++---- 6 files changed, 26 insertions(+), 44 deletions(-) diff --git a/pyrogram/client/types/bots/force_reply.py b/pyrogram/client/types/bots/force_reply.py index 2d3cc98f..6d91ff5f 100644 --- a/pyrogram/client/types/bots/force_reply.py +++ b/pyrogram/client/types/bots/force_reply.py @@ -16,11 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object from pyrogram.api.types import ReplyKeyboardForceReply +from ..pyrogram_type import PyrogramType -class ForceReply(Object): +class ForceReply(PyrogramType): """Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to @@ -33,9 +33,9 @@ class ForceReply(Object): 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. """ - ID = 0xb0700018 - def __init__(self, selective: bool = None): + super().__init__(None, None) + self.selective = selective @staticmethod diff --git a/pyrogram/client/types/bots/inline_keyboard_button.py b/pyrogram/client/types/bots/inline_keyboard_button.py index 3d6c7b6b..34718fd1 100644 --- a/pyrogram/client/types/bots/inline_keyboard_button.py +++ b/pyrogram/client/types/bots/inline_keyboard_button.py @@ -16,15 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object - from pyrogram.api.types import ( KeyboardButtonUrl, KeyboardButtonCallback, KeyboardButtonSwitchInline ) +from ..pyrogram_type import PyrogramType -class InlineKeyboardButton(Object): +class InlineKeyboardButton(PyrogramType): """This object represents one button of an inline keyboard. You must use exactly one of the optional fields. Args: @@ -54,18 +53,10 @@ class InlineKeyboardButton(Object): # TODO: Add callback_game and pay fields - ID = 0xb0700019 + def __init__(self, text: str, callback_data: bytes = None, url: str = None, + switch_inline_query: str = None, switch_inline_query_current_chat: str = None): + super().__init__(None, None) - def __init__( - self, - text: str, - callback_data: bytes = None, - url: str = None, - switch_inline_query: str = None, - switch_inline_query_current_chat: str = None, - # callback_game=None, - # pay: bool = None - ): self.text = text self.url = url self.callback_data = callback_data diff --git a/pyrogram/client/types/bots/inline_keyboard_markup.py b/pyrogram/client/types/bots/inline_keyboard_markup.py index 2a6993c4..bde31ec4 100644 --- a/pyrogram/client/types/bots/inline_keyboard_markup.py +++ b/pyrogram/client/types/bots/inline_keyboard_markup.py @@ -16,13 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object - from pyrogram.api.types import ReplyInlineMarkup, KeyboardButtonRow from . import InlineKeyboardButton +from ..pyrogram_type import PyrogramType -class InlineKeyboardMarkup(Object): +class InlineKeyboardMarkup(PyrogramType): """This object represents an inline keyboard that appears right next to the message it belongs to. Args: @@ -30,9 +29,9 @@ class InlineKeyboardMarkup(Object): List of button rows, each represented by a List of InlineKeyboardButton objects. """ - ID = 0xb0700020 - def __init__(self, inline_keyboard: list): + super().__init__(None, None) + self.inline_keyboard = inline_keyboard @staticmethod diff --git a/pyrogram/client/types/bots/keyboard_button.py b/pyrogram/client/types/bots/keyboard_button.py index b9af3b46..0566fc20 100644 --- a/pyrogram/client/types/bots/keyboard_button.py +++ b/pyrogram/client/types/bots/keyboard_button.py @@ -16,13 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object - from pyrogram.api.types import KeyboardButton as RawKeyboardButton from pyrogram.api.types import KeyboardButtonRequestPhone, KeyboardButtonRequestGeoLocation +from ..pyrogram_type import PyrogramType -class KeyboardButton(Object): +class KeyboardButton(PyrogramType): """This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields are mutually exclusive. @@ -41,9 +40,9 @@ class KeyboardButton(Object): Available in private chats only. """ - ID = 0xb0700021 - def __init__(self, text: str, request_contact: bool = None, request_location: bool = None): + super().__init__(None, None) + self.text = text self.request_contact = request_contact self.request_location = request_location diff --git a/pyrogram/client/types/bots/reply_keyboard_markup.py b/pyrogram/client/types/bots/reply_keyboard_markup.py index 29fc1081..77b72562 100644 --- a/pyrogram/client/types/bots/reply_keyboard_markup.py +++ b/pyrogram/client/types/bots/reply_keyboard_markup.py @@ -16,15 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object - from pyrogram.api.types import KeyboardButtonRow from pyrogram.api.types import ReplyKeyboardMarkup as RawReplyKeyboardMarkup - from . import KeyboardButton +from ..pyrogram_type import PyrogramType -class ReplyKeyboardMarkup(Object): +class ReplyKeyboardMarkup(PyrogramType): """This object represents a custom keyboard with reply options. Args: @@ -49,15 +47,10 @@ class ReplyKeyboardMarkup(Object): select the new language. Other users in the group don't see the keyboard. """ - ID = 0xb0700022 + def __init__(self, keyboard: list, resize_keyboard: bool = None, one_time_keyboard: bool = None, + selective: bool = None): + super().__init__(None, None) - def __init__( - self, - keyboard: list, - resize_keyboard: bool = None, - one_time_keyboard: bool = None, - selective: bool = None - ): self.keyboard = keyboard self.resize_keyboard = resize_keyboard self.one_time_keyboard = one_time_keyboard diff --git a/pyrogram/client/types/bots/reply_keyboard_remove.py b/pyrogram/client/types/bots/reply_keyboard_remove.py index 3e2aebf5..82adb859 100644 --- a/pyrogram/client/types/bots/reply_keyboard_remove.py +++ b/pyrogram/client/types/bots/reply_keyboard_remove.py @@ -16,11 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object from pyrogram.api.types import ReplyKeyboardHide +from ..pyrogram_type import PyrogramType -class ReplyKeyboardRemove(Object): +class ReplyKeyboardRemove(PyrogramType): """Upon receiving a message with this object, Telegram clients will remove the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a @@ -35,9 +35,9 @@ class ReplyKeyboardRemove(Object): keyboard for that user, while still showing the keyboard with poll options to users who haven't voted yet. """ - ID = 0xb0700023 - def __init__(self, selective: bool = None): + super().__init__(None, None) + self.selective = selective @staticmethod From 8cbb9c93161eafac8f15a2d6a7fe0417edc50202 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 13:51:08 +0100 Subject: [PATCH 32/62] Refactor UserProfilePhotos --- .../client/methods/users/get_user_profile_photos.py | 6 ++++-- .../types/messages_and_media/user_profile_photos.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/methods/users/get_user_profile_photos.py b/pyrogram/client/methods/users/get_user_profile_photos.py index 552ebb9a..1403eb79 100644 --- a/pyrogram/client/methods/users/get_user_profile_photos.py +++ b/pyrogram/client/methods/users/get_user_profile_photos.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetUserProfilePhotos(BaseClient): @@ -47,7 +48,8 @@ class GetUserProfilePhotos(BaseClient): Raises: :class:`Error ` in case of a Telegram RPC error. """ - return utils.parse_profile_photos( + return pyrogram.UserProfilePhotos.parse( + self, self.send( functions.photos.GetUserPhotos( user_id=self.resolve_peer(user_id), diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index 8133cae4..ca277a95 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types +from .photo import Photo from ..pyrogram_type import PyrogramType @@ -35,3 +37,12 @@ class UserProfilePhotos(PyrogramType): self.total_count = total_count self.photos = photos + + @staticmethod + def parse(client, photos) -> "UserProfilePhotos": + return UserProfilePhotos( + total_count=len(photos.photos) if isinstance(photos, types.photos.Photos) else photos.count, + photos=[Photo.parse(client, photo) for photo in photos.photos], + client=client, + raw=photos + ) From 5bc9e0fc75d4c05b78a30903acea32ad320a52ed Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 14:17:57 +0100 Subject: [PATCH 33/62] Fix Video not having client and raw attributes set --- pyrogram/client/types/messages_and_media/video.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 5115e651..9ca54b69 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -89,5 +89,7 @@ class Video(PyrogramType): mime_type=video.mime_type, file_size=video.size, file_name=file_name, - date=video.date + date=video.date, + client=client, + raw=video ) From 7b90a0e7c56cd2566179c751f3f00be4ed0956a1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 14:18:15 +0100 Subject: [PATCH 34/62] Refactor Messages --- .../client/types/messages_and_media/messages.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 48907940..5619b968 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types +from .message import Message from ..pyrogram_type import PyrogramType @@ -35,3 +37,17 @@ class Messages(PyrogramType): self.total_count = total_count self.messages = messages + + @staticmethod + def parse(client, messages: types.messages.Messages) -> "Messages": + users = {i.id: i for i in messages.users} + chats = {i.id: i for i in messages.chats} + + total_count = getattr(messages, "count", len(messages.messages)) + + return Messages( + total_count=total_count, + messages=[Message.parse(client, message, users, chats) for message in messages.messages], + client=client, + raw=messages + ) From 603bc88aa3b80991070ec1ce92e132e04d789518 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 14:18:41 +0100 Subject: [PATCH 35/62] Refactor get_history method --- .../client/methods/messages/get_history.py | 62 +++++-------------- 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index 017c7f8b..bc272ce3 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -18,7 +18,7 @@ import pyrogram from pyrogram.api import functions -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetHistory(BaseClient): @@ -59,52 +59,18 @@ class GetHistory(BaseClient): :class:`Error ` in case of a Telegram RPC error. """ - r = self.send( - functions.messages.GetHistory( - peer=self.resolve_peer(chat_id), - offset_id=offset_id, - offset_date=offset_date, - add_offset=offset, - limit=limit, - max_id=0, - min_id=0, - hash=0 + return 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, + limit=limit, + max_id=0, + min_id=0, + hash=0 + ) ) ) - - users = {i.id: i for i in r.users} - chats = {i.id: i for i in r.chats} - - reply_to_messages = { - i.reply_to_msg_id: None - for i in r.messages - if i.reply_to_msg_id - } - - if reply_to_messages: - temp = self.get_messages( - chat_id, reply_to_messages, - replies=0 - ) - - assert len(temp) == len(reply_to_messages) - - for i in range(len(temp)): - reply_to_messages[temp[i].message_id] = temp[i] - - messages = utils.parse_messages( - self, r.messages, - users, chats, - replies=0 - ) - - assert len(messages) == len(r.messages) - - for i in range(len(messages)): - if r.messages[i].reply_to_msg_id: - messages[i].reply_to_message = reply_to_messages[r.messages[i].reply_to_msg_id] - - return pyrogram.Messages( - total_count=getattr(r, "count", len(r.messages)), - messages=messages - ) From 8c02a1553d1fd740a5e24842efcafb40b98feac8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:12:53 +0100 Subject: [PATCH 36/62] Add Chat.parse_dialog --- pyrogram/client/types/user_and_chats/chat.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index d98d57aa..a9564836 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -149,6 +149,15 @@ class Chat(PyrogramType): return Chat.parse_channel_chat(client, chats[message.to_id.channel_id]) + @staticmethod + def parse_dialog(client, peer, users: dict, chats: dict): + if isinstance(peer, types.PeerUser): + return Chat.parse_user_chat(client, users[peer.user_id]) + elif isinstance(peer, types.PeerChat): + return Chat.parse_chat_chat(client, chats[peer.chat_id]) + else: + return Chat.parse_channel_chat(client, chats[peer.channel_id]) + @staticmethod def parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat": if isinstance(chat_full, types.UserFull): From ccf677f3a026ec2071560e3893ea65c9769976f5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:13:28 +0100 Subject: [PATCH 37/62] Refactor Dialog and Dialogs --- .../client/types/user_and_chats/dialog.py | 26 ++++++++++++++- .../client/types/user_and_chats/dialogs.py | 32 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 3d4d0faf..0fc17fe0 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types from ..pyrogram_type import PyrogramType +from ..user_and_chats import Chat class Dialog(PyrogramType): @@ -51,4 +53,26 @@ class Dialog(PyrogramType): self.unread_messages_count = unread_messages_count self.unread_mentions_count = unread_mentions_count self.unread_mark = unread_mark - self.is_pinned = is_pinned \ No newline at end of file + self.is_pinned = is_pinned + + @staticmethod + def parse(client, dialog, messages, users, chats) -> "Dialog": + chat_id = dialog.peer + + if isinstance(chat_id, types.PeerUser): + chat_id = chat_id.user_id + elif isinstance(chat_id, types.PeerChat): + chat_id = -chat_id.chat_id + else: + chat_id = int("-100" + str(chat_id.channel_id)) + + return Dialog( + chat=Chat.parse_dialog(client, dialog.peer, users, chats), + top_message=messages.get(chat_id), + unread_messages_count=dialog.unread_count, + unread_mentions_count=dialog.unread_mentions_count, + unread_mark=dialog.unread_mark, + is_pinned=dialog.pinned, + client=client, + raw=dialog + ) diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 030018e4..8261e97c 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from pyrogram.api import types +from .dialog import Dialog +from ..messages_and_media import Message from ..pyrogram_type import PyrogramType @@ -35,3 +38,32 @@ class Dialogs(PyrogramType): self.total_count = total_count self.dialogs = dialogs + + @staticmethod + def parse(client, dialogs) -> "Dialogs": + users = {i.id: i for i in dialogs.users} + chats = {i.id: i for i in dialogs.chats} + + messages = {} + + for message in dialogs.messages: + to_id = message.to_id + + if isinstance(to_id, types.PeerUser): + if message.out: + chat_id = to_id.user_id + else: + chat_id = message.from_id + elif isinstance(to_id, types.PeerChat): + chat_id = -to_id.chat_id + else: + chat_id = int("-100" + str(to_id.channel_id)) + + messages[chat_id] = Message.parse(client, message, users, chats) + + return Dialogs( + total_count=getattr(dialogs, "count", len(dialogs.dialogs)), + dialogs=[Dialog.parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs], + client=client, + raw=dialogs + ) From e6dced80cf5619e7ba510964037030649dd91173 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:13:57 +0100 Subject: [PATCH 38/62] Update get_dialogs to use the new refactored Dialogs type --- pyrogram/client/methods/chats/get_dialogs.py | 76 +++----------------- 1 file changed, 9 insertions(+), 67 deletions(-) diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index 045c541a..655bb431 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -18,12 +18,12 @@ import pyrogram from pyrogram.api import functions, types -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetDialogs(BaseClient): def get_dialogs(self, - offset_dialogs=None, + offset_dialog=None, limit: int = 100, pinned_only: bool = False): """Use this method to get the user's dialogs @@ -31,18 +31,18 @@ class GetDialogs(BaseClient): You can get up to 100 dialogs at once. Args: + offset_dialog (:obj:`Dialog`): + Pass the last dialog object to retrieve the next dialogs chunk starting it. + Defaults to None (start from the beginning). + limit (``str``, *optional*): Limits the number of dialogs to be retrieved. - Defaults to 100 + Defaults to 100. pinned_only (``bool``, *optional*): Pass True if you want to get only pinned dialogs. Defaults to False. - offset_dialogs (:obj:`Dialogs`): - Pass the previous dialogs object to retrieve the next dialogs chunk starting from the last dialog. - Defaults to None (start from the beginning). - Returns: On success, a :obj:`Dialogs` object is returned. @@ -53,22 +53,9 @@ class GetDialogs(BaseClient): if pinned_only: r = self.send(functions.messages.GetPinnedDialogs()) else: - offset_date = 0 - - if offset_dialogs: - for dialog in reversed(offset_dialogs.dialogs): - top_message = dialog.top_message - - if top_message: - message_date = top_message.date - - if message_date: - offset_date = message_date - break - r = self.send( functions.messages.GetDialogs( - offset_date=offset_date, + offset_date=offset_dialog.top_message.date if offset_dialog else 0, offset_id=0, offset_peer=types.InputPeerEmpty(), limit=limit, @@ -77,49 +64,4 @@ class GetDialogs(BaseClient): ) ) - users = {i.id: i for i in r.users} - chats = {i.id: i for i in r.chats} - messages = {} - - for message in r.messages: - to_id = message.to_id - - if isinstance(to_id, types.PeerUser): - if message.out: - chat_id = to_id.user_id - else: - chat_id = message.from_id - elif isinstance(to_id, types.PeerChat): - chat_id = -to_id.chat_id - else: - chat_id = int("-100" + str(to_id.channel_id)) - - messages[chat_id] = utils.parse_messages(self, message, users, chats) - - dialogs = [] - - for dialog in r.dialogs: - chat_id = dialog.peer - - if isinstance(chat_id, types.PeerUser): - chat_id = chat_id.user_id - elif isinstance(chat_id, types.PeerChat): - chat_id = -chat_id.chat_id - else: - chat_id = int("-100" + str(chat_id.channel_id)) - - dialogs.append( - pyrogram.Dialog( - chat=utils.parse_dialog_chat(dialog.peer, users, chats), - top_message=messages.get(chat_id), - unread_messages_count=dialog.unread_count, - unread_mentions_count=dialog.unread_mentions_count, - unread_mark=dialog.unread_mark, - is_pinned=dialog.pinned - ) - ) - - return pyrogram.Dialogs( - total_count=getattr(r, "count", len(r.dialogs)), - dialogs=dialogs - ) + return pyrogram.Dialogs.parse(self, r) From 6bc2db7157c3ac24e2ccbef72e2e5e6ee27312c4 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:14:32 +0100 Subject: [PATCH 39/62] Better way of parsing total_count for Messages and UserProfilePhotos --- pyrogram/client/types/messages_and_media/messages.py | 4 +--- .../client/types/messages_and_media/user_profile_photos.py | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 5619b968..cd53c38a 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -43,10 +43,8 @@ class Messages(PyrogramType): users = {i.id: i for i in messages.users} chats = {i.id: i for i in messages.chats} - total_count = getattr(messages, "count", len(messages.messages)) - return Messages( - total_count=total_count, + total_count=getattr(messages, "count", len(messages.messages)), messages=[Message.parse(client, message, users, chats) for message in messages.messages], client=client, raw=messages diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index ca277a95..2196608f 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -16,7 +16,6 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api import types from .photo import Photo from ..pyrogram_type import PyrogramType @@ -41,7 +40,7 @@ class UserProfilePhotos(PyrogramType): @staticmethod def parse(client, photos) -> "UserProfilePhotos": return UserProfilePhotos( - total_count=len(photos.photos) if isinstance(photos, types.photos.Photos) else photos.count, + total_count=getattr(photos, "count", len(photos.photos)), photos=[Photo.parse(client, photo) for photo in photos.photos], client=client, raw=photos From 187334446796dcdb078977b4f9c2bcf86e281d66 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:27:16 +0100 Subject: [PATCH 40/62] Update get_users to use the refactored User type --- pyrogram/client/methods/users/get_users.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/methods/users/get_users.py b/pyrogram/client/methods/users/get_users.py index 70c62568..7251476a 100644 --- a/pyrogram/client/methods/users/get_users.py +++ b/pyrogram/client/methods/users/get_users.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetUsers(BaseClient): @@ -52,6 +53,6 @@ class GetUsers(BaseClient): users = [] for i in r: - users.append(utils.parse_user(i)) + users.append(pyrogram.User.parse(self, i)) return users if is_iterable else users[0] From 6a0a271d24ee4efb7b64281c13603a83e2322004 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:28:16 +0100 Subject: [PATCH 41/62] Fix broken UpdateStatus in case of incoming updates --- pyrogram/client/types/user_and_chats/user.py | 2 +- .../types/user_and_chats/user_status.py | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 1c1d41fe..ed3ea16d 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -104,7 +104,7 @@ class User(PyrogramType): is_bot=user.bot, first_name=user.first_name, last_name=user.last_name, - status=UserStatus.parse(client, user), + status=UserStatus.parse(client, user.status, user.id, user.bot), username=user.username, language_code=user.lang_code, phone_number=user.phone, diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index d6bfa13d..d9e27498 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -77,24 +77,23 @@ class UserStatus(PyrogramType): self.long_time_ago = long_time_ago @staticmethod - def parse(client, user: types.User): - if user.bot: + def parse(client, user_status, user_id: int, is_bot: bool = False): + if is_bot: return None - raw_status = user.status - status = UserStatus(user_id=user.id, client=client, raw=raw_status) + status = UserStatus(user_id=user_id, client=client, raw=user_status) - if isinstance(raw_status, types.UserStatusOnline): + if isinstance(user_status, types.UserStatusOnline): status.online = True - status.date = raw_status.expires - elif isinstance(raw_status, types.UserStatusOffline): + status.date = user_status.expires + elif isinstance(user_status, types.UserStatusOffline): status.offline = True - status.date = raw_status.was_online - elif isinstance(raw_status, types.UserStatusRecently): + status.date = user_status.was_online + elif isinstance(user_status, types.UserStatusRecently): status.recently = True - elif isinstance(raw_status, types.UserStatusLastWeek): + elif isinstance(user_status, types.UserStatusLastWeek): status.within_week = True - elif isinstance(raw_status, types.UserStatusLastMonth): + elif isinstance(user_status, types.UserStatusLastMonth): status.within_month = True else: status.long_time_ago = True From 2a1a6301a61dbb4884742876ae0622435cbf0edd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:37:58 +0100 Subject: [PATCH 42/62] Add Messages.parse_delete method --- .../types/messages_and_media/messages.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index cd53c38a..444c88ce 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -19,6 +19,7 @@ from pyrogram.api import types from .message import Message from ..pyrogram_type import PyrogramType +from ..user_and_chats import Chat class Messages(PyrogramType): @@ -49,3 +50,32 @@ class Messages(PyrogramType): client=client, raw=messages ) + + @staticmethod + def parse_deleted(client, update) -> "Messages": + messages = update.messages + channel_id = getattr(update, "channel_id", None) + + parsed_messages = [] + + for message in messages: + parsed_messages.append( + Message( + message_id=message, + chat=Chat( + id=int("-100" + str(channel_id)), + type="channel", + client=client, + raw=None + ) if channel_id is not None else None, + client=client, + raw=None + ) + ) + + return Messages( + total_count=len(parsed_messages), + messages=parsed_messages, + client=client, + raw=update + ) From 1344e95be6957dfea6c53c3447b0db8d9f32da84 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:40:06 +0100 Subject: [PATCH 43/62] Use the new parsers in dispatcher.py --- pyrogram/client/dispatcher/dispatcher.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index 3ab704fc..4f3ae620 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -22,9 +22,9 @@ from collections import OrderedDict from queue import Queue from threading import Thread +import pyrogram from pyrogram.api import types -from ..ext import utils -from ..handlers import CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler +from ..handlers import CallbackQueryHandler, MessageHandler, RawUpdateHandler, UserStatusHandler, DeletedMessagesHandler log = logging.getLogger(__name__) @@ -40,7 +40,7 @@ class Dispatcher: types.UpdateEditChannelMessage ) - DELETE_MESSAGE_UPDATES = ( + DELETE_MESSAGES_UPDATES = ( types.UpdateDeleteMessages, types.UpdateDeleteChannelMessages ) @@ -62,16 +62,18 @@ class Dispatcher: self.update_parsers = { Dispatcher.MESSAGE_UPDATES: - lambda upd, usr, cht: (utils.parse_messages(self.client, upd.message, usr, cht), MessageHandler), + lambda upd, usr, cht: (pyrogram.Message.parse(self.client, upd.message, usr, cht), MessageHandler), - Dispatcher.DELETE_MESSAGE_UPDATES: - lambda upd, usr, cht: (utils.parse_deleted_messages(upd), DeletedMessagesHandler), + Dispatcher.DELETE_MESSAGES_UPDATES: + lambda upd, usr, cht: (pyrogram.Messages.parse_deleted(self.client, upd), DeletedMessagesHandler), Dispatcher.CALLBACK_QUERY_UPDATES: - lambda upd, usr, cht: (utils.parse_callback_query(self.client, upd, usr), CallbackQueryHandler), + lambda upd, usr, cht: (pyrogram.CallbackQuery.parse(self.client, upd, usr), CallbackQueryHandler), (types.UpdateUserStatus,): - lambda upd, usr, cht: (utils.parse_user_status(upd.status, upd.user_id), UserStatusHandler) + lambda upd, usr, cht: ( + pyrogram.UserStatus.parse(self.client, upd.status, upd.user_id), UserStatusHandler + ) } self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple} From 761a07bda884887aeda0c7203c4bd5ce5dcd1af4 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Dec 2018 16:41:06 +0100 Subject: [PATCH 44/62] Remove unneeded parts in utils.py --- pyrogram/client/ext/utils.py | 979 +---------------------------------- 1 file changed, 1 insertion(+), 978 deletions(-) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index fac611f3..3e67e3cd 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -16,233 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -import logging from base64 import b64decode, b64encode -from struct import pack -from pyrogram.client import types as pyrogram_types -from ...api import types, functions -from ...api.errors import StickersetInvalid, MessageIdsEmpty - -log = logging.getLogger(__name__) - - -# TODO: Organize the code better? - -class Str(str): - __slots__ = "_client", "_entities" - - def __init__(self, *args): - super().__init__() - self._client = None - self._entities = None - - def init(self, client, entities): - self._client = client - self._entities = entities - - @property - def text(self): - return self - - @property - def markdown(self): - return self._client.markdown.unparse(self, self._entities) - - @property - def html(self): - return self._client.html.unparse(self, self._entities) - - -ENTITIES = { - types.MessageEntityMention.ID: "mention", - types.MessageEntityHashtag.ID: "hashtag", - types.MessageEntityCashtag.ID: "cashtag", - types.MessageEntityBotCommand.ID: "bot_command", - types.MessageEntityUrl.ID: "url", - types.MessageEntityEmail.ID: "email", - types.MessageEntityBold.ID: "bold", - types.MessageEntityItalic.ID: "italic", - types.MessageEntityCode.ID: "code", - types.MessageEntityPre.ID: "pre", - types.MessageEntityTextUrl.ID: "text_link", - types.MessageEntityMentionName.ID: "text_mention", - types.MessageEntityPhone.ID: "phone_number" -} - - -def parse_entities(entities: list, users: dict) -> list: - output_entities = [] - - for entity in entities: - entity_type = ENTITIES.get(entity.ID, None) - - if entity_type: - output_entities.append( - pyrogram_types.MessageEntity( - type=entity_type, - offset=entity.offset, - length=entity.length, - url=getattr(entity, "url", None), - user=parse_user( - users.get( - getattr(entity, "user_id", None), - None - ) - ) - ) - ) - - return output_entities - - -def parse_chat_photo(photo): - if not isinstance(photo, (types.UserProfilePhoto, types.ChatPhoto)): - return None - - if not isinstance(photo.photo_small, types.FileLocation): - return None - - if not isinstance(photo.photo_big, types.FileLocation): - return None - - photo_id = getattr(photo, "photo_id", 0) - loc_small = photo.photo_small - loc_big = photo.photo_big - - return pyrogram_types.ChatPhoto( - small_file_id=encode( - pack( - " pyrogram_types.UserStatus or None: - if is_bot: - return None - - status = pyrogram_types.UserStatus(user_id) - - if isinstance(user_status, types.UserStatusOnline): - status.online = True - status.date = user_status.expires - elif isinstance(user_status, types.UserStatusOffline): - status.offline = True - status.date = user_status.was_online - elif isinstance(user_status, types.UserStatusRecently): - status.recently = True - elif isinstance(user_status, types.UserStatusLastWeek): - status.within_week = True - elif isinstance(user_status, types.UserStatusLastMonth): - status.within_month = True - else: - status.long_time_ago = True - - return status - - -def parse_user(user: types.User) -> pyrogram_types.User or None: - return pyrogram_types.User( - id=user.id, - is_self=user.is_self, - is_contact=user.contact, - is_mutual_contact=user.mutual_contact, - is_deleted=user.deleted, - is_bot=user.bot, - first_name=user.first_name, - last_name=user.last_name, - username=user.username, - language_code=user.lang_code, - phone_number=user.phone, - photo=parse_chat_photo(user.photo), - status=parse_user_status(user.status, is_bot=user.bot), - restriction_reason=user.restriction_reason - ) if user else None - - -def parse_chat(message: types.Message, users: dict, chats: dict) -> pyrogram_types.Chat: - if isinstance(message.to_id, types.PeerUser): - return parse_user_chat(users[message.to_id.user_id if message.out else message.from_id]) - elif isinstance(message.to_id, types.PeerChat): - return parse_chat_chat(chats[message.to_id.chat_id]) - else: - return parse_channel_chat(chats[message.to_id.channel_id]) - - -def parse_user_chat(user: types.User) -> pyrogram_types.Chat: - return pyrogram_types.Chat( - id=user.id, - type="private", - username=user.username, - first_name=user.first_name, - last_name=user.last_name, - photo=parse_chat_photo(user.photo), - restriction_reason=user.restriction_reason - ) - - -def parse_chat_chat(chat: types.Chat) -> pyrogram_types.Chat: - admins_enabled = getattr(chat, "admins_enabled", None) - - if admins_enabled is not None: - admins_enabled = not admins_enabled - - return pyrogram_types.Chat( - id=-chat.id, - type="group", - title=chat.title, - all_members_are_administrators=admins_enabled, - photo=parse_chat_photo(getattr(chat, "photo", None)) - ) - - -def parse_channel_chat(channel: types.Channel) -> pyrogram_types.Chat: - return pyrogram_types.Chat( - id=int("-100" + str(channel.id)), - type="supergroup" if channel.megagroup else "channel", - title=channel.title, - username=getattr(channel, "username", None), - photo=parse_chat_photo(getattr(channel, "photo", None)), - restriction_reason=getattr(channel, "restriction_reason", None) - ) - - -def parse_thumb(thumb: types.PhotoSize or types.PhotoCachedSize) -> pyrogram_types.PhotoSize or None: - if isinstance(thumb, (types.PhotoSize, types.PhotoCachedSize)): - loc = thumb.location - - if isinstance(thumb, types.PhotoSize): - file_size = thumb.size - else: - file_size = len(thumb.bytes) - - if isinstance(loc, types.FileLocation): - return pyrogram_types.PhotoSize( - file_id=encode( - pack( - " bytes: @@ -281,507 +57,6 @@ def encode(s: bytes) -> str: return b64encode(r, b"-_").decode().rstrip("=") -# TODO: Reorganize code, maybe split parts as well -def parse_messages( - client, - messages: list or types.Message or types.MessageService or types.MessageEmpty, - users: dict, - chats: dict, - replies: int = 1 -) -> pyrogram_types.Message or list: - is_list = isinstance(messages, list) - messages = messages if is_list else [messages] - parsed_messages = [] - - for message in messages: - if isinstance(message, types.Message): - entities = parse_entities(message.entities, users) - - forward_from = None - forward_from_chat = None - forward_from_message_id = None - forward_signature = None - forward_date = None - - forward_header = message.fwd_from # type: types.MessageFwdHeader - - if forward_header: - forward_date = forward_header.date - - if forward_header.from_id: - forward_from = parse_user(users[forward_header.from_id]) - else: - forward_from_chat = parse_channel_chat(chats[forward_header.channel_id]) - forward_from_message_id = forward_header.channel_post - forward_signature = forward_header.post_author - - photo = None - location = None - contact = None - venue = None - audio = None - voice = None - animation = None - video = None - video_note = None - sticker = None - document = None - web_page = None - - media = message.media - - if media: - if isinstance(media, types.MessageMediaPhoto): - photo = media.photo - - if isinstance(photo, types.Photo): - sizes = photo.sizes - photo_sizes = [] - - for size in sizes: - if isinstance(size, (types.PhotoSize, types.PhotoCachedSize)): - loc = size.location - - if isinstance(size, types.PhotoSize): - file_size = size.size - else: - file_size = len(size.bytes) - - if isinstance(loc, types.FileLocation): - photo_size = pyrogram_types.PhotoSize( - file_id=encode( - pack( - " pyrogram_types.Messages: - messages = update.messages - channel_id = getattr(update, "channel_id", None) - - parsed_messages = [] - - for message in messages: - parsed_messages.append( - pyrogram_types.Message( - message_id=message, - chat=(pyrogram_types.Chat(id=int("-100" + str(channel_id)), type="channel") - if channel_id is not None - else None) - ) - ) - - return pyrogram_types.Messages(len(parsed_messages), parsed_messages) - - def get_peer_id(input_peer) -> int: return ( input_peer.user_id if isinstance(input_peer, types.InputPeerUser) @@ -807,255 +82,3 @@ def get_offset_date(dialogs): return m.date else: return 0 - - -def parse_profile_photos(photos): - if isinstance(photos, types.photos.Photos): - total_count = len(photos.photos) - else: - total_count = photos.count - - user_profile_photos = [] - - for photo in photos.photos: - if isinstance(photo, types.Photo): - sizes = photo.sizes - photo_sizes = [] - - for size in sizes: - if isinstance(size, (types.PhotoSize, types.PhotoCachedSize)): - loc = size.location - - if isinstance(size, types.PhotoSize): - file_size = size.size - else: - file_size = len(size.bytes) - - if isinstance(loc, types.FileLocation): - photo_size = pyrogram_types.PhotoSize( - file_id=encode( - pack( - " Date: Tue, 18 Dec 2018 08:48:20 +0100 Subject: [PATCH 45/62] Ditch raw attribute. There's no use for it now --- pyrogram/client/types/bots/callback_query.py | 7 ++--- pyrogram/client/types/bots/force_reply.py | 4 +-- .../types/bots/inline_keyboard_button.py | 28 +++++++++---------- .../types/bots/inline_keyboard_markup.py | 6 ++-- pyrogram/client/types/bots/keyboard_button.py | 16 +++++------ .../types/bots/reply_keyboard_markup.py | 4 +-- .../types/bots/reply_keyboard_remove.py | 4 +-- .../types/messages_and_media/animation.py | 7 ++--- .../client/types/messages_and_media/audio.py | 7 ++--- .../types/messages_and_media/contact.py | 7 ++--- .../types/messages_and_media/document.py | 7 ++--- .../types/messages_and_media/location.py | 7 ++--- .../types/messages_and_media/message.py | 16 ++++------- .../messages_and_media/message_entity.py | 7 ++--- .../types/messages_and_media/messages.py | 16 ++++------- .../client/types/messages_and_media/photo.py | 10 +++---- .../types/messages_and_media/photo_size.py | 7 ++--- .../types/messages_and_media/sticker.py | 7 ++--- .../messages_and_media/user_profile_photos.py | 7 ++--- .../client/types/messages_and_media/venue.py | 7 ++--- .../client/types/messages_and_media/video.py | 7 ++--- .../types/messages_and_media/video_note.py | 7 +++-- .../client/types/messages_and_media/voice.py | 7 ++--- pyrogram/client/types/pyrogram_type.py | 3 +- pyrogram/client/types/user_and_chats/chat.py | 10 +++---- .../types/user_and_chats/chat_member.py | 14 +++++----- .../types/user_and_chats/chat_members.py | 7 ++--- .../client/types/user_and_chats/chat_photo.py | 11 ++++---- .../client/types/user_and_chats/dialog.py | 7 ++--- .../client/types/user_and_chats/dialogs.py | 7 ++--- pyrogram/client/types/user_and_chats/user.py | 7 ++--- .../types/user_and_chats/user_status.py | 6 ++-- 32 files changed, 122 insertions(+), 152 deletions(-) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index 61ece31e..e586c736 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -56,9 +56,9 @@ class CallbackQuery(PyrogramType): """ - def __init__(self, *, client, raw, id: str, from_user, chat_instance: str, message=None, + def __init__(self, *, client, id: str, from_user, chat_instance: str, message=None, inline_message_id: str = None, data: bytes = None, game_short_name: str = None): - super().__init__(client, raw) + super().__init__(client) self.id = id self.from_user = from_user @@ -103,8 +103,7 @@ class CallbackQuery(PyrogramType): chat_instance=str(callback_query.chat_instance), data=callback_query.data, game_short_name=callback_query.game_short_name, - client=client, - raw=callback_query + client=client ) def answer(self, text: str = None, show_alert: bool = None, url: str = None, cache_time: int = 0): diff --git a/pyrogram/client/types/bots/force_reply.py b/pyrogram/client/types/bots/force_reply.py index 6d91ff5f..eb3a8729 100644 --- a/pyrogram/client/types/bots/force_reply.py +++ b/pyrogram/client/types/bots/force_reply.py @@ -34,12 +34,12 @@ class ForceReply(PyrogramType): """ def __init__(self, selective: bool = None): - super().__init__(None, None) + super().__init__(None) self.selective = selective @staticmethod - def read(o, *args): + def read(o): return ForceReply( selective=o.selective ) diff --git a/pyrogram/client/types/bots/inline_keyboard_button.py b/pyrogram/client/types/bots/inline_keyboard_button.py index 34718fd1..0fd1355a 100644 --- a/pyrogram/client/types/bots/inline_keyboard_button.py +++ b/pyrogram/client/types/bots/inline_keyboard_button.py @@ -55,7 +55,7 @@ class InlineKeyboardButton(PyrogramType): def __init__(self, text: str, callback_data: bytes = None, url: str = None, switch_inline_query: str = None, switch_inline_query_current_chat: str = None): - super().__init__(None, None) + super().__init__(None) self.text = text self.url = url @@ -66,29 +66,29 @@ class InlineKeyboardButton(PyrogramType): # self.pay = pay @staticmethod - def read(b, *args): - if isinstance(b, KeyboardButtonUrl): + def read(o): + if isinstance(o, KeyboardButtonUrl): return InlineKeyboardButton( - text=b.text, - url=b.url + text=o.text, + url=o.url ) - if isinstance(b, KeyboardButtonCallback): + if isinstance(o, KeyboardButtonCallback): return InlineKeyboardButton( - text=b.text, - callback_data=b.data + text=o.text, + callback_data=o.data ) - if isinstance(b, KeyboardButtonSwitchInline): - if b.same_peer: + if isinstance(o, KeyboardButtonSwitchInline): + if o.same_peer: return InlineKeyboardButton( - text=b.text, - switch_inline_query_current_chat=b.query + text=o.text, + switch_inline_query_current_chat=o.query ) else: return InlineKeyboardButton( - text=b.text, - switch_inline_query=b.query + text=o.text, + switch_inline_query=o.query ) def write(self): diff --git a/pyrogram/client/types/bots/inline_keyboard_markup.py b/pyrogram/client/types/bots/inline_keyboard_markup.py index bde31ec4..b4eb936f 100644 --- a/pyrogram/client/types/bots/inline_keyboard_markup.py +++ b/pyrogram/client/types/bots/inline_keyboard_markup.py @@ -30,15 +30,15 @@ class InlineKeyboardMarkup(PyrogramType): """ def __init__(self, inline_keyboard: list): - super().__init__(None, None) + super().__init__(None) self.inline_keyboard = inline_keyboard @staticmethod - def read(kb, *args): + def read(o): inline_keyboard = [] - for i in kb.rows: + for i in o.rows: row = [] for j in i.buttons: diff --git a/pyrogram/client/types/bots/keyboard_button.py b/pyrogram/client/types/bots/keyboard_button.py index 0566fc20..ceb7a2b7 100644 --- a/pyrogram/client/types/bots/keyboard_button.py +++ b/pyrogram/client/types/bots/keyboard_button.py @@ -41,26 +41,26 @@ class KeyboardButton(PyrogramType): """ def __init__(self, text: str, request_contact: bool = None, request_location: bool = None): - super().__init__(None, None) + super().__init__(None) self.text = text self.request_contact = request_contact self.request_location = request_location @staticmethod - def read(b, *args): - if isinstance(b, RawKeyboardButton): - return b.text + def read(o): + if isinstance(o, RawKeyboardButton): + return o.text - if isinstance(b, KeyboardButtonRequestPhone): + if isinstance(o, KeyboardButtonRequestPhone): return KeyboardButton( - text=b.text, + text=o.text, request_contact=True ) - if isinstance(b, KeyboardButtonRequestGeoLocation): + if isinstance(o, KeyboardButtonRequestGeoLocation): return KeyboardButton( - text=b.text, + text=o.text, request_location=True ) diff --git a/pyrogram/client/types/bots/reply_keyboard_markup.py b/pyrogram/client/types/bots/reply_keyboard_markup.py index 77b72562..fb0817ac 100644 --- a/pyrogram/client/types/bots/reply_keyboard_markup.py +++ b/pyrogram/client/types/bots/reply_keyboard_markup.py @@ -49,7 +49,7 @@ class ReplyKeyboardMarkup(PyrogramType): def __init__(self, keyboard: list, resize_keyboard: bool = None, one_time_keyboard: bool = None, selective: bool = None): - super().__init__(None, None) + super().__init__(None) self.keyboard = keyboard self.resize_keyboard = resize_keyboard @@ -57,7 +57,7 @@ class ReplyKeyboardMarkup(PyrogramType): self.selective = selective @staticmethod - def read(kb, *args): + def read(kb): keyboard = [] for i in kb.rows: diff --git a/pyrogram/client/types/bots/reply_keyboard_remove.py b/pyrogram/client/types/bots/reply_keyboard_remove.py index 82adb859..7f82fd6e 100644 --- a/pyrogram/client/types/bots/reply_keyboard_remove.py +++ b/pyrogram/client/types/bots/reply_keyboard_remove.py @@ -36,12 +36,12 @@ class ReplyKeyboardRemove(PyrogramType): """ def __init__(self, selective: bool = None): - super().__init__(None, None) + super().__init__(None) self.selective = selective @staticmethod - def read(o, *args): + def read(o): return ReplyKeyboardRemove( selective=o.selective ) diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index c270dfa9..45beefc3 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -56,9 +56,9 @@ class Animation(PyrogramType): Date the animation was sent in Unix time. """ - def __init__(self, *, client, raw, file_id: str, width: int, height: int, duration: int, thumb=None, + def __init__(self, *, client, file_id: str, width: int, height: int, duration: int, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -91,6 +91,5 @@ class Animation(PyrogramType): file_size=animation.size, file_name=file_name, date=animation.date, - client=client, - raw=animation + client=client ) diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index bc5cf166..9d001615 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -56,10 +56,10 @@ class Audio(PyrogramType): Title of the audio as defined by sender or by audio tags. """ - def __init__(self, *, client, raw, file_id: str, duration: int, thumb=None, file_name: str = None, + def __init__(self, *, client, file_id: str, duration: int, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, performer: str = None, title: str = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -91,6 +91,5 @@ class Audio(PyrogramType): thumb=PhotoSize.parse(client, audio.thumb), file_name=file_name, date=audio.date, - client=client, - raw=audio + client=client ) diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 0525c463..2195df45 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -40,9 +40,9 @@ class Contact(PyrogramType): Additional data about the contact in the form of a vCard. """ - def __init__(self, *, client, raw, phone_number: str, first_name: str, last_name: str = None, user_id: int = None, + def __init__(self, *, client, phone_number: str, first_name: str, last_name: str = None, user_id: int = None, vcard: str = None): - super().__init__(client, raw) + super().__init__(client) self.phone_number = phone_number self.first_name = first_name @@ -58,6 +58,5 @@ class Contact(PyrogramType): last_name=contact.last_name or None, vcard=contact.vcard or None, user_id=contact.user_id or None, - client=client, - raw=contact + client=client ) diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 0526a190..6d124e2a 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -47,9 +47,9 @@ class Document(PyrogramType): Date the document was sent in Unix time. """ - def __init__(self, *, client, raw, file_id: str, thumb=None, file_name: str = None, mime_type: str = None, + def __init__(self, *, client, file_id: str, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -75,6 +75,5 @@ class Document(PyrogramType): mime_type=document.mime_type, file_size=document.size, date=document.date, - client=client, - raw=document + client=client ) diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 853a4c90..3f5bb059 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -31,8 +31,8 @@ class Location(PyrogramType): Latitude as defined by sender. """ - def __init__(self, *, client, raw, longitude: float, latitude: float, ): - super().__init__(client, raw) + def __init__(self, *, client, longitude: float, latitude: float, ): + super().__init__(client) self.longitude = longitude self.latitude = latitude @@ -43,6 +43,5 @@ class Location(PyrogramType): return Location( longitude=geo_point.long, latitude=geo_point.lat, - client=client, - raw=geo_point + client=client ) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 66072d22..c0c7edd1 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -224,7 +224,7 @@ class Message(PyrogramType): # TODO: Add game missing field. Also invoice, successful_payment, connected_website - def __init__(self, *, client, raw, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, + def __init__(self, *, client, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, forward_from_message_id: int = None, forward_signature: str = None, forward_date: int = None, reply_to_message=None, mentioned=None, empty=None, service=None, media=None, edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, @@ -236,7 +236,7 @@ class Message(PyrogramType): channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, command: list = None, reply_markup=None): - super().__init__(client, raw) + super().__init__(client) self.message_id = message_id self.date = date @@ -293,11 +293,7 @@ class Message(PyrogramType): def parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, replies: int = 1): if isinstance(message, types.MessageEmpty): - return Message( - message_id=message.id, - client=client, - raw=message - ) + return Message(message_id=message.id, empty=True, client=client) if isinstance(message, types.MessageService): action = message.action @@ -348,8 +344,7 @@ class Message(PyrogramType): migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None, group_chat_created=group_chat_created, channel_chat_created=channel_chat_created, - client=client, - raw=message + client=client # TODO: supergroup_chat_created ) @@ -504,8 +499,7 @@ class Message(PyrogramType): via_bot=User.parse(client, users.get(message.via_bot_id, None)), outgoing=message.out, reply_markup=reply_markup, - client=client, - raw=message + client=client ) if message.reply_to_msg_id and replies: diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index c72df95d..9f894f75 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -61,8 +61,8 @@ class MessageEntity(PyrogramType): types.MessageEntityPhone.ID: "phone_number" } - def __init__(self, *, client, raw, type: str, offset: int, length: int, url: str = None, user=None): - super().__init__(client, raw) + def __init__(self, *, client, type: str, offset: int, length: int, url: str = None, user=None): + super().__init__(client) self.type = type self.offset = offset @@ -83,6 +83,5 @@ class MessageEntity(PyrogramType): length=entity.length, url=getattr(entity, "url", None), user=User.parse(client, users.get(getattr(entity, "user_id", None), None)), - client=client, - raw=entity + client=client ) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 444c88ce..6d2d666a 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -33,8 +33,8 @@ class Messages(PyrogramType): Requested messages. """ - def __init__(self, *, client, raw, total_count: int, messages: list): - super().__init__(client, raw) + def __init__(self, *, client, total_count: int, messages: list): + super().__init__(client) self.total_count = total_count self.messages = messages @@ -47,8 +47,7 @@ class Messages(PyrogramType): return Messages( total_count=getattr(messages, "count", len(messages.messages)), messages=[Message.parse(client, message, users, chats) for message in messages.messages], - client=client, - raw=messages + client=client ) @staticmethod @@ -65,17 +64,14 @@ class Messages(PyrogramType): chat=Chat( id=int("-100" + str(channel_id)), type="channel", - client=client, - raw=None + client=client ) if channel_id is not None else None, - client=client, - raw=None + client=client ) ) return Messages( total_count=len(parsed_messages), messages=parsed_messages, - client=client, - raw=update + client=client ) diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 96a1e19a..01edd0fc 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -39,8 +39,8 @@ class Photo(PyrogramType): Available sizes of this photo. """ - def __init__(self, *, client, raw, id: str, date: int, sizes: list): - super().__init__(client, raw) + def __init__(self, *, client, id: str, date: int, sizes: list): + super().__init__(client) self.id = id self.date = date @@ -74,8 +74,7 @@ class Photo(PyrogramType): width=raw_size.w, height=raw_size.h, file_size=file_size, - client=client, - raw=raw_size + client=client ) sizes.append(size) @@ -91,6 +90,5 @@ class Photo(PyrogramType): ).decode().rstrip("="), date=photo.date, sizes=sizes, - client=client, - raw=photo + client=client ) diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index 1e37399c..efc9def9 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -40,8 +40,8 @@ class PhotoSize(PyrogramType): File size. """ - def __init__(self, *, client, raw, file_id: str, width: int, height: int, file_size: int): - super().__init__(client, raw) + def __init__(self, *, client, file_id: str, width: int, height: int, file_size: int): + super().__init__(client) self.file_id = file_id self.width = width @@ -71,6 +71,5 @@ class PhotoSize(PyrogramType): width=photo_size.w, height=photo_size.h, file_size=file_size, - client=client, - raw=photo_size + client=client ) diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index f2414311..9892694a 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -63,10 +63,10 @@ class Sticker(PyrogramType): # TODO: Add mask position - def __init__(self, *, client, raw, file_id: str, width: int, height: int, thumb=None, file_name: str = None, + def __init__(self, *, client, file_id: str, width: int, height: int, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, emoji: str = None, set_name: str = None, mask_position=None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -123,6 +123,5 @@ class Sticker(PyrogramType): mime_type=sticker.mime_type, file_name=file_name, date=sticker.date, - client=client, - raw=sticker + client=client ) diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index 2196608f..23b3200c 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -31,8 +31,8 @@ class UserProfilePhotos(PyrogramType): Requested profile pictures. """ - def __init__(self, *, client, raw, total_count: int, photos: list): - super().__init__(client, raw) + def __init__(self, *, client, total_count: int, photos: list): + super().__init__(client) self.total_count = total_count self.photos = photos @@ -42,6 +42,5 @@ class UserProfilePhotos(PyrogramType): return UserProfilePhotos( total_count=getattr(photos, "count", len(photos.photos)), photos=[Photo.parse(client, photo) for photo in photos.photos], - client=client, - raw=photos + client=client ) diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index ee533d59..c206a2a0 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -43,9 +43,9 @@ class Venue(PyrogramType): """ - def __init__(self, *, client, raw, location, title: str, address: str, foursquare_id: str = None, + def __init__(self, *, client, location, title: str, address: str, foursquare_id: str = None, foursquare_type: str = None): - super().__init__(client, raw) + super().__init__(client) self.location = location self.title = title @@ -61,6 +61,5 @@ class Venue(PyrogramType): address=venue.address, foursquare_id=venue.venue_id or None, foursquare_type=venue.venue_type, - client=client, - raw=venue + client=client ) diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 9ca54b69..7d995ea8 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -56,9 +56,9 @@ class Video(PyrogramType): Date the video was sent in Unix time. """ - def __init__(self, *, client, raw, file_id: str, width: int, height: int, duration: int, thumb=None, + def __init__(self, *, client, file_id: str, width: int, height: int, duration: int, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -90,6 +90,5 @@ class Video(PyrogramType): file_size=video.size, file_name=file_name, date=video.date, - client=client, - raw=video + client=client ) diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 61a86454..688bca19 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -50,9 +50,9 @@ class VideoNote(PyrogramType): Date the video note was sent in Unix time. """ - def __init__(self, *, client, raw, file_id: str, length: int, duration: int, thumb=None, mime_type: str = None, + def __init__(self, *, client, file_id: str, length: int, duration: int, thumb=None, mime_type: str = None, file_size: int = None, date: int = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.thumb = thumb @@ -79,5 +79,6 @@ class VideoNote(PyrogramType): thumb=PhotoSize.parse(client, video_note.thumb), file_size=video_note.size, mime_type=video_note.mime_type, - date=video_note.date + date=video_note.date, + client=client ) diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index 88cc269e..cce70a31 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -46,9 +46,9 @@ class Voice(PyrogramType): Date the voice was sent in Unix time. """ - def __init__(self, *, client, raw, file_id: str, duration: int, waveform: bytes = None, mime_type: str = None, + def __init__(self, *, client, file_id: str, duration: int, waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None): - super().__init__(client, raw) + super().__init__(client) self.file_id = file_id self.duration = duration @@ -74,6 +74,5 @@ class Voice(PyrogramType): file_size=voice.size, waveform=attributes.waveform, date=voice.date, - client=client, - raw=voice + client=client ) diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index 880c452a..3c2babf8 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -21,9 +21,8 @@ from json import dumps, JSONEncoder class PyrogramType: - def __init__(self, client, raw): + def __init__(self, client): self._client = client - self._raw = raw def __str__(self): return dumps(self, cls=Encoder, indent=4) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index a9564836..b9d0d796 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -76,11 +76,11 @@ class Chat(PyrogramType): The reason why this chat might be unavailable to some users. """ - def __init__(self, *, client, raw, id: int, type: str, title: str = None, username: str = None, + def __init__(self, *, client, id: int, type: str, title: str = None, username: str = None, first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, photo=None, description: str = None, invite_link: str = None, pinned_message=None, sticker_set_name: str = None, can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None): - super().__init__(client, raw) + super().__init__(client) self.id = id self.type = type @@ -108,7 +108,7 @@ class Chat(PyrogramType): last_name=user.last_name, photo=ChatPhoto.parse(client, user.photo), restriction_reason=user.restriction_reason, - client=client, raw=user + client=client ) @staticmethod @@ -124,7 +124,7 @@ class Chat(PyrogramType): title=chat.title, all_members_are_administrators=admins_enabled, photo=ChatPhoto.parse(client, getattr(chat, "photo", None)), - client=client, raw=chat + client=client ) @staticmethod @@ -136,7 +136,7 @@ class Chat(PyrogramType): username=getattr(channel, "username", None), photo=ChatPhoto.parse(client, getattr(channel, "photo", None)), restriction_reason=getattr(channel, "restriction_reason", None), - client=client, raw=channel + client=client ) @staticmethod diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 11b2c9a0..27eeb897 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -79,13 +79,13 @@ class ChatMember(PyrogramType): Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages. """ - def __init__(self, *, client, raw, user, status: str, until_date: int = None, can_be_edited: bool = None, + def __init__(self, *, client, user, status: str, until_date: int = None, can_be_edited: bool = None, can_change_info: bool = None, can_post_messages: bool = None, can_edit_messages: bool = None, can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None, can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None, can_send_media_messages: bool = None, can_send_other_messages: bool = None, can_add_web_page_previews: bool = None): - super().__init__(client, raw) + super().__init__(client) self.user = user self.status = status @@ -107,13 +107,13 @@ class ChatMember(PyrogramType): @staticmethod def parse(client, member, user) -> "ChatMember": if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): - return ChatMember(user=user, status="member", client=client, raw=member) + return ChatMember(user=user, status="member", client=client) if isinstance(member, (types.ChannelParticipantCreator, types.ChatParticipantCreator)): - return ChatMember(user=user, status="creator", client=client, raw=member) + return ChatMember(user=user, status="creator", client=client) if isinstance(member, types.ChatParticipantAdmin): - return ChatMember(user=user, status="administrator", client=client, raw=member) + return ChatMember(user=user, status="administrator", client=client) if isinstance(member, types.ChannelParticipantAdmin): rights = member.admin_rights @@ -130,7 +130,7 @@ class ChatMember(PyrogramType): can_restrict_members=rights.ban_users, can_pin_messages=rights.pin_messages, can_promote_members=rights.add_admins, - client=client, raw=member + client=client ) if isinstance(member, types.ChannelParticipantBanned): @@ -140,7 +140,7 @@ class ChatMember(PyrogramType): user=user, status="kicked" if rights.view_messages else "restricted", until_date=0 if rights.until_date == (1 << 31) - 1 else rights.until_date, - client=client, raw=member + client=client ) if chat_member.status == "restricted": diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index 5fa8275d..ba28b23d 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -33,8 +33,8 @@ class ChatMembers(PyrogramType): Requested chat members. """ - def __init__(self, *, client, raw, total_count: int, chat_members: list): - super().__init__(client, raw) + def __init__(self, *, client, total_count: int, chat_members: list): + super().__init__(client) self.total_count = total_count self.chat_members = chat_members @@ -58,6 +58,5 @@ class ChatMembers(PyrogramType): return ChatMembers( total_count=total_count, chat_members=chat_members, - client=client, - raw=members + client=client ) diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index b998cf90..91c77b06 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -19,11 +19,11 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class ChatPhoto(Object): +class ChatPhoto(PyrogramType): """This object represents a chat photo. Args: @@ -34,9 +34,9 @@ class ChatPhoto(Object): Unique file identifier of big (640x640) chat photo. This file_id can be used only for photo download. """ - ID = 0xb0700015 + def __init__(self, *, client, small_file_id: str, big_file_id: str): + super().__init__(client) - def __init__(self, *, client, raw, small_file_id: str, big_file_id: str): self.small_file_id = small_file_id self.big_file_id = big_file_id @@ -68,6 +68,5 @@ class ChatPhoto(Object): 1, loc_big.dc_id, photo_id, 0, loc_big.volume_id, loc_big.secret, loc_big.local_id ) ), - client=client, - raw=chat_photo + client=client ) diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 0fc17fe0..d617c11e 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -44,9 +44,9 @@ class Dialog(PyrogramType): True, if the dialog is pinned. """ - def __init__(self, *, client, raw, chat, top_message, unread_messages_count: int, unread_mentions_count: int, + def __init__(self, *, client, chat, top_message, unread_messages_count: int, unread_mentions_count: int, unread_mark: bool, is_pinned: bool): - super().__init__(client, raw) + super().__init__(client) self.chat = chat self.top_message = top_message @@ -73,6 +73,5 @@ class Dialog(PyrogramType): unread_mentions_count=dialog.unread_mentions_count, unread_mark=dialog.unread_mark, is_pinned=dialog.pinned, - client=client, - raw=dialog + client=client ) diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 8261e97c..21eda2f0 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -33,8 +33,8 @@ class Dialogs(PyrogramType): Requested dialogs. """ - def __init__(self, *, client, raw, total_count: int, dialogs: list): - super().__init__(client, raw) + def __init__(self, *, client, total_count: int, dialogs: list): + super().__init__(client) self.total_count = total_count self.dialogs = dialogs @@ -64,6 +64,5 @@ class Dialogs(PyrogramType): return Dialogs( total_count=getattr(dialogs, "count", len(dialogs.dialogs)), dialogs=[Dialog.parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs], - client=client, - raw=dialogs + client=client ) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index ed3ea16d..66d68121 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -69,11 +69,11 @@ class User(PyrogramType): The reason why this bot might be unavailable to some users. """ - def __init__(self, *, client, raw, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, + def __init__(self, *, client, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool, is_bot: bool, first_name: str, last_name: str = None, status=None, username: str = None, language_code: str = None, phone_number: str = None, photo=None, restriction_reason: str = None): - super().__init__(client, raw) + super().__init__(client) self.id = id self.is_self = is_self @@ -110,6 +110,5 @@ class User(PyrogramType): phone_number=user.phone, photo=ChatPhoto.parse(client, user.photo), restriction_reason=user.restriction_reason, - client=client, - raw=user + client=client ) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index d9e27498..8102ffe0 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -62,10 +62,10 @@ class UserStatus(PyrogramType): always shown to blocked users), None otherwise. """ - def __init__(self, *, client, raw, user_id: int, online: bool = None, offline: bool = None, date: int = None, + def __init__(self, *, client, user_id: int, online: bool = None, offline: bool = None, date: int = None, recently: bool = None, within_week: bool = None, within_month: bool = None, long_time_ago: bool = None): - super().__init__(client, raw) + super().__init__(client) self.user_id = user_id self.online = online @@ -81,7 +81,7 @@ class UserStatus(PyrogramType): if is_bot: return None - status = UserStatus(user_id=user_id, client=client, raw=user_status) + status = UserStatus(user_id=user_id, client=client) if isinstance(user_status, types.UserStatusOnline): status.online = True From 27052e31ca8f853bebab7f19727d59e3347db08a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 18 Dec 2018 09:45:49 +0100 Subject: [PATCH 46/62] Fix all those unresolved references caused by the refactor --- .../client/methods/chats/get_chat_members.py | 4 ++-- .../client/methods/chats/kick_chat_member.py | 4 ++-- .../methods/messages/edit_message_caption.py | 6 ++++-- .../methods/messages/edit_message_media.py | 3 ++- .../messages/edit_message_reply_markup.py | 5 +++-- .../methods/messages/edit_message_text.py | 5 +++-- .../methods/messages/forward_messages.py | 5 +++-- .../client/methods/messages/get_messages.py | 21 +++++++------------ .../client/methods/messages/send_animation.py | 3 ++- .../client/methods/messages/send_audio.py | 3 ++- .../client/methods/messages/send_contact.py | 5 +++-- .../client/methods/messages/send_document.py | 3 ++- .../client/methods/messages/send_location.py | 5 +++-- .../client/methods/messages/send_message.py | 14 ++++++++----- .../client/methods/messages/send_photo.py | 3 ++- .../client/methods/messages/send_sticker.py | 3 ++- .../client/methods/messages/send_venue.py | 5 +++-- .../client/methods/messages/send_video.py | 3 ++- .../methods/messages/send_video_note.py | 3 ++- .../client/methods/messages/send_voice.py | 3 ++- pyrogram/client/methods/users/get_me.py | 6 ++++-- .../methods/utilities/download_media.py | 9 +++++--- 22 files changed, 70 insertions(+), 51 deletions(-) diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index df1ca8b1..71728676 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -16,9 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api import functions, types -from ...ext import BaseClient, utils import pyrogram +from pyrogram.api import functions, types +from ...ext import BaseClient class Filters: diff --git a/pyrogram/client/methods/chats/kick_chat_member.py b/pyrogram/client/methods/chats/kick_chat_member.py index 5bac80b8..292fdfd6 100644 --- a/pyrogram/client/methods/chats/kick_chat_member.py +++ b/pyrogram/client/methods/chats/kick_chat_member.py @@ -16,8 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import utils from ...ext import BaseClient @@ -86,7 +86,7 @@ class KickChatMember(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_caption.py b/pyrogram/client/methods/messages/edit_message_caption.py index 0e12f72b..a79c21f4 100644 --- a/pyrogram/client/methods/messages/edit_message_caption.py +++ b/pyrogram/client/methods/messages/edit_message_caption.py @@ -16,8 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class EditMessageCaption(BaseClient): @@ -68,7 +70,7 @@ class EditMessageCaption(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_media.py b/pyrogram/client/methods/messages/edit_message_media.py index ccbacac7..5d06c31b 100644 --- a/pyrogram/client/methods/messages/edit_message_media.py +++ b/pyrogram/client/methods/messages/edit_message_media.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid from pyrogram.client.ext import BaseClient, utils @@ -333,7 +334,7 @@ class EditMessageMedia(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_reply_markup.py b/pyrogram/client/methods/messages/edit_message_reply_markup.py index 72796306..3b301dcb 100644 --- a/pyrogram/client/methods/messages/edit_message_reply_markup.py +++ b/pyrogram/client/methods/messages/edit_message_reply_markup.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class EditMessageReplyMarkup(BaseClient): @@ -57,7 +58,7 @@ class EditMessageReplyMarkup(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_text.py b/pyrogram/client/methods/messages/edit_message_text.py index 144f5413..991ff049 100644 --- a/pyrogram/client/methods/messages/edit_message_text.py +++ b/pyrogram/client/methods/messages/edit_message_text.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class EditMessageText(BaseClient): @@ -73,7 +74,7 @@ class EditMessageText(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/forward_messages.py b/pyrogram/client/methods/messages/forward_messages.py index a8783044..e2e10c93 100644 --- a/pyrogram/client/methods/messages/forward_messages.py +++ b/pyrogram/client/methods/messages/forward_messages.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from ...ext import BaseClient, utils +from ...ext import BaseClient class ForwardMessages(BaseClient): @@ -77,7 +78,7 @@ class ForwardMessages(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): messages.append( - utils.parse_messages( + pyrogram.Message.parse( self, i.message, users, chats ) diff --git a/pyrogram/client/methods/messages/get_messages.py b/pyrogram/client/methods/messages/get_messages.py index 7411e22f..2e3930ae 100644 --- a/pyrogram/client/methods/messages/get_messages.py +++ b/pyrogram/client/methods/messages/get_messages.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetMessages(BaseClient): @@ -48,10 +49,9 @@ class GetMessages(BaseClient): The number of subsequent replies to get for each message. Defaults to 1. Returns: - On success and in case *message_ids* or *reply_to_message_ids* was a list, the returned value will be a - list of the requested :obj:`Messages ` even if a list contains just one element, - otherwise if *message_ids* or *reply_to_message_ids* was an integer, the single requested - :obj:`Message ` is returned. + On success and in case *message_ids* or *reply_to_message_ids* was an iterable, the returned value will be a + :obj:`Messages ` even if a list contains just one element. Otherwise, if *message_ids* or + *reply_to_message_ids* was an integer, the single requested :obj:`Message ` is returned. Raises: :class:`Error ` in case of a Telegram RPC error. @@ -76,13 +76,6 @@ class GetMessages(BaseClient): else: rpc = functions.messages.GetMessages(id=ids) - r = self.send(rpc) + messages = pyrogram.Messages.parse(self, self.send(rpc)) - messages = utils.parse_messages( - self, r.messages, - {i.id: i for i in r.users}, - {i.id: i for i in r.chats}, - replies=replies - ) - - return messages if is_iterable else messages[0] + return messages if is_iterable else messages.messages[0] diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py index dbd29575..c86fb867 100644 --- a/pyrogram/client/methods/messages/send_animation.py +++ b/pyrogram/client/methods/messages/send_animation.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -184,7 +185,7 @@ class SendAnimation(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 613ec90c..12c65dec 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -184,7 +185,7 @@ class SendAudio(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_contact.py b/pyrogram/client/methods/messages/send_contact.py index b40321be..a5d88f97 100644 --- a/pyrogram/client/methods/messages/send_contact.py +++ b/pyrogram/client/methods/messages/send_contact.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class SendContact(BaseClient): @@ -86,7 +87,7 @@ class SendContact(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py index f3f52178..66a65e9d 100644 --- a/pyrogram/client/methods/messages/send_document.py +++ b/pyrogram/client/methods/messages/send_document.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -165,7 +166,7 @@ class SendDocument(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_location.py b/pyrogram/client/methods/messages/send_location.py index 2bd3eff3..baa84966 100644 --- a/pyrogram/client/methods/messages/send_location.py +++ b/pyrogram/client/methods/messages/send_location.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class SendLocation(BaseClient): @@ -78,7 +79,7 @@ class SendLocation(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_message.py b/pyrogram/client/methods/messages/send_message.py index 78ce27e5..77af4db8 100644 --- a/pyrogram/client/methods/messages/send_message.py +++ b/pyrogram/client/methods/messages/send_message.py @@ -16,9 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client import types as pyrogram_types -from ...ext import utils, BaseClient +from ...ext import BaseClient class SendMessage(BaseClient): @@ -83,9 +83,13 @@ class SendMessage(BaseClient): ) if isinstance(r, types.UpdateShortSentMessage): - return pyrogram_types.Message( + return pyrogram.Message( message_id=r.id, - chat=pyrogram_types.Chat(id=list(self.resolve_peer(chat_id).__dict__.values())[0], type="private"), + chat=pyrogram.Chat( + id=list(self.resolve_peer(chat_id).__dict__.values())[0], + type="private", + client=self + ), text=message, date=r.date, outgoing=r.out, @@ -95,7 +99,7 @@ class SendMessage(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py index 7f0c2d07..323c2fa5 100644 --- a/pyrogram/client/methods/messages/send_photo.py +++ b/pyrogram/client/methods/messages/send_photo.py @@ -20,6 +20,7 @@ import binascii import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -160,7 +161,7 @@ class SendPhoto(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_sticker.py b/pyrogram/client/methods/messages/send_sticker.py index 0b8f8073..fccfb0b5 100644 --- a/pyrogram/client/methods/messages/send_sticker.py +++ b/pyrogram/client/methods/messages/send_sticker.py @@ -20,6 +20,7 @@ import binascii import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -144,7 +145,7 @@ class SendSticker(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_venue.py b/pyrogram/client/methods/messages/send_venue.py index 30b848ea..1155ca79 100644 --- a/pyrogram/client/methods/messages/send_venue.py +++ b/pyrogram/client/methods/messages/send_venue.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient class SendVenue(BaseClient): @@ -100,7 +101,7 @@ class SendVenue(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index e8e4627a..47b15d59 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -187,7 +188,7 @@ class SendVideo(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_video_note.py b/pyrogram/client/methods/messages/send_video_note.py index de539a81..61ae0017 100644 --- a/pyrogram/client/methods/messages/send_video_note.py +++ b/pyrogram/client/methods/messages/send_video_note.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -163,7 +164,7 @@ class SendVideoNote(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py index 4be702d0..4882a87f 100644 --- a/pyrogram/client/methods/messages/send_voice.py +++ b/pyrogram/client/methods/messages/send_voice.py @@ -21,6 +21,7 @@ import mimetypes import os import struct +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid, FilePartMissing from pyrogram.client.ext import BaseClient, utils @@ -163,7 +164,7 @@ class SendVoice(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return utils.parse_messages( + return pyrogram.Message.parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/users/get_me.py b/pyrogram/client/methods/users/get_me.py index 009ef71e..c7d32968 100644 --- a/pyrogram/client/methods/users/get_me.py +++ b/pyrogram/client/methods/users/get_me.py @@ -16,8 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types -from ...ext import BaseClient, utils +from ...ext import BaseClient class GetMe(BaseClient): @@ -30,7 +31,8 @@ class GetMe(BaseClient): Raises: :class:`Error ` in case of a Telegram RPC error. """ - return utils.parse_user( + return pyrogram.User.parse( + self, self.send( functions.users.GetFullUser( types.InputPeerSelf() diff --git a/pyrogram/client/methods/utilities/download_media.py b/pyrogram/client/methods/utilities/download_media.py index 9b6c554a..f0ad0396 100644 --- a/pyrogram/client/methods/utilities/download_media.py +++ b/pyrogram/client/methods/utilities/download_media.py @@ -84,7 +84,8 @@ class DownloadMedia(BaseClient): file_id=message.photo.sizes[-1].file_id, file_size=message.photo.sizes[-1].file_size, mime_type="", - date=message.photo.date + date=message.photo.date, + client=self ) elif message.audio: media = message.audio @@ -118,7 +119,8 @@ class DownloadMedia(BaseClient): file_id=message.sizes[-1].file_id, file_size=message.sizes[-1].file_size, mime_type="", - date=message.date + date=message.date, + client=self ) else: media = message @@ -126,7 +128,8 @@ class DownloadMedia(BaseClient): media = pyrogram_types.Document( file_id=message, file_size=0, - mime_type="" + mime_type="", + client=self ) else: raise ValueError(error_message) From 914dfca5749375ca2c6fe3c040e2b33aac6219b9 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 18 Dec 2018 09:50:39 +0100 Subject: [PATCH 47/62] Reformat code and optimize imports --- pyrogram/client/client.py | 6 +++--- pyrogram/client/methods/chats/get_chat_member.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index bd132851..7003823d 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1135,13 +1135,13 @@ class Client(Methods, BaseClient): progress_args: tuple = ()): part_size = 512 * 1024 file_size = os.path.getsize(path) - + if file_size == 0: raise ValueError("File size equals to 0 B") - + if file_size > 1500 * 1024 * 1024: raise ValueError("Telegram doesn't support uploading files bigger than 1500 MiB") - + file_total_parts = int(math.ceil(file_size / part_size)) is_big = True if file_size > 10 * 1024 * 1024 else False is_missing_part = True if file_id is not None else False diff --git a/pyrogram/client/methods/chats/get_chat_member.py b/pyrogram/client/methods/chats/get_chat_member.py index b5e7cec7..b304be17 100644 --- a/pyrogram/client/methods/chats/get_chat_member.py +++ b/pyrogram/client/methods/chats/get_chat_member.py @@ -16,9 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import functions, types, errors from ...ext import BaseClient -import pyrogram class GetChatMember(BaseClient): From e203a5500f65c52c46c551fea5232092a888f9c5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 18 Dec 2018 10:07:40 +0100 Subject: [PATCH 48/62] Don't treat messages containing web page previews as media --- pyrogram/client/types/messages_and_media/message.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index c0c7edd1..00567491 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -447,6 +447,7 @@ class Message(PyrogramType): document = pyrogram.Document.parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): web_page = True + media = None else: media = None From c9bcf93cf7ddf8667427ab5cacd00a787152598e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 23:42:31 +0100 Subject: [PATCH 49/62] Add extra checks in case sign-in or sign-up fails --- pyrogram/client/client.py | 40 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index bd132851..1560da9d 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -45,7 +45,7 @@ from pyrogram.api.errors import ( PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty, PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded, PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned, - VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate) + VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate, PhoneNumberOccupied) from pyrogram.client.handlers import DisconnectHandler from pyrogram.client.handlers.handler import Handler from pyrogram.crypto import AES @@ -529,13 +529,18 @@ class Client(Methods, BaseClient): try: if phone_registered: - r = self.send( - functions.auth.SignIn( - self.phone_number, - phone_code_hash, - self.phone_code + try: + r = self.send( + functions.auth.SignIn( + self.phone_number, + phone_code_hash, + self.phone_code + ) ) - ) + except PhoneNumberUnoccupied: + print("PHONE NON-OCCUPIED") + phone_registered = False + continue else: try: self.send( @@ -551,15 +556,20 @@ class Client(Methods, BaseClient): self.first_name = self.first_name if self.first_name is not None else input("First name: ") self.last_name = self.last_name if self.last_name is not None else input("Last name: ") - r = self.send( - functions.auth.SignUp( - self.phone_number, - phone_code_hash, - self.phone_code, - self.first_name, - self.last_name + try: + r = self.send( + functions.auth.SignUp( + self.phone_number, + phone_code_hash, + self.phone_code, + self.first_name, + self.last_name + ) ) - ) + except PhoneNumberOccupied: + print("PHONE OCCUPIED") + phone_registered = True + continue except (PhoneCodeInvalid, PhoneCodeEmpty, PhoneCodeExpired, PhoneCodeHashEmpty) as e: if phone_code_invalid_raises: raise From bc824f738c370c463c01903a7b495c13d1ddca0c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 18 Dec 2018 11:21:37 +0100 Subject: [PATCH 50/62] Add extra warnings when sign-ins or sign-ups fail --- pyrogram/client/client.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 1560da9d..16f015d9 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -538,21 +538,10 @@ class Client(Methods, BaseClient): ) ) except PhoneNumberUnoccupied: - print("PHONE NON-OCCUPIED") + log.warning("Phone number unregistered") phone_registered = False continue else: - try: - self.send( - functions.auth.SignIn( - self.phone_number, - phone_code_hash, - self.phone_code - ) - ) - except PhoneNumberUnoccupied: - pass - self.first_name = self.first_name if self.first_name is not None else input("First name: ") self.last_name = self.last_name if self.last_name is not None else input("Last name: ") @@ -567,7 +556,7 @@ class Client(Methods, BaseClient): ) ) except PhoneNumberOccupied: - print("PHONE OCCUPIED") + log.warning("Phone number already registered") phone_registered = True continue except (PhoneCodeInvalid, PhoneCodeEmpty, PhoneCodeExpired, PhoneCodeHashEmpty) as e: From 510b2456b6799f25aaac9cab3d25d5b37160b303 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 10:27:47 +0100 Subject: [PATCH 51/62] Reword offset_dialog docstrings in Dialog --- pyrogram/client/methods/chats/get_dialogs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index 655bb431..f8bed7c8 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -32,7 +32,7 @@ class GetDialogs(BaseClient): Args: offset_dialog (:obj:`Dialog`): - Pass the last dialog object to retrieve the next dialogs chunk starting it. + Sequential Dialog of the first dialog to be returned. Defaults to None (start from the beginning). limit (``str``, *optional*): From a13707a3b5d75ade4dc5e7d0654f48e50a1b9cdd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 10:28:47 +0100 Subject: [PATCH 52/62] Completely remove obsolete commented code --- compiler/api/compiler.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index ac507537..d31353c6 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -475,40 +475,6 @@ def start(): f.write("\n 0x3072cfa1: \"pyrogram.api.core.GzipPacked\",") f.write("\n 0x5bb8e511: \"pyrogram.api.core.Message\",") - # TODO Remove completely - # f.write("\n 0xb0700000: \"pyrogram.client.types.Update\",") - # f.write("\n 0xb0700001: \"pyrogram.client.types.User\",") - # f.write("\n 0xb0700002: \"pyrogram.client.types.Chat\",") - # f.write("\n 0xb0700003: \"pyrogram.client.types.Message\",") - # f.write("\n 0xb0700004: \"pyrogram.client.types.MessageEntity\",") - # f.write("\n 0xb0700005: \"pyrogram.client.types.PhotoSize\",") - # f.write("\n 0xb0700006: \"pyrogram.client.types.Audio\",") - # f.write("\n 0xb0700007: \"pyrogram.client.types.Document\",") - # f.write("\n 0xb0700008: \"pyrogram.client.types.Video\",") - # f.write("\n 0xb0700009: \"pyrogram.client.types.Voice\",") - # f.write("\n 0xb0700010: \"pyrogram.client.types.VideoNote\",") - # f.write("\n 0xb0700011: \"pyrogram.client.types.Contact\",") - # f.write("\n 0xb0700012: \"pyrogram.client.types.Location\",") - # f.write("\n 0xb0700013: \"pyrogram.client.types.Venue\",") - # f.write("\n 0xb0700014: \"pyrogram.client.types.UserProfilePhotos\",") - # f.write("\n 0xb0700015: \"pyrogram.client.types.ChatPhoto\",") - # f.write("\n 0xb0700016: \"pyrogram.client.types.ChatMember\",") - # f.write("\n 0xb0700017: \"pyrogram.client.types.Sticker\",") - # f.write("\n 0xb0700018: \"pyrogram.client.types.bots.ForceReply\",") - # f.write("\n 0xb0700019: \"pyrogram.client.types.bots.InlineKeyboardButton\",") - # f.write("\n 0xb0700020: \"pyrogram.client.types.bots.InlineKeyboardMarkup\",") - # f.write("\n 0xb0700021: \"pyrogram.client.types.bots.KeyboardButton\",") - # f.write("\n 0xb0700022: \"pyrogram.client.types.bots.ReplyKeyboardMarkup\",") - # f.write("\n 0xb0700023: \"pyrogram.client.types.bots.ReplyKeyboardRemove\",") - # f.write("\n 0xb0700024: \"pyrogram.client.types.CallbackQuery\",") - # f.write("\n 0xb0700025: \"pyrogram.client.types.Animation\",") - # f.write("\n 0xb0700026: \"pyrogram.client.types.Messages\",") - # f.write("\n 0xb0700027: \"pyrogram.client.types.Photo\",") - # f.write("\n 0xb0700028: \"pyrogram.client.types.Dialog\",") - # f.write("\n 0xb0700029: \"pyrogram.client.types.Dialogs\",") - # f.write("\n 0xb0700030: \"pyrogram.client.types.ChatMembers\",") - # f.write("\n 0xb0700031: \"pyrogram.client.types.UserStatus\"") - f.write("\n}\n") for k, v in namespaces.items(): From 26ef8ea9534c9f9ca8d8763dd268fefeb75e38b1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 10:59:24 +0100 Subject: [PATCH 53/62] Revert to the old style --- pyrogram/client/types/bots/callback_query.py | 12 +++- pyrogram/client/types/bots/force_reply.py | 3 +- .../types/bots/inline_keyboard_button.py | 8 ++- .../types/bots/inline_keyboard_markup.py | 3 +- pyrogram/client/types/bots/keyboard_button.py | 5 +- .../types/bots/reply_keyboard_markup.py | 5 +- .../types/bots/reply_keyboard_remove.py | 3 +- .../client/types/input_media/input_media.py | 5 +- .../input_media/input_media_animation.py | 10 ++- .../types/input_media/input_media_audio.py | 10 ++- .../types/input_media/input_media_document.py | 6 +- .../types/input_media/input_media_photo.py | 5 +- .../types/input_media/input_media_video.py | 11 +++- .../types/input_media/input_phone_contact.py | 10 ++- .../types/messages_and_media/animation.py | 14 +++- .../client/types/messages_and_media/audio.py | 13 +++- .../types/messages_and_media/contact.py | 8 ++- .../types/messages_and_media/document.py | 11 +++- .../types/messages_and_media/location.py | 2 +- .../types/messages_and_media/message.py | 65 +++++++++++++++---- .../messages_and_media/message_entity.py | 9 ++- .../types/messages_and_media/messages.py | 6 +- .../client/types/messages_and_media/photo.py | 7 +- .../types/messages_and_media/photo_size.py | 8 ++- .../types/messages_and_media/sticker.py | 17 ++++- .../messages_and_media/user_profile_photos.py | 6 +- .../client/types/messages_and_media/venue.py | 8 ++- .../client/types/messages_and_media/video.py | 14 +++- .../types/messages_and_media/video_note.py | 12 +++- .../client/types/messages_and_media/voice.py | 11 +++- pyrogram/client/types/user_and_chats/chat.py | 22 +++++-- .../types/user_and_chats/chat_member.py | 23 +++++-- .../types/user_and_chats/chat_members.py | 6 +- .../client/types/user_and_chats/chat_photo.py | 6 +- .../client/types/user_and_chats/dialog.py | 11 +++- .../client/types/user_and_chats/dialogs.py | 6 +- pyrogram/client/types/user_and_chats/user.py | 19 +++++- .../types/user_and_chats/user_status.py | 12 +++- 38 files changed, 338 insertions(+), 74 deletions(-) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index e586c736..c6c3ee66 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -56,8 +56,16 @@ class CallbackQuery(PyrogramType): """ - def __init__(self, *, client, id: str, from_user, chat_instance: str, message=None, - inline_message_id: str = None, data: bytes = None, game_short_name: str = None): + def __init__(self, + *, + client, + id: str, + from_user, + chat_instance: str, + message=None, + inline_message_id: str = None, + data: bytes = None, + game_short_name: str = None): super().__init__(client) self.id = id diff --git a/pyrogram/client/types/bots/force_reply.py b/pyrogram/client/types/bots/force_reply.py index eb3a8729..0c15b92a 100644 --- a/pyrogram/client/types/bots/force_reply.py +++ b/pyrogram/client/types/bots/force_reply.py @@ -33,7 +33,8 @@ class ForceReply(PyrogramType): 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. """ - def __init__(self, selective: bool = None): + def __init__(self, + selective: bool = None): super().__init__(None) self.selective = selective diff --git a/pyrogram/client/types/bots/inline_keyboard_button.py b/pyrogram/client/types/bots/inline_keyboard_button.py index 0fd1355a..655e78f4 100644 --- a/pyrogram/client/types/bots/inline_keyboard_button.py +++ b/pyrogram/client/types/bots/inline_keyboard_button.py @@ -53,8 +53,12 @@ class InlineKeyboardButton(PyrogramType): # TODO: Add callback_game and pay fields - def __init__(self, text: str, callback_data: bytes = None, url: str = None, - switch_inline_query: str = None, switch_inline_query_current_chat: str = None): + def __init__(self, + text: str, + callback_data: bytes = None, + url: str = None, + switch_inline_query: str = None, + switch_inline_query_current_chat: str = None): super().__init__(None) self.text = text diff --git a/pyrogram/client/types/bots/inline_keyboard_markup.py b/pyrogram/client/types/bots/inline_keyboard_markup.py index b4eb936f..c13cc096 100644 --- a/pyrogram/client/types/bots/inline_keyboard_markup.py +++ b/pyrogram/client/types/bots/inline_keyboard_markup.py @@ -29,7 +29,8 @@ class InlineKeyboardMarkup(PyrogramType): List of button rows, each represented by a List of InlineKeyboardButton objects. """ - def __init__(self, inline_keyboard: list): + def __init__(self, + inline_keyboard: list): super().__init__(None) self.inline_keyboard = inline_keyboard diff --git a/pyrogram/client/types/bots/keyboard_button.py b/pyrogram/client/types/bots/keyboard_button.py index ceb7a2b7..4b025375 100644 --- a/pyrogram/client/types/bots/keyboard_button.py +++ b/pyrogram/client/types/bots/keyboard_button.py @@ -40,7 +40,10 @@ class KeyboardButton(PyrogramType): Available in private chats only. """ - def __init__(self, text: str, request_contact: bool = None, request_location: bool = None): + def __init__(self, + text: str, + request_contact: bool = None, + request_location: bool = None): super().__init__(None) self.text = text diff --git a/pyrogram/client/types/bots/reply_keyboard_markup.py b/pyrogram/client/types/bots/reply_keyboard_markup.py index fb0817ac..a8378614 100644 --- a/pyrogram/client/types/bots/reply_keyboard_markup.py +++ b/pyrogram/client/types/bots/reply_keyboard_markup.py @@ -47,7 +47,10 @@ class ReplyKeyboardMarkup(PyrogramType): select the new language. Other users in the group don't see the keyboard. """ - def __init__(self, keyboard: list, resize_keyboard: bool = None, one_time_keyboard: bool = None, + def __init__(self, + keyboard: list, + resize_keyboard: bool = None, + one_time_keyboard: bool = None, selective: bool = None): super().__init__(None) diff --git a/pyrogram/client/types/bots/reply_keyboard_remove.py b/pyrogram/client/types/bots/reply_keyboard_remove.py index 7f82fd6e..def9917c 100644 --- a/pyrogram/client/types/bots/reply_keyboard_remove.py +++ b/pyrogram/client/types/bots/reply_keyboard_remove.py @@ -35,7 +35,8 @@ class ReplyKeyboardRemove(PyrogramType): keyboard for that user, while still showing the keyboard with poll options to users who haven't voted yet. """ - def __init__(self, selective: bool = None): + def __init__(self, + selective: bool = None): super().__init__(None) self.selective = selective diff --git a/pyrogram/client/types/input_media/input_media.py b/pyrogram/client/types/input_media/input_media.py index 611d5865..7a380f89 100644 --- a/pyrogram/client/types/input_media/input_media.py +++ b/pyrogram/client/types/input_media/input_media.py @@ -18,7 +18,10 @@ class InputMedia: - def __init__(self, media: str, caption: str, parse_mode: str): + def __init__(self, + media: str, + caption: str, + parse_mode: str): self.media = media self.caption = caption self.parse_mode = parse_mode diff --git a/pyrogram/client/types/input_media/input_media_animation.py b/pyrogram/client/types/input_media/input_media_animation.py index cc3d9ad0..0e7b2433 100644 --- a/pyrogram/client/types/input_media/input_media_animation.py +++ b/pyrogram/client/types/input_media/input_media_animation.py @@ -52,8 +52,14 @@ class InputMediaAnimation(InputMedia): Animation duration. """ - def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", width: int = 0, - height: int = 0, duration: int = 0): + def __init__(self, + media: str, + thumb: str = None, + caption: str = "", + parse_mode: str = "", + width: int = 0, + height: int = 0, + duration: int = 0): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_audio.py b/pyrogram/client/types/input_media/input_media_audio.py index 7c1b6072..455c2292 100644 --- a/pyrogram/client/types/input_media/input_media_audio.py +++ b/pyrogram/client/types/input_media/input_media_audio.py @@ -53,8 +53,14 @@ class InputMediaAudio(InputMedia): Title of the audio """ - def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", duration: int = 0, - performer: int = "", title: str = ""): + def __init__(self, + media: str, + thumb: str = None, + caption: str = "", + parse_mode: str = "", + duration: int = 0, + performer: int = "", + title: str = ""): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_document.py b/pyrogram/client/types/input_media/input_media_document.py index 267abbde..08fcae5b 100644 --- a/pyrogram/client/types/input_media/input_media_document.py +++ b/pyrogram/client/types/input_media/input_media_document.py @@ -43,7 +43,11 @@ class InputMediaDocument(InputMedia): Defaults to Markdown. """ - def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = ""): + def __init__(self, + media: str, + thumb: str = None, + caption: str = "", + parse_mode: str = ""): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_photo.py b/pyrogram/client/types/input_media/input_media_photo.py index f5af9c89..c8cdccb8 100644 --- a/pyrogram/client/types/input_media/input_media_photo.py +++ b/pyrogram/client/types/input_media/input_media_photo.py @@ -39,5 +39,8 @@ class InputMediaPhoto(InputMedia): Defaults to Markdown. """ - def __init__(self, media: str, caption: str = "", parse_mode: str = ""): + def __init__(self, + media: str, + caption: str = "", + parse_mode: str = ""): super().__init__(media, caption, parse_mode) diff --git a/pyrogram/client/types/input_media/input_media_video.py b/pyrogram/client/types/input_media/input_media_video.py index 1884ea8d..955cf633 100644 --- a/pyrogram/client/types/input_media/input_media_video.py +++ b/pyrogram/client/types/input_media/input_media_video.py @@ -57,8 +57,15 @@ class InputMediaVideo(InputMedia): Pass True, if the uploaded video is suitable for streaming. """ - def __init__(self, media: str, thumb: str = None, caption: str = "", parse_mode: str = "", width: int = 0, - height: int = 0, duration: int = 0, supports_streaming: bool = True): + def __init__(self, + media: str, + thumb: str = None, + caption: str = "", + parse_mode: str = "", + width: int = 0, + height: int = 0, + duration: int = 0, + supports_streaming: bool = True): super().__init__(media, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_phone_contact.py b/pyrogram/client/types/input_media/input_phone_contact.py index 0e61c006..eacecaf8 100644 --- a/pyrogram/client/types/input_media/input_phone_contact.py +++ b/pyrogram/client/types/input_media/input_phone_contact.py @@ -35,10 +35,16 @@ class InputPhoneContact: Contact's last name """ - def __init__(self, phone: str, first_name: str, last_name: str = ""): + def __init__(self, + phone: str, + first_name: str, + last_name: str = ""): pass - def __new__(cls, phone: str, first_name: str, last_name: str = ""): + def __new__(cls, + phone: str, + first_name: str, + last_name: str = ""): return RawInputPhoneContact( client_id=MsgId(), phone="+" + phone.strip("+"), diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 45beefc3..9f1e5734 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -56,8 +56,18 @@ class Animation(PyrogramType): Date the animation was sent in Unix time. """ - def __init__(self, *, client, file_id: str, width: int, height: int, duration: int, thumb=None, - file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): + def __init__(self, + *, + client, + file_id: str, + width: int, + height: int, + duration: int, + thumb=None, + file_name: str = None, + mime_type: str = None, + file_size: int = None, + date: int = None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 9d001615..dfcfd9fa 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -56,8 +56,17 @@ class Audio(PyrogramType): Title of the audio as defined by sender or by audio tags. """ - def __init__(self, *, client, file_id: str, duration: int, thumb=None, file_name: str = None, - mime_type: str = None, file_size: int = None, date: int = None, performer: str = None, + def __init__(self, + *, + client, + file_id: str, + duration: int, + thumb=None, + file_name: str = None, + mime_type: str = None, + file_size: int = None, + date: int = None, + performer: str = None, title: str = None): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 2195df45..bcff7961 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -40,7 +40,13 @@ class Contact(PyrogramType): Additional data about the contact in the form of a vCard. """ - def __init__(self, *, client, phone_number: str, first_name: str, last_name: str = None, user_id: int = None, + def __init__(self, + *, + client, + phone_number: str, + first_name: str, + last_name: str = None, + user_id: int = None, vcard: str = None): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 6d124e2a..dd95846a 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -47,8 +47,15 @@ class Document(PyrogramType): Date the document was sent in Unix time. """ - def __init__(self, *, client, file_id: str, thumb=None, file_name: str = None, mime_type: str = None, - file_size: int = None, date: int = None): + def __init__(self, + *, + client, + file_id: str, + thumb=None, + file_name: str = None, + mime_type: str = None, + file_size: int = None, + date: int = None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 3f5bb059..82eb3d1d 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -31,7 +31,7 @@ class Location(PyrogramType): Latitude as defined by sender. """ - def __init__(self, *, client, longitude: float, latitude: float, ): + def __init__(self, *, client, longitude: float, latitude: float): super().__init__(client) self.longitude = longitude diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 00567491..e4b15357 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -224,18 +224,59 @@ class Message(PyrogramType): # TODO: Add game missing field. Also invoice, successful_payment, connected_website - def __init__(self, *, client, message_id: int, date: int = None, chat=None, from_user=None, forward_from=None, - forward_from_chat=None, forward_from_message_id: int = None, forward_signature: str = None, - forward_date: int = None, reply_to_message=None, mentioned=None, empty=None, service=None, media=None, - edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, - entities: list = None, caption_entities: list = None, audio=None, document=None, photo=None, - sticker=None, animation=None, video=None, voice=None, video_note=None, caption: str = None, - contact=None, location=None, venue=None, web_page=None, new_chat_members: list = None, - left_chat_member=None, new_chat_title: str = None, new_chat_photo=None, delete_chat_photo: bool = None, - group_chat_created: bool = None, supergroup_chat_created: bool = None, - channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, - pinned_message=None, views: int = None, via_bot=None, outgoing: bool = None, matches: list = None, - command: list = None, reply_markup=None): + def __init__(self, + *, + client, + message_id: int, + date: int = None, + chat=None, + from_user=None, + forward_from=None, + forward_from_chat=None, + forward_from_message_id: int = None, + forward_signature: str = None, + forward_date: int = None, + reply_to_message=None, + mentioned=None, + empty=None, + service=None, + media=None, + edit_date: int = None, + media_group_id: str = None, + author_signature: str = None, + text: str = None, + entities: list = None, + caption_entities: list = None, + audio=None, + document=None, + photo=None, + sticker=None, + animation=None, + video=None, + voice=None, + video_note=None, + caption: str = None, + contact=None, + location=None, + venue=None, + web_page=None, + new_chat_members: list = None, + left_chat_member=None, + new_chat_title: str = None, + new_chat_photo=None, + delete_chat_photo: bool = None, + group_chat_created: bool = None, + supergroup_chat_created: bool = None, + channel_chat_created: bool = None, + migrate_to_chat_id: int = None, + migrate_from_chat_id: int = None, + pinned_message=None, + views: int = None, + via_bot=None, + outgoing: bool = None, + matches: list = None, + command: list = None, + reply_markup=None): super().__init__(client) self.message_id = message_id diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 9f894f75..325006af 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -61,7 +61,14 @@ class MessageEntity(PyrogramType): types.MessageEntityPhone.ID: "phone_number" } - def __init__(self, *, client, type: str, offset: int, length: int, url: str = None, user=None): + def __init__(self, + *, + client, + type: str, + offset: int, + length: int, + url: str = None, + user=None): super().__init__(client) self.type = type diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 6d2d666a..da1f595c 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -33,7 +33,11 @@ class Messages(PyrogramType): Requested messages. """ - def __init__(self, *, client, total_count: int, messages: list): + def __init__(self, + *, + client, + total_count: int, + messages: list): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 01edd0fc..61ae7b53 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -39,7 +39,12 @@ class Photo(PyrogramType): Available sizes of this photo. """ - def __init__(self, *, client, id: str, date: int, sizes: list): + def __init__(self, + *, + client, + id: str, + date: int, + sizes: list): super().__init__(client) self.id = id diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index efc9def9..f6a8cb76 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -40,7 +40,13 @@ class PhotoSize(PyrogramType): File size. """ - def __init__(self, *, client, file_id: str, width: int, height: int, file_size: int): + def __init__(self, + *, + client, + file_id: str, + width: int, + height: int, + file_size: int): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index 9892694a..16228d0e 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -63,9 +63,20 @@ class Sticker(PyrogramType): # TODO: Add mask position - def __init__(self, *, client, file_id: str, width: int, height: int, thumb=None, file_name: str = None, - mime_type: str = None, file_size: int = None, date: int = None, emoji: str = None, - set_name: str = None, mask_position=None): + def __init__(self, + *, + client, + file_id: str, + width: int, + height: int, + thumb=None, + file_name: str = None, + mime_type: str = None, + file_size: int = None, + date: int = None, + emoji: str = None, + set_name: str = None, + mask_position=None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index 23b3200c..9f0998b1 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -31,7 +31,11 @@ class UserProfilePhotos(PyrogramType): Requested profile pictures. """ - def __init__(self, *, client, total_count: int, photos: list): + def __init__(self, + *, + client, + total_count: int, + photos: list): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index c206a2a0..a1fb372e 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -43,7 +43,13 @@ class Venue(PyrogramType): """ - def __init__(self, *, client, location, title: str, address: str, foursquare_id: str = None, + def __init__(self, + *, + client, + location, + title: str, + address: str, + foursquare_id: str = None, foursquare_type: str = None): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 7d995ea8..39b29e64 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -56,8 +56,18 @@ class Video(PyrogramType): Date the video was sent in Unix time. """ - def __init__(self, *, client, file_id: str, width: int, height: int, duration: int, thumb=None, - file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None): + def __init__(self, + *, + client, + file_id: str, + width: int, + height: int, + duration: int, + thumb=None, + file_name: str = None, + mime_type: str = None, + file_size: int = None, + date: int = None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 688bca19..103bab76 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -50,8 +50,16 @@ class VideoNote(PyrogramType): Date the video note was sent in Unix time. """ - def __init__(self, *, client, file_id: str, length: int, duration: int, thumb=None, mime_type: str = None, - file_size: int = None, date: int = None): + def __init__(self, + *, + client, + file_id: str, + length: int, + duration: int, + thumb=None, + mime_type: str = None, + file_size: int = None, + date: int = None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index cce70a31..e3ebd2e2 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -46,8 +46,15 @@ class Voice(PyrogramType): Date the voice was sent in Unix time. """ - def __init__(self, *, client, file_id: str, duration: int, waveform: bytes = None, mime_type: str = None, - file_size: int = None, date: int = None): + def __init__(self, + *, + client, + file_id: str, + duration: int, + waveform: bytes = None, + mime_type: str = None, + file_size: int = None, + date: int = None): super().__init__(client) self.file_id = file_id diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index b9d0d796..ddaa59dd 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -76,10 +76,24 @@ class Chat(PyrogramType): The reason why this chat might be unavailable to some users. """ - def __init__(self, *, client, id: int, type: str, title: str = None, username: str = None, - first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, photo=None, - description: str = None, invite_link: str = None, pinned_message=None, sticker_set_name: str = None, - can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None): + def __init__(self, + *, + client, + id: int, + type: str, + title: str = None, + username: str = None, + first_name: str = None, + last_name: str = None, + all_members_are_administrators: bool = None, + photo=None, + description: str = None, + invite_link: str = None, + pinned_message=None, + sticker_set_name: str = None, + can_set_sticker_set: bool = None, + members_count: int = None, + restriction_reason: str = None): super().__init__(client) self.id = id diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 27eeb897..50f44bad 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -79,11 +79,24 @@ class ChatMember(PyrogramType): Restricted only. True, if user may add web page previews to his messages, implies can_send_media_messages. """ - def __init__(self, *, client, user, status: str, until_date: int = None, can_be_edited: bool = None, - can_change_info: bool = None, can_post_messages: bool = None, can_edit_messages: bool = None, - can_delete_messages: bool = None, can_invite_users: bool = None, can_restrict_members: bool = None, - can_pin_messages: bool = None, can_promote_members: bool = None, can_send_messages: bool = None, - can_send_media_messages: bool = None, can_send_other_messages: bool = None, + def __init__(self, + *, + client, + user, + status: str, + until_date: int = None, + can_be_edited: bool = None, + can_change_info: bool = None, + can_post_messages: bool = None, + can_edit_messages: bool = None, + can_delete_messages: bool = None, + can_invite_users: bool = None, + can_restrict_members: bool = None, + can_pin_messages: bool = None, + can_promote_members: bool = None, + can_send_messages: bool = None, + can_send_media_messages: bool = None, + can_send_other_messages: bool = None, can_add_web_page_previews: bool = None): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index ba28b23d..5fcddecf 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -33,7 +33,11 @@ class ChatMembers(PyrogramType): Requested chat members. """ - def __init__(self, *, client, total_count: int, chat_members: list): + def __init__(self, + *, + client, + total_count: int, + chat_members: list): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index 91c77b06..b0e630a4 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -34,7 +34,11 @@ class ChatPhoto(PyrogramType): Unique file identifier of big (640x640) chat photo. This file_id can be used only for photo download. """ - def __init__(self, *, client, small_file_id: str, big_file_id: str): + def __init__(self, + *, + client, + small_file_id: str, + big_file_id: str): super().__init__(client) self.small_file_id = small_file_id diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index d617c11e..7b1d66b4 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -44,8 +44,15 @@ class Dialog(PyrogramType): True, if the dialog is pinned. """ - def __init__(self, *, client, chat, top_message, unread_messages_count: int, unread_mentions_count: int, - unread_mark: bool, is_pinned: bool): + def __init__(self, + *, + client, + chat, + top_message, + unread_messages_count: int, + unread_mentions_count: int, + unread_mark: bool, + is_pinned: bool): super().__init__(client) self.chat = chat diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 21eda2f0..e1e8c5e2 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -33,7 +33,11 @@ class Dialogs(PyrogramType): Requested dialogs. """ - def __init__(self, *, client, total_count: int, dialogs: list): + def __init__(self, + *, + client, + total_count: int, + dialogs: list): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 66d68121..4755e297 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -69,9 +69,22 @@ class User(PyrogramType): The reason why this bot might be unavailable to some users. """ - def __init__(self, *, client, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, - is_deleted: bool, is_bot: bool, first_name: str, last_name: str = None, status=None, - username: str = None, language_code: str = None, phone_number: str = None, photo=None, + def __init__(self, + *, + client, + id: int, + is_self: bool, + is_contact: bool, + is_mutual_contact: bool, + is_deleted: bool, + is_bot: bool, + first_name: str, + last_name: str = None, + status=None, + username: str = None, + language_code: str = None, + phone_number: str = None, + photo=None, restriction_reason: str = None): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 8102ffe0..bcb4d195 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -62,8 +62,16 @@ class UserStatus(PyrogramType): always shown to blocked users), None otherwise. """ - def __init__(self, *, client, user_id: int, online: bool = None, offline: bool = None, date: int = None, - recently: bool = None, within_week: bool = None, within_month: bool = None, + def __init__(self, + *, + client, + user_id: int, + online: bool = None, + offline: bool = None, + date: int = None, + recently: bool = None, + within_week: bool = None, + within_month: bool = None, long_time_ago: bool = None): super().__init__(client) From a8599efa9481e710f1a65729a9e844cdd0bbe7b1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 12:29:55 +0100 Subject: [PATCH 54/62] Add backported typing package as requirement To be removed when Pyrogram drops Python 3.4 support --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 17447fdf..8f1eea95 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pyaes==1.6.1 pysocks==1.6.8 +typing==3.6.6 \ No newline at end of file From b79f395d372620357216bcd3a33c2cf7e6deb2ef Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 12:30:25 +0100 Subject: [PATCH 55/62] Type hint Message --- .../types/messages_and_media/message.py | 75 ++++++++++--------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index e4b15357..61121b79 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List, Match, Union + import pyrogram from pyrogram.api import types from pyrogram.api.errors import MessageIdsEmpty @@ -201,18 +203,18 @@ class Message(PyrogramType): via_bot (:obj:`User `): The information of the bot that generated the message from an inline query of a user. - + outgoing (``bool``, *optional*): Whether the message is incoming or outgoing. Messages received from other chats are incoming (*outgoing* is False). Messages sent from yourself to other chats are outgoing (*outgoing* is True). An exception is made for your own personal chat; messages sent there will be incoming. - matches (``list``, *optional*): + matches (``List of regex Matches``, *optional*): A list containing all `Match Objects `_ that match the text of this message. Only applicable when using :obj:`Filters.regex `. - command (``list``, *optional*): + command (``List of strings``, *optional*): A list containing the command and its arguments, if any. E.g.: "/start 1 2 3" would produce ["start", "1", "2", "3"]. Only applicable when using :obj:`Filters.command `. @@ -226,57 +228,60 @@ class Message(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", message_id: int, date: int = None, - chat=None, - from_user=None, - forward_from=None, - forward_from_chat=None, + chat: Chat = None, + from_user: User = None, + forward_from: User = None, + forward_from_chat: Chat = None, forward_from_message_id: int = None, forward_signature: str = None, forward_date: int = None, - reply_to_message=None, - mentioned=None, - empty=None, - service=None, - media=None, + reply_to_message: "Message" = None, + mentioned: bool = None, + empty: bool = None, + service: bool = None, + media: bool = None, edit_date: int = None, media_group_id: str = None, author_signature: str = None, text: str = None, - entities: list = None, - caption_entities: list = None, - audio=None, - document=None, - photo=None, - sticker=None, - animation=None, - video=None, - voice=None, - video_note=None, + entities: List["pyrogram.MessageEntity"] = None, + caption_entities: List["pyrogram.MessageEntity"] = None, + audio: "pyrogram.Audio" = None, + document: "pyrogram.Document" = None, + photo: "pyrogram.Photo" = None, + sticker: "pyrogram.Sticker" = None, + animation: "pyrogram.Animation" = None, + video: "pyrogram.Video" = None, + voice: "pyrogram.Voice" = None, + video_note: "pyrogram.VideoNote" = None, caption: str = None, - contact=None, - location=None, - venue=None, - web_page=None, - new_chat_members: list = None, - left_chat_member=None, + contact: "pyrogram.Contact" = None, + location: "pyrogram.Location" = None, + venue: "pyrogram.Venue" = None, + web_page: bool = None, + new_chat_members: List[User] = None, + left_chat_member: User = None, new_chat_title: str = None, - new_chat_photo=None, + new_chat_photo: "pyrogram.Photo" = None, delete_chat_photo: bool = None, group_chat_created: bool = None, supergroup_chat_created: bool = None, channel_chat_created: bool = None, migrate_to_chat_id: int = None, migrate_from_chat_id: int = None, - pinned_message=None, + pinned_message: "Message" = None, views: int = None, - via_bot=None, + via_bot: User = None, outgoing: bool = None, - matches: list = None, - command: list = None, - reply_markup=None): + matches: List[Match] = None, + command: List[str] = None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None): super().__init__(client) self.message_id = message_id From e8fbae31668f343fd64441e78cf8f00dc735498e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 13:00:33 +0100 Subject: [PATCH 56/62] Make all parse methods protected to hide them from the lib user --- pyrogram/client/dispatcher/dispatcher.py | 8 ++-- pyrogram/client/types/bots/callback_query.py | 4 +- .../types/messages_and_media/animation.py | 4 +- .../client/types/messages_and_media/audio.py | 4 +- .../types/messages_and_media/contact.py | 2 +- .../types/messages_and_media/document.py | 4 +- .../types/messages_and_media/location.py | 2 +- .../types/messages_and_media/message.py | 46 +++++++++---------- .../messages_and_media/message_entity.py | 4 +- .../types/messages_and_media/messages.py | 6 +-- .../client/types/messages_and_media/photo.py | 2 +- .../types/messages_and_media/photo_size.py | 2 +- .../types/messages_and_media/sticker.py | 4 +- .../messages_and_media/user_profile_photos.py | 4 +- .../client/types/messages_and_media/venue.py | 4 +- .../client/types/messages_and_media/video.py | 4 +- .../types/messages_and_media/video_note.py | 4 +- .../client/types/messages_and_media/voice.py | 2 +- pyrogram/client/types/user_and_chats/chat.py | 36 +++++++-------- .../types/user_and_chats/chat_member.py | 2 +- .../types/user_and_chats/chat_members.py | 6 +-- .../client/types/user_and_chats/chat_photo.py | 2 +- .../client/types/user_and_chats/dialog.py | 4 +- .../client/types/user_and_chats/dialogs.py | 6 +-- pyrogram/client/types/user_and_chats/user.py | 6 +-- .../types/user_and_chats/user_status.py | 2 +- 26 files changed, 87 insertions(+), 87 deletions(-) diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index 4f3ae620..5b30daf9 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -62,17 +62,17 @@ class Dispatcher: self.update_parsers = { Dispatcher.MESSAGE_UPDATES: - lambda upd, usr, cht: (pyrogram.Message.parse(self.client, upd.message, usr, cht), MessageHandler), + lambda upd, usr, cht: (pyrogram.Message._parse(self.client, upd.message, usr, cht), MessageHandler), Dispatcher.DELETE_MESSAGES_UPDATES: - lambda upd, usr, cht: (pyrogram.Messages.parse_deleted(self.client, upd), DeletedMessagesHandler), + lambda upd, usr, cht: (pyrogram.Messages._parse_deleted(self.client, upd), DeletedMessagesHandler), Dispatcher.CALLBACK_QUERY_UPDATES: - lambda upd, usr, cht: (pyrogram.CallbackQuery.parse(self.client, upd, usr), CallbackQueryHandler), + lambda upd, usr, cht: (pyrogram.CallbackQuery._parse(self.client, upd, usr), CallbackQueryHandler), (types.UpdateUserStatus,): lambda upd, usr, cht: ( - pyrogram.UserStatus.parse(self.client, upd.status, upd.user_id), UserStatusHandler + pyrogram.UserStatus._parse(self.client, upd.status, upd.user_id), UserStatusHandler ) } diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index c6c3ee66..ed96edea 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -77,7 +77,7 @@ class CallbackQuery(PyrogramType): self.game_short_name = game_short_name @staticmethod - def parse(client, callback_query, users) -> "CallbackQuery": + def _parse(client, callback_query, users) -> "CallbackQuery": message = None inline_message_id = None @@ -105,7 +105,7 @@ class CallbackQuery(PyrogramType): return CallbackQuery( id=str(callback_query.query_id), - from_user=User.parse(client, users[callback_query.user_id]), + from_user=User._parse(client, users[callback_query.user_id]), message=message, inline_message_id=inline_message_id, chat_instance=str(callback_query.chat_instance), diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 9f1e5734..ba9b4dad 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -81,7 +81,7 @@ class Animation(PyrogramType): self.duration = duration @staticmethod - def parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo, + def _parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Animation": return Animation( file_id=encode( @@ -96,7 +96,7 @@ class Animation(PyrogramType): width=getattr(video_attributes, "w", 0), height=getattr(video_attributes, "h", 0), duration=getattr(video_attributes, "duration", 0), - thumb=PhotoSize.parse(client, animation.thumb), + thumb=PhotoSize._parse(client, animation.thumb), mime_type=animation.mime_type, file_size=animation.size, file_name=file_name, diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index dfcfd9fa..0590e760 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -81,7 +81,7 @@ class Audio(PyrogramType): self.title = title @staticmethod - def parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio": + def _parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio": return Audio( file_id=encode( pack( @@ -97,7 +97,7 @@ class Audio(PyrogramType): title=audio_attributes.title, mime_type=audio.mime_type, file_size=audio.size, - thumb=PhotoSize.parse(client, audio.thumb), + thumb=PhotoSize._parse(client, audio.thumb), file_name=file_name, date=audio.date, client=client diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index bcff7961..05cbfb1d 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -57,7 +57,7 @@ class Contact(PyrogramType): self.vcard = vcard @staticmethod - def parse(client, contact: types.MessageMediaContact) -> "Contact": + def _parse(client, contact: types.MessageMediaContact) -> "Contact": return Contact( phone_number=contact.phone_number, first_name=contact.first_name, diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index dd95846a..172d816d 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -66,7 +66,7 @@ class Document(PyrogramType): self.date = date @staticmethod - def parse(client, document: types.Document, file_name: str) -> "Document": + def _parse(client, document: types.Document, file_name: str) -> "Document": return Document( file_id=encode( pack( @@ -77,7 +77,7 @@ class Document(PyrogramType): document.access_hash ) ), - thumb=PhotoSize.parse(client, document.thumb), + thumb=PhotoSize._parse(client, document.thumb), file_name=file_name, mime_type=document.mime_type, file_size=document.size, diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 82eb3d1d..455e7f58 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -38,7 +38,7 @@ class Location(PyrogramType): self.latitude = latitude @staticmethod - def parse(client, geo_point: types.GeoPoint) -> "Location": + def _parse(client, geo_point: types.GeoPoint) -> "Location": if isinstance(geo_point, types.GeoPoint): return Location( longitude=geo_point.long, diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 61121b79..14662e00 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -336,7 +336,7 @@ class Message(PyrogramType): self.reply_markup = reply_markup @staticmethod - def parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, + def _parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, replies: int = 1): if isinstance(message, types.MessageEmpty): return Message(message_id=message.id, empty=True, client=client) @@ -355,11 +355,11 @@ class Message(PyrogramType): new_chat_photo = None if isinstance(action, types.MessageActionChatAddUser): - new_chat_members = [User.parse(client, users[i]) for i in action.users] + new_chat_members = [User._parse(client, users[i]) for i in action.users] elif isinstance(action, types.MessageActionChatJoinedByLink): - new_chat_members = [User.parse(client, users[message.from_id])] + new_chat_members = [User._parse(client, users[message.from_id])] elif isinstance(action, types.MessageActionChatDeleteUser): - left_chat_member = User.parse(client, users[action.user_id]) + left_chat_member = User._parse(client, users[action.user_id]) elif isinstance(action, types.MessageActionChatEditTitle): new_chat_title = action.title elif isinstance(action, types.MessageActionChatDeletePhoto): @@ -373,13 +373,13 @@ class Message(PyrogramType): elif isinstance(action, types.MessageActionChannelCreate): channel_chat_created = True elif isinstance(action, types.MessageActionChatEditPhoto): - new_chat_photo = Photo.parse(client, action.photo) + new_chat_photo = Photo._parse(client, action.photo) parsed_message = Message( message_id=message.id, date=message.date, - chat=Chat.parse(client, message, users, chats), - from_user=User.parse(client, users.get(message.from_id, None)), + chat=Chat._parse(client, message, users, chats), + from_user=User._parse(client, users.get(message.from_id, None)), service=True, new_chat_members=new_chat_members, left_chat_member=left_chat_member, @@ -407,7 +407,7 @@ class Message(PyrogramType): return parsed_message if isinstance(message, types.Message): - entities = [MessageEntity.parse(client, entity, users) for entity in message.entities] + entities = [MessageEntity._parse(client, entity, users) for entity in message.entities] entities = list(filter(lambda x: x is not None, entities)) forward_from = None @@ -422,9 +422,9 @@ class Message(PyrogramType): forward_date = forward_header.date if forward_header.from_id: - forward_from = User.parse(client, users[forward_header.from_id]) + forward_from = User._parse(client, users[forward_header.from_id]) else: - forward_from_chat = Chat.parse_channel_chat(client, chats[forward_header.channel_id]) + forward_from_chat = Chat._parse_channel_chat(client, chats[forward_header.channel_id]) forward_from_message_id = forward_header.channel_post forward_signature = forward_header.post_author @@ -445,13 +445,13 @@ class Message(PyrogramType): if media: if isinstance(media, types.MessageMediaPhoto): - photo = Photo.parse(client, media.photo) + photo = Photo._parse(client, media.photo) elif isinstance(media, types.MessageMediaGeo): - location = Location.parse(client, media.geo) + location = Location._parse(client, media.geo) elif isinstance(media, types.MessageMediaContact): - contact = Contact.parse(client, media) + contact = Contact._parse(client, media) elif isinstance(media, types.MessageMediaVenue): - venue = pyrogram.Venue.parse(client, media) + venue = pyrogram.Venue._parse(client, media) elif isinstance(media, types.MessageMediaDocument): doc = media.document @@ -468,20 +468,20 @@ class Message(PyrogramType): audio_attributes = attributes[types.DocumentAttributeAudio] if audio_attributes.voice: - voice = pyrogram.Voice.parse(client, doc, audio_attributes) + voice = pyrogram.Voice._parse(client, doc, audio_attributes) else: - audio = pyrogram.Audio.parse(client, doc, audio_attributes, file_name) + audio = pyrogram.Audio._parse(client, doc, audio_attributes, file_name) elif types.DocumentAttributeAnimated in attributes: video_attributes = attributes.get(types.DocumentAttributeVideo, None) - animation = pyrogram.Animation.parse(client, doc, video_attributes, file_name) + animation = pyrogram.Animation._parse(client, doc, video_attributes, file_name) elif types.DocumentAttributeVideo in attributes: video_attributes = attributes[types.DocumentAttributeVideo] if video_attributes.round_message: - video_note = pyrogram.VideoNote.parse(client, doc, video_attributes) + video_note = pyrogram.VideoNote._parse(client, doc, video_attributes) else: - video = pyrogram.Video.parse(client, doc, video_attributes, file_name) + video = pyrogram.Video._parse(client, doc, video_attributes, file_name) elif types.DocumentAttributeSticker in attributes: sticker = pyrogram.Sticker.parse( client, doc, @@ -490,7 +490,7 @@ class Message(PyrogramType): file_name ) else: - document = pyrogram.Document.parse(client, doc, file_name) + document = pyrogram.Document._parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): web_page = True media = None @@ -514,8 +514,8 @@ class Message(PyrogramType): parsed_message = Message( message_id=message.id, date=message.date, - chat=Chat.parse(client, message, users, chats), - from_user=User.parse(client, users.get(message.from_id, None)), + chat=Chat._parse(client, message, users, chats), + from_user=User._parse(client, users.get(message.from_id, None)), text=Str(message.message).init(client, entities) or None if media is None else None, caption=Str(message.message).init(client, entities) or None if media is not None else None, entities=entities or None if media is None else None, @@ -543,7 +543,7 @@ class Message(PyrogramType): document=document, web_page=web_page, views=message.views, - via_bot=User.parse(client, users.get(message.via_bot_id, None)), + via_bot=User._parse(client, users.get(message.via_bot_id, None)), outgoing=message.out, reply_markup=reply_markup, client=client diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 325006af..4f625bd3 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -78,7 +78,7 @@ class MessageEntity(PyrogramType): self.user = user @staticmethod - def parse(client, entity, users: dict) -> "MessageEntity" or None: + def _parse(client, entity, users: dict) -> "MessageEntity" or None: type = MessageEntity.ENTITIES.get(entity.ID, None) if type is None: @@ -89,6 +89,6 @@ class MessageEntity(PyrogramType): offset=entity.offset, length=entity.length, url=getattr(entity, "url", None), - user=User.parse(client, users.get(getattr(entity, "user_id", None), None)), + user=User._parse(client, users.get(getattr(entity, "user_id", None), None)), client=client ) diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index da1f595c..48d6c85b 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -44,18 +44,18 @@ class Messages(PyrogramType): self.messages = messages @staticmethod - def parse(client, messages: types.messages.Messages) -> "Messages": + def _parse(client, messages: types.messages.Messages) -> "Messages": users = {i.id: i for i in messages.users} chats = {i.id: i for i in messages.chats} return Messages( total_count=getattr(messages, "count", len(messages.messages)), - messages=[Message.parse(client, message, users, chats) for message in messages.messages], + messages=[Message._parse(client, message, users, chats) for message in messages.messages], client=client ) @staticmethod - def parse_deleted(client, update) -> "Messages": + def _parse_deleted(client, update) -> "Messages": messages = update.messages channel_id = getattr(update, "channel_id", None) diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 61ae7b53..cdf3b191 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -52,7 +52,7 @@ class Photo(PyrogramType): self.sizes = sizes @staticmethod - def parse(client, photo: types.Photo): + def _parse(client, photo: types.Photo): if isinstance(photo, types.Photo): raw_sizes = photo.sizes sizes = [] diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index f6a8cb76..4fb9b30d 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -55,7 +55,7 @@ class PhotoSize(PyrogramType): self.file_size = file_size @staticmethod - def parse(client, photo_size: types.PhotoSize or types.PhotoCachedSize): + def _parse(client, photo_size: types.PhotoSize or types.PhotoCachedSize): if isinstance(photo_size, (types.PhotoSize, types.PhotoCachedSize)): if isinstance(photo_size, types.PhotoSize): diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index 16228d0e..22121dd8 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -104,7 +104,7 @@ class Sticker(PyrogramType): return None @staticmethod - def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, + def _parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": sticker_set = sticker_attributes.stickerset @@ -126,7 +126,7 @@ class Sticker(PyrogramType): ), width=image_size_attributes.w if image_size_attributes else 0, height=image_size_attributes.h if image_size_attributes else 0, - thumb=PhotoSize.parse(client, sticker.thumb), + thumb=PhotoSize._parse(client, sticker.thumb), # TODO: mask_position set_name=set_name, emoji=sticker_attributes.alt or None, diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index 9f0998b1..ca27fe2c 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -42,9 +42,9 @@ class UserProfilePhotos(PyrogramType): self.photos = photos @staticmethod - def parse(client, photos) -> "UserProfilePhotos": + def _parse(client, photos) -> "UserProfilePhotos": return UserProfilePhotos( total_count=getattr(photos, "count", len(photos.photos)), - photos=[Photo.parse(client, photo) for photo in photos.photos], + photos=[Photo._parse(client, photo) for photo in photos.photos], client=client ) diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index a1fb372e..12701d8c 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -60,9 +60,9 @@ class Venue(PyrogramType): self.foursquare_type = foursquare_type @staticmethod - def parse(client, venue: types.MessageMediaVenue): + def _parse(client, venue: types.MessageMediaVenue): return Venue( - location=Location.parse(client, venue.geo), + location=Location._parse(client, venue.geo), title=venue.title, address=venue.address, foursquare_id=venue.venue_id or None, diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 39b29e64..a69f0138 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -81,7 +81,7 @@ class Video(PyrogramType): self.duration = duration @staticmethod - def parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video": + def _parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video": return Video( file_id=encode( pack( @@ -95,7 +95,7 @@ class Video(PyrogramType): width=video_attributes.w, height=video_attributes.h, duration=video_attributes.duration, - thumb=PhotoSize.parse(client, video.thumb), + thumb=PhotoSize._parse(client, video.thumb), mime_type=video.mime_type, file_size=video.size, file_name=file_name, diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 103bab76..53419dda 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -71,7 +71,7 @@ class VideoNote(PyrogramType): self.duration = duration @staticmethod - def parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote": + def _parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote": return VideoNote( file_id=encode( pack( @@ -84,7 +84,7 @@ class VideoNote(PyrogramType): ), length=video_attributes.w, duration=video_attributes.duration, - thumb=PhotoSize.parse(client, video_note.thumb), + thumb=PhotoSize._parse(client, video_note.thumb), file_size=video_note.size, mime_type=video_note.mime_type, date=video_note.date, diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index e3ebd2e2..bf5ffaa8 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -65,7 +65,7 @@ class Voice(PyrogramType): self.date = date @staticmethod - def parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice": + def _parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice": return Voice( file_id=encode( pack( diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index ddaa59dd..4a3dfb20 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -113,20 +113,20 @@ class Chat(PyrogramType): self.restriction_reason = restriction_reason @staticmethod - def parse_user_chat(client, user: types.User) -> "Chat": + def _parse_user_chat(client, user: types.User) -> "Chat": return Chat( id=user.id, type="private", username=user.username, first_name=user.first_name, last_name=user.last_name, - photo=ChatPhoto.parse(client, user.photo), + photo=ChatPhoto._parse(client, user.photo), restriction_reason=user.restriction_reason, client=client ) @staticmethod - def parse_chat_chat(client, chat: types.Chat) -> "Chat": + def _parse_chat_chat(client, chat: types.Chat) -> "Chat": admins_enabled = getattr(chat, "admins_enabled", None) if admins_enabled is not None: @@ -137,45 +137,45 @@ class Chat(PyrogramType): type="group", title=chat.title, all_members_are_administrators=admins_enabled, - photo=ChatPhoto.parse(client, getattr(chat, "photo", None)), + photo=ChatPhoto._parse(client, getattr(chat, "photo", None)), client=client ) @staticmethod - def parse_channel_chat(client, channel: types.Channel) -> "Chat": + def _parse_channel_chat(client, channel: types.Channel) -> "Chat": return Chat( id=int("-100" + str(channel.id)), type="supergroup" if channel.megagroup else "channel", title=channel.title, username=getattr(channel, "username", None), - photo=ChatPhoto.parse(client, getattr(channel, "photo", None)), + photo=ChatPhoto._parse(client, getattr(channel, "photo", None)), restriction_reason=getattr(channel, "restriction_reason", None), client=client ) @staticmethod - def parse(client, message: types.Message or types.MessageService, users: dict, chats: dict) -> "Chat": + def _parse(client, message: types.Message or types.MessageService, users: dict, chats: dict) -> "Chat": if isinstance(message.to_id, types.PeerUser): - return Chat.parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id]) + return Chat._parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id]) if isinstance(message.to_id, types.PeerChat): - return Chat.parse_chat_chat(client, chats[message.to_id.chat_id]) + return Chat._parse_chat_chat(client, chats[message.to_id.chat_id]) - return Chat.parse_channel_chat(client, chats[message.to_id.channel_id]) + return Chat._parse_channel_chat(client, chats[message.to_id.channel_id]) @staticmethod - def parse_dialog(client, peer, users: dict, chats: dict): + def _parse_dialog(client, peer, users: dict, chats: dict): if isinstance(peer, types.PeerUser): - return Chat.parse_user_chat(client, users[peer.user_id]) + return Chat._parse_user_chat(client, users[peer.user_id]) elif isinstance(peer, types.PeerChat): - return Chat.parse_chat_chat(client, chats[peer.chat_id]) + return Chat._parse_chat_chat(client, chats[peer.chat_id]) else: - return Chat.parse_channel_chat(client, chats[peer.channel_id]) + return Chat._parse_channel_chat(client, chats[peer.channel_id]) @staticmethod - def parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat": + def _parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat": if isinstance(chat_full, types.UserFull): - parsed_chat = Chat.parse_user_chat(client, chat_full.user) + _parsed_chat = Chat.parse_user_chat(client, chat_full.user) parsed_chat.description = chat_full.about else: full_chat = chat_full.full_chat @@ -186,12 +186,12 @@ class Chat(PyrogramType): chat = i if isinstance(full_chat, types.ChatFull): - parsed_chat = Chat.parse_chat_chat(client, chat) + _parsed_chat = Chat.parse_chat_chat(client, chat) if isinstance(full_chat.participants, types.ChatParticipants): parsed_chat.members_count = len(full_chat.participants.participants) else: - parsed_chat = Chat.parse_channel_chat(client, chat) + _parsed_chat = Chat.parse_channel_chat(client, chat) parsed_chat.members_count = full_chat.participants_count parsed_chat.description = full_chat.about or None # TODO: Add StickerSet type diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 50f44bad..5fc9c988 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -118,7 +118,7 @@ class ChatMember(PyrogramType): self.can_add_web_page_previews = can_add_web_page_previews @staticmethod - def parse(client, member, user) -> "ChatMember": + def _parse(client, member, user) -> "ChatMember": if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): return ChatMember(user=user, status="member", client=client) diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index 5fcddecf..a798f312 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -44,7 +44,7 @@ class ChatMembers(PyrogramType): self.chat_members = chat_members @staticmethod - def parse(client, members): + def _parse(client, members): users = {i.id: i for i in members.users} chat_members = [] @@ -56,8 +56,8 @@ class ChatMembers(PyrogramType): total_count = len(members) for member in members: - user = User.parse(client, users[member.user_id]) - chat_members.append(ChatMember.parse(client, member, user)) + user = User._parse(client, users[member.user_id]) + chat_members.append(ChatMember._parse(client, member, user)) return ChatMembers( total_count=total_count, diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index b0e630a4..33eca8c8 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -45,7 +45,7 @@ class ChatPhoto(PyrogramType): self.big_file_id = big_file_id @staticmethod - def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): + def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)): return None diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 7b1d66b4..b5679ed2 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -63,7 +63,7 @@ class Dialog(PyrogramType): self.is_pinned = is_pinned @staticmethod - def parse(client, dialog, messages, users, chats) -> "Dialog": + def _parse(client, dialog, messages, users, chats) -> "Dialog": chat_id = dialog.peer if isinstance(chat_id, types.PeerUser): @@ -74,7 +74,7 @@ class Dialog(PyrogramType): chat_id = int("-100" + str(chat_id.channel_id)) return Dialog( - chat=Chat.parse_dialog(client, dialog.peer, users, chats), + chat=Chat._parse_dialog(client, dialog.peer, users, chats), top_message=messages.get(chat_id), unread_messages_count=dialog.unread_count, unread_mentions_count=dialog.unread_mentions_count, diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index e1e8c5e2..0668052b 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -44,7 +44,7 @@ class Dialogs(PyrogramType): self.dialogs = dialogs @staticmethod - def parse(client, dialogs) -> "Dialogs": + def _parse(client, dialogs) -> "Dialogs": users = {i.id: i for i in dialogs.users} chats = {i.id: i for i in dialogs.chats} @@ -63,10 +63,10 @@ class Dialogs(PyrogramType): else: chat_id = int("-100" + str(to_id.channel_id)) - messages[chat_id] = Message.parse(client, message, users, chats) + messages[chat_id] = Message._parse(client, message, users, chats) return Dialogs( total_count=getattr(dialogs, "count", len(dialogs.dialogs)), - dialogs=[Dialog.parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs], + dialogs=[Dialog._parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs], client=client ) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 4755e297..74fc0516 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -104,7 +104,7 @@ class User(PyrogramType): self.restriction_reason = restriction_reason @staticmethod - def parse(client, user: types.User) -> "User" or None: + def _parse(client, user: types.User) -> "User" or None: if user is None: return None @@ -117,11 +117,11 @@ class User(PyrogramType): is_bot=user.bot, first_name=user.first_name, last_name=user.last_name, - status=UserStatus.parse(client, user.status, user.id, user.bot), + status=UserStatus._parse(client, user.status, user.id, user.bot), username=user.username, language_code=user.lang_code, phone_number=user.phone, - photo=ChatPhoto.parse(client, user.photo), + photo=ChatPhoto._parse(client, user.photo), restriction_reason=user.restriction_reason, client=client ) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index bcb4d195..feda1eaa 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -85,7 +85,7 @@ class UserStatus(PyrogramType): self.long_time_ago = long_time_ago @staticmethod - def parse(client, user_status, user_id: int, is_bot: bool = False): + def _parse(client, user_status, user_id: int, is_bot: bool = False): if is_bot: return None From 5035daa9d77e19b650d9b188eb8eaa42f4577e42 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 13:29:58 +0100 Subject: [PATCH 57/62] Type hint all Pyrogram types --- pyrogram/client/types/bots/callback_query.py | 7 ++++--- pyrogram/client/types/bots/inline_keyboard_markup.py | 4 +++- pyrogram/client/types/bots/reply_keyboard_markup.py | 4 +++- .../client/types/messages_and_media/animation.py | 7 ++++--- pyrogram/client/types/messages_and_media/audio.py | 8 +++++--- pyrogram/client/types/messages_and_media/contact.py | 4 +++- pyrogram/client/types/messages_and_media/document.py | 5 +++-- pyrogram/client/types/messages_and_media/location.py | 8 +++++++- pyrogram/client/types/messages_and_media/message.py | 2 +- .../types/messages_and_media/message_entity.py | 6 ++++-- pyrogram/client/types/messages_and_media/messages.py | 7 +++++-- pyrogram/client/types/messages_and_media/photo.py | 6 ++++-- .../client/types/messages_and_media/photo_size.py | 3 ++- pyrogram/client/types/messages_and_media/sticker.py | 12 ++++++------ .../types/messages_and_media/user_profile_photos.py | 7 +++++-- pyrogram/client/types/messages_and_media/venue.py | 5 +++-- pyrogram/client/types/messages_and_media/video.py | 8 +++++--- .../client/types/messages_and_media/video_note.py | 5 +++-- pyrogram/client/types/messages_and_media/voice.py | 3 ++- pyrogram/client/types/user_and_chats/chat.py | 11 ++++++----- pyrogram/client/types/user_and_chats/chat_member.py | 6 ++++-- pyrogram/client/types/user_and_chats/chat_members.py | 7 +++++-- pyrogram/client/types/user_and_chats/chat_photo.py | 3 ++- pyrogram/client/types/user_and_chats/dialog.py | 8 +++++--- pyrogram/client/types/user_and_chats/dialogs.py | 7 +++++-- pyrogram/client/types/user_and_chats/user.py | 7 ++++--- pyrogram/client/types/user_and_chats/user_status.py | 4 +++- 27 files changed, 106 insertions(+), 58 deletions(-) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index ed96edea..5d242162 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -19,6 +19,7 @@ from base64 import b64encode from struct import pack +import pyrogram from pyrogram.api import types from ..pyrogram_type import PyrogramType from ..user_and_chats import User @@ -58,11 +59,11 @@ class CallbackQuery(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", id: str, - from_user, + from_user: User, chat_instance: str, - message=None, + message: "pyrogram.Message" = None, inline_message_id: str = None, data: bytes = None, game_short_name: str = None): diff --git a/pyrogram/client/types/bots/inline_keyboard_markup.py b/pyrogram/client/types/bots/inline_keyboard_markup.py index c13cc096..d958f306 100644 --- a/pyrogram/client/types/bots/inline_keyboard_markup.py +++ b/pyrogram/client/types/bots/inline_keyboard_markup.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + from pyrogram.api.types import ReplyInlineMarkup, KeyboardButtonRow from . import InlineKeyboardButton from ..pyrogram_type import PyrogramType @@ -30,7 +32,7 @@ class InlineKeyboardMarkup(PyrogramType): """ def __init__(self, - inline_keyboard: list): + inline_keyboard: List[List[InlineKeyboardButton]]): super().__init__(None) self.inline_keyboard = inline_keyboard diff --git a/pyrogram/client/types/bots/reply_keyboard_markup.py b/pyrogram/client/types/bots/reply_keyboard_markup.py index a8378614..85f38b10 100644 --- a/pyrogram/client/types/bots/reply_keyboard_markup.py +++ b/pyrogram/client/types/bots/reply_keyboard_markup.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + from pyrogram.api.types import KeyboardButtonRow from pyrogram.api.types import ReplyKeyboardMarkup as RawReplyKeyboardMarkup from . import KeyboardButton @@ -48,7 +50,7 @@ class ReplyKeyboardMarkup(PyrogramType): """ def __init__(self, - keyboard: list, + keyboard: List[List[KeyboardButton]], resize_keyboard: bool = None, one_time_keyboard: bool = None, selective: bool = None): diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index ba9b4dad..ff5ab577 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -58,12 +59,12 @@ class Animation(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, width: int, height: int, duration: int, - thumb=None, + thumb: PhotoSize = None, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -82,7 +83,7 @@ class Animation(PyrogramType): @staticmethod def _parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo, - file_name: str) -> "Animation": + file_name: str) -> "Animation": return Animation( file_id=encode( pack( diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 0590e760..614c18fb 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -58,10 +59,10 @@ class Audio(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, duration: int, - thumb=None, + thumb: PhotoSize = None, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -81,7 +82,8 @@ class Audio(PyrogramType): self.title = title @staticmethod - def _parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio": + def _parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, + file_name: str) -> "Audio": return Audio( file_id=encode( pack( diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 05cbfb1d..29f76bfc 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType @@ -42,7 +44,7 @@ class Contact(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", phone_number: str, first_name: str, last_name: str = None, diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 172d816d..330474c4 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -49,9 +50,9 @@ class Document(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, - thumb=None, + thumb: PhotoSize = None, file_name: str = None, mime_type: str = None, file_size: int = None, diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 455e7f58..b57efb46 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType @@ -31,7 +33,11 @@ class Location(PyrogramType): Latitude as defined by sender. """ - def __init__(self, *, client, longitude: float, latitude: float): + def __init__(self, + *, + client: "pyrogram.Client", + longitude: float, + latitude: float): super().__init__(client) self.longitude = longitude diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 14662e00..13fb1b16 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -337,7 +337,7 @@ class Message(PyrogramType): @staticmethod def _parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict, - replies: int = 1): + replies: int = 1): if isinstance(message, types.MessageEmpty): return Message(message_id=message.id, empty=True, client=client) diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 4f625bd3..637d9fdd 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType from ..user_and_chats.user import User @@ -63,12 +65,12 @@ class MessageEntity(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", type: str, offset: int, length: int, url: str = None, - user=None): + user: User = None): super().__init__(client) self.type = type diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 48d6c85b..55c4fde1 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + +import pyrogram from pyrogram.api import types from .message import Message from ..pyrogram_type import PyrogramType @@ -35,9 +38,9 @@ class Messages(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", total_count: int, - messages: list): + messages: List[Message]): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index cdf3b191..6531d1b7 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -18,7 +18,9 @@ from base64 import b64encode from struct import pack +from typing import List +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -41,10 +43,10 @@ class Photo(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", id: str, date: int, - sizes: list): + sizes: List[PhotoSize]): super().__init__(client) self.id = id diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index 4fb9b30d..0250d76b 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from pyrogram.client.ext.utils import encode from ..pyrogram_type import PyrogramType @@ -42,7 +43,7 @@ class PhotoSize(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, width: int, height: int, diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index 22121dd8..fcdf63e6 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -19,6 +19,7 @@ from functools import lru_cache from struct import pack +import pyrogram from pyrogram.api import types, functions from pyrogram.api.errors import StickersetInvalid from .photo_size import PhotoSize @@ -65,18 +66,17 @@ class Sticker(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, width: int, height: int, - thumb=None, + thumb: PhotoSize = None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, emoji: str = None, - set_name: str = None, - mask_position=None): + set_name: str = None): super().__init__(client) self.file_id = file_id @@ -89,7 +89,7 @@ class Sticker(PyrogramType): self.height = height self.emoji = emoji self.set_name = set_name - self.mask_position = mask_position + # self.mask_position = mask_position @staticmethod @lru_cache(maxsize=256) @@ -105,7 +105,7 @@ class Sticker(PyrogramType): @staticmethod def _parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, - sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": + sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker": sticker_set = sticker_attributes.stickerset if isinstance(sticker_set, types.InputStickerSetID): diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index ca27fe2c..dd850475 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + +import pyrogram from .photo import Photo from ..pyrogram_type import PyrogramType @@ -33,9 +36,9 @@ class UserProfilePhotos(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", total_count: int, - photos: list): + photos: List[Photo]): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 12701d8c..20c2d6db 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import types from .location import Location from ..pyrogram_type import PyrogramType @@ -45,8 +46,8 @@ class Venue(PyrogramType): def __init__(self, *, - client, - location, + client: "pyrogram.Client", + location: Location, title: str, address: str, foursquare_id: str = None, diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index a69f0138..fe183bbd 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -58,12 +59,12 @@ class Video(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, width: int, height: int, duration: int, - thumb=None, + thumb: PhotoSize = None, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -81,7 +82,8 @@ class Video(PyrogramType): self.duration = duration @staticmethod - def _parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video": + def _parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, + file_name: str) -> "Video": return Video( file_id=encode( pack( diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 53419dda..c9f23efb 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from .photo_size import PhotoSize from ..pyrogram_type import PyrogramType @@ -52,11 +53,11 @@ class VideoNote(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, length: int, duration: int, - thumb=None, + thumb: PhotoSize = None, mime_type: str = None, file_size: int = None, date: int = None): diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index bf5ffaa8..c06b1741 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from ..pyrogram_type import PyrogramType from ...ext.utils import encode @@ -48,7 +49,7 @@ class Voice(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", file_id: str, duration: int, waveform: bytes = None, diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 4a3dfb20..914383ac 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import types from .chat_photo import ChatPhoto from ..pyrogram_type import PyrogramType @@ -78,7 +79,7 @@ class Chat(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", id: int, type: str, title: str = None, @@ -86,7 +87,7 @@ class Chat(PyrogramType): first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, - photo=None, + photo: ChatPhoto = None, description: str = None, invite_link: str = None, pinned_message=None, @@ -175,7 +176,7 @@ class Chat(PyrogramType): @staticmethod def _parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat": if isinstance(chat_full, types.UserFull): - _parsed_chat = Chat.parse_user_chat(client, chat_full.user) + parsed_chat = Chat._parse_user_chat(client, chat_full.user) parsed_chat.description = chat_full.about else: full_chat = chat_full.full_chat @@ -186,12 +187,12 @@ class Chat(PyrogramType): chat = i if isinstance(full_chat, types.ChatFull): - _parsed_chat = Chat.parse_chat_chat(client, chat) + parsed_chat = Chat._parse_chat_chat(client, chat) if isinstance(full_chat.participants, types.ChatParticipants): parsed_chat.members_count = len(full_chat.participants.participants) else: - _parsed_chat = Chat.parse_channel_chat(client, chat) + parsed_chat = Chat._parse_channel_chat(client, chat) parsed_chat.members_count = full_chat.participants_count parsed_chat.description = full_chat.about or None # TODO: Add StickerSet type diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 5fc9c988..aaf39859 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType @@ -81,8 +83,8 @@ class ChatMember(PyrogramType): def __init__(self, *, - client, - user, + client: "pyrogram.Client", + user: "pyrogram.User", status: str, until_date: int = None, can_be_edited: bool = None, diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index a798f312..7e79cd5f 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + +import pyrogram from pyrogram.api import types from .chat_member import ChatMember from .user import User @@ -35,9 +38,9 @@ class ChatMembers(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", total_count: int, - chat_members: list): + chat_members: List[ChatMember]): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index 33eca8c8..f4ed0fe3 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -18,6 +18,7 @@ from struct import pack +import pyrogram from pyrogram.api import types from ..pyrogram_type import PyrogramType from ...ext.utils import encode @@ -36,7 +37,7 @@ class ChatPhoto(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", small_file_id: str, big_file_id: str): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index b5679ed2..82dff29b 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType from ..user_and_chats import Chat @@ -46,9 +48,9 @@ class Dialog(PyrogramType): def __init__(self, *, - client, - chat, - top_message, + client: "pyrogram.Client", + chat: Chat, + top_message: "pyrogram.Message", unread_messages_count: int, unread_mentions_count: int, unread_mark: bool, diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 0668052b..3f44ea4c 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + +import pyrogram from pyrogram.api import types from .dialog import Dialog from ..messages_and_media import Message @@ -35,9 +38,9 @@ class Dialogs(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", total_count: int, - dialogs: list): + dialogs: List[Dialog]): super().__init__(client) self.total_count = total_count diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index 74fc0516..c3b733b0 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram.api import types from .chat_photo import ChatPhoto from .user_status import UserStatus @@ -71,7 +72,7 @@ class User(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", id: int, is_self: bool, is_contact: bool, @@ -80,11 +81,11 @@ class User(PyrogramType): is_bot: bool, first_name: str, last_name: str = None, - status=None, + status: UserStatus = None, username: str = None, language_code: str = None, phone_number: str = None, - photo=None, + photo: ChatPhoto = None, restriction_reason: str = None): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index feda1eaa..4a62adc8 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from pyrogram.api import types from ..pyrogram_type import PyrogramType @@ -64,7 +66,7 @@ class UserStatus(PyrogramType): def __init__(self, *, - client, + client: "pyrogram.Client", user_id: int, online: bool = None, offline: bool = None, From 4fb99694708d37747916cb519abec51b0dad2bbb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 14:45:16 +0100 Subject: [PATCH 58/62] Replace Client with BaseClient type hint --- pyrogram/client/types/bots/callback_query.py | 2 +- pyrogram/client/types/messages_and_media/animation.py | 2 +- pyrogram/client/types/messages_and_media/audio.py | 2 +- pyrogram/client/types/messages_and_media/contact.py | 2 +- pyrogram/client/types/messages_and_media/document.py | 2 +- pyrogram/client/types/messages_and_media/location.py | 2 +- pyrogram/client/types/messages_and_media/message.py | 6 +++--- pyrogram/client/types/messages_and_media/message_entity.py | 2 +- pyrogram/client/types/messages_and_media/messages.py | 2 +- pyrogram/client/types/messages_and_media/photo.py | 2 +- pyrogram/client/types/messages_and_media/photo_size.py | 2 +- pyrogram/client/types/messages_and_media/sticker.py | 2 +- .../client/types/messages_and_media/user_profile_photos.py | 2 +- pyrogram/client/types/messages_and_media/venue.py | 2 +- pyrogram/client/types/messages_and_media/video.py | 2 +- pyrogram/client/types/messages_and_media/video_note.py | 2 +- pyrogram/client/types/messages_and_media/voice.py | 2 +- pyrogram/client/types/pyrogram_type.py | 2 +- pyrogram/client/types/user_and_chats/chat.py | 2 +- pyrogram/client/types/user_and_chats/chat_member.py | 2 +- pyrogram/client/types/user_and_chats/chat_members.py | 2 +- pyrogram/client/types/user_and_chats/chat_photo.py | 2 +- pyrogram/client/types/user_and_chats/dialog.py | 2 +- pyrogram/client/types/user_and_chats/dialogs.py | 2 +- pyrogram/client/types/user_and_chats/user.py | 2 +- pyrogram/client/types/user_and_chats/user_status.py | 2 +- 26 files changed, 28 insertions(+), 28 deletions(-) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index 5d242162..c3c23333 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -59,7 +59,7 @@ class CallbackQuery(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", id: str, from_user: User, chat_instance: str, diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index ff5ab577..6b7f7cf7 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -59,7 +59,7 @@ class Animation(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, width: int, height: int, diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 614c18fb..148ae805 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -59,7 +59,7 @@ class Audio(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, duration: int, thumb: PhotoSize = None, diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 29f76bfc..16dd52bb 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -44,7 +44,7 @@ class Contact(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", phone_number: str, first_name: str, last_name: str = None, diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 330474c4..db41df6c 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -50,7 +50,7 @@ class Document(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, thumb: PhotoSize = None, file_name: str = None, diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index b57efb46..fcdd5e31 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -35,7 +35,7 @@ class Location(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", longitude: float, latitude: float): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 13fb1b16..0dede950 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -210,11 +210,11 @@ class Message(PyrogramType): Messages sent from yourself to other chats are outgoing (*outgoing* is True). An exception is made for your own personal chat; messages sent there will be incoming. - matches (``List of regex Matches``, *optional*): + matches (List of regex Matches, *optional*): A list containing all `Match Objects `_ that match the text of this message. Only applicable when using :obj:`Filters.regex `. - command (``List of strings``, *optional*): + command (List of ``str``, *optional*): A list containing the command and its arguments, if any. E.g.: "/start 1 2 3" would produce ["start", "1", "2", "3"]. Only applicable when using :obj:`Filters.command `. @@ -228,7 +228,7 @@ class Message(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", message_id: int, date: int = None, chat: Chat = None, diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 637d9fdd..7544424c 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -65,7 +65,7 @@ class MessageEntity(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", type: str, offset: int, length: int, diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 55c4fde1..cd401c1b 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -38,7 +38,7 @@ class Messages(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", total_count: int, messages: List[Message]): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 6531d1b7..a52a7aa2 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -43,7 +43,7 @@ class Photo(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", id: str, date: int, sizes: List[PhotoSize]): diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index 0250d76b..7a4db1c2 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -43,7 +43,7 @@ class PhotoSize(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, width: int, height: int, diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index fcdf63e6..8b88e7bf 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -66,7 +66,7 @@ class Sticker(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, width: int, height: int, diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index dd850475..2bfd29c8 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -36,7 +36,7 @@ class UserProfilePhotos(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", total_count: int, photos: List[Photo]): super().__init__(client) diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 20c2d6db..443f479a 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -46,7 +46,7 @@ class Venue(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", location: Location, title: str, address: str, diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index fe183bbd..2c476b39 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -59,7 +59,7 @@ class Video(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, width: int, height: int, diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index c9f23efb..718432fa 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -53,7 +53,7 @@ class VideoNote(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, length: int, duration: int, diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index c06b1741..b9f799b3 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -49,7 +49,7 @@ class Voice(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", file_id: str, duration: int, waveform: bytes = None, diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py index 3c2babf8..f5502a72 100644 --- a/pyrogram/client/types/pyrogram_type.py +++ b/pyrogram/client/types/pyrogram_type.py @@ -52,7 +52,7 @@ class Encoder(JSONEncoder): return remove_none( OrderedDict( - [("_", "pyrogram:" + o.__class__.__name__)] + [("_", "pyrogram." + o.__class__.__name__)] + [i for i in content.items()] ) ) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 914383ac..5b5a54a8 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -79,7 +79,7 @@ class Chat(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", id: int, type: str, title: str = None, diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index aaf39859..b778d4d8 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -83,7 +83,7 @@ class ChatMember(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", user: "pyrogram.User", status: str, until_date: int = None, diff --git a/pyrogram/client/types/user_and_chats/chat_members.py b/pyrogram/client/types/user_and_chats/chat_members.py index 7e79cd5f..838517ab 100644 --- a/pyrogram/client/types/user_and_chats/chat_members.py +++ b/pyrogram/client/types/user_and_chats/chat_members.py @@ -38,7 +38,7 @@ class ChatMembers(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", total_count: int, chat_members: List[ChatMember]): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index f4ed0fe3..ad4b3151 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -37,7 +37,7 @@ class ChatPhoto(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", small_file_id: str, big_file_id: str): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/dialog.py b/pyrogram/client/types/user_and_chats/dialog.py index 82dff29b..c001708f 100644 --- a/pyrogram/client/types/user_and_chats/dialog.py +++ b/pyrogram/client/types/user_and_chats/dialog.py @@ -48,7 +48,7 @@ class Dialog(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", chat: Chat, top_message: "pyrogram.Message", unread_messages_count: int, diff --git a/pyrogram/client/types/user_and_chats/dialogs.py b/pyrogram/client/types/user_and_chats/dialogs.py index 3f44ea4c..2492d5e2 100644 --- a/pyrogram/client/types/user_and_chats/dialogs.py +++ b/pyrogram/client/types/user_and_chats/dialogs.py @@ -38,7 +38,7 @@ class Dialogs(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", total_count: int, dialogs: List[Dialog]): super().__init__(client) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index c3b733b0..354e8a09 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -72,7 +72,7 @@ class User(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", id: int, is_self: bool, is_contact: bool, diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 4a62adc8..69c1921b 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -66,7 +66,7 @@ class UserStatus(PyrogramType): def __init__(self, *, - client: "pyrogram.Client", + client: "pyrogram.client.ext.BaseClient", user_id: int, online: bool = None, offline: bool = None, From b593463bd7819b18a36175e590243786dccc637d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 14:50:23 +0100 Subject: [PATCH 59/62] Type hint all the remaining method parameters --- .../methods/bots/get_inline_bot_results.py | 4 ++- .../methods/bots/request_callback_answer.py | 4 ++- .../methods/bots/send_inline_bot_result.py | 4 ++- .../client/methods/chats/delete_chat_photo.py | 5 ++- .../methods/chats/export_chat_invite_link.py | 5 ++- pyrogram/client/methods/chats/get_chat.py | 7 ++-- .../client/methods/chats/get_chat_member.py | 10 +++--- .../client/methods/chats/get_chat_members.py | 8 +++-- .../methods/chats/get_chat_members_count.py | 5 ++- pyrogram/client/methods/chats/get_dialogs.py | 4 +-- pyrogram/client/methods/chats/join_chat.py | 3 +- .../client/methods/chats/kick_chat_member.py | 8 +++-- pyrogram/client/methods/chats/leave_chat.py | 6 +++- .../client/methods/chats/pin_chat_message.py | 7 +++- .../methods/chats/promote_chat_member.py | 6 ++-- .../methods/chats/restrict_chat_member.py | 6 ++-- .../methods/chats/set_chat_description.py | 6 +++- .../client/methods/chats/set_chat_photo.py | 5 ++- .../client/methods/chats/set_chat_title.py | 6 +++- .../client/methods/chats/unban_chat_member.py | 6 ++-- .../methods/chats/unpin_chat_message.py | 5 ++- .../client/methods/contacts/add_contacts.py | 10 ++++-- .../methods/contacts/delete_contacts.py | 7 ++-- .../methods/decorators/on_callback_query.py | 4 ++- .../methods/decorators/on_deleted_messages.py | 4 ++- .../client/methods/decorators/on_message.py | 4 ++- .../methods/decorators/on_raw_update.py | 3 +- .../methods/decorators/on_user_status.py | 4 ++- .../methods/messages/delete_messages.py | 6 ++-- .../methods/messages/edit_message_caption.py | 9 ++--- .../methods/messages/edit_message_media.py | 10 +++--- .../messages/edit_message_reply_markup.py | 8 +++-- .../methods/messages/edit_message_text.py | 8 +++-- .../methods/messages/forward_messages.py | 10 +++--- .../client/methods/messages/get_history.py | 6 ++-- .../client/methods/messages/get_messages.py | 10 +++--- .../client/methods/messages/send_animation.py | 10 ++++-- .../client/methods/messages/send_audio.py | 10 ++++-- .../methods/messages/send_chat_action.py | 6 ++-- .../client/methods/messages/send_contact.py | 11 +++++-- .../client/methods/messages/send_document.py | 10 ++++-- .../client/methods/messages/send_location.py | 11 +++++-- .../methods/messages/send_media_group.py | 11 ++++--- .../client/methods/messages/send_message.py | 11 +++++-- .../client/methods/messages/send_photo.py | 10 ++++-- .../client/methods/messages/send_sticker.py | 10 ++++-- .../client/methods/messages/send_venue.py | 11 +++++-- .../client/methods/messages/send_video.py | 10 ++++-- .../methods/messages/send_video_note.py | 10 ++++-- .../client/methods/messages/send_voice.py | 10 ++++-- .../methods/password/change_cloud_password.py | 5 ++- .../methods/password/enable_cloud_password.py | 5 ++- .../methods/password/remove_cloud_password.py | 3 +- .../users/delete_user_profile_photos.py | 4 ++- pyrogram/client/methods/users/get_me.py | 2 +- .../methods/users/get_user_profile_photos.py | 6 ++-- pyrogram/client/methods/users/get_users.py | 7 ++-- .../methods/users/set_user_profile_photo.py | 3 +- .../methods/utilities/download_media.py | 33 ++++++++++--------- .../types/messages_and_media/message.py | 2 +- 60 files changed, 295 insertions(+), 139 deletions(-) diff --git a/pyrogram/client/methods/bots/get_inline_bot_results.py b/pyrogram/client/methods/bots/get_inline_bot_results.py index cb931d49..a5b9ae9f 100644 --- a/pyrogram/client/methods/bots/get_inline_bot_results.py +++ b/pyrogram/client/methods/bots/get_inline_bot_results.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from pyrogram.api.errors import UnknownError from pyrogram.client.ext import BaseClient @@ -23,7 +25,7 @@ from pyrogram.client.ext import BaseClient class GetInlineBotResults(BaseClient): def get_inline_bot_results(self, - bot: int or str, + bot: Union[int, str], query: str, offset: str = "", latitude: float = None, diff --git a/pyrogram/client/methods/bots/request_callback_answer.py b/pyrogram/client/methods/bots/request_callback_answer.py index 470ad15a..0cb6a1dd 100644 --- a/pyrogram/client/methods/bots/request_callback_answer.py +++ b/pyrogram/client/methods/bots/request_callback_answer.py @@ -16,13 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions from pyrogram.client.ext import BaseClient class RequestCallbackAnswer(BaseClient): def request_callback_answer(self, - chat_id: int or str, + chat_id: Union[int, str], message_id: int, callback_data: bytes): """Use this method to request a callback answer from bots. This is the equivalent of clicking an diff --git a/pyrogram/client/methods/bots/send_inline_bot_result.py b/pyrogram/client/methods/bots/send_inline_bot_result.py index 97ea2a4a..23d36cba 100644 --- a/pyrogram/client/methods/bots/send_inline_bot_result.py +++ b/pyrogram/client/methods/bots/send_inline_bot_result.py @@ -16,13 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions from pyrogram.client.ext import BaseClient class SendInlineBotResult(BaseClient): def send_inline_bot_result(self, - chat_id: int or str, + chat_id: Union[int, str], query_id: int, result_id: str, disable_notification: bool = None, diff --git a/pyrogram/client/methods/chats/delete_chat_photo.py b/pyrogram/client/methods/chats/delete_chat_photo.py index 5a1fe74e..d4658e4d 100644 --- a/pyrogram/client/methods/chats/delete_chat_photo.py +++ b/pyrogram/client/methods/chats/delete_chat_photo.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class DeleteChatPhoto(BaseClient): - def delete_chat_photo(self, chat_id: int or str): + def delete_chat_photo(self, + chat_id: Union[int, str]): """Use this method to delete a chat photo. Photos can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/export_chat_invite_link.py b/pyrogram/client/methods/chats/export_chat_invite_link.py index 4972493b..8f068586 100644 --- a/pyrogram/client/methods/chats/export_chat_invite_link.py +++ b/pyrogram/client/methods/chats/export_chat_invite_link.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class ExportChatInviteLink(BaseClient): - def export_chat_invite_link(self, chat_id: int or str): + def export_chat_invite_link(self, + chat_id: Union[int, str]): """Use this method to generate a new invite link for a chat; any previously generated link is revoked. You must be an administrator in the chat for this to work and have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/get_chat.py b/pyrogram/client/methods/chats/get_chat.py index 7c191bf3..24651d37 100644 --- a/pyrogram/client/methods/chats/get_chat.py +++ b/pyrogram/client/methods/chats/get_chat.py @@ -16,13 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient class GetChat(BaseClient): - def get_chat(self, chat_id: int or str): + def get_chat(self, + chat_id: Union[int, str]): """Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.) @@ -45,4 +48,4 @@ class GetChat(BaseClient): else: r = self.send(functions.messages.GetFullChat(peer.chat_id)) - return pyrogram.Chat.parse_full(self, r) + return pyrogram.Chat._parse_full(self, r) diff --git a/pyrogram/client/methods/chats/get_chat_member.py b/pyrogram/client/methods/chats/get_chat_member.py index b304be17..7708f6fc 100644 --- a/pyrogram/client/methods/chats/get_chat_member.py +++ b/pyrogram/client/methods/chats/get_chat_member.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types, errors from ...ext import BaseClient @@ -23,8 +25,8 @@ from ...ext import BaseClient class GetChatMember(BaseClient): def get_chat_member(self, - chat_id: int or str, - user_id: int or str): + chat_id: Union[int, str], + user_id: Union[int, str]): """Use this method to get information about one member of a chat. Args: @@ -52,7 +54,7 @@ class GetChatMember(BaseClient): ) ) - for member in pyrogram.ChatMembers.parse(self, full_chat).chat_members: + for member in pyrogram.ChatMembers._parse(self, full_chat).chat_members: if member.user.id == user_id.user_id: return member else: @@ -65,7 +67,7 @@ class GetChatMember(BaseClient): ) ) - return pyrogram.ChatMembers.parse( + return pyrogram.ChatMembers._parse( self, types.channels.ChannelParticipants( count=1, diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index 71728676..031ea46b 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -32,7 +34,7 @@ class Filters: class GetChatMembers(BaseClient): def get_chat_members(self, - chat_id: int or str, + chat_id: Union[int, str], offset: int = 0, limit: int = 200, query: str = "", @@ -84,7 +86,7 @@ class GetChatMembers(BaseClient): peer = self.resolve_peer(chat_id) if isinstance(peer, types.InputPeerChat): - return pyrogram.ChatMembers.parse( + return pyrogram.ChatMembers._parse( self, self.send( functions.messages.GetFullChat( @@ -110,7 +112,7 @@ class GetChatMembers(BaseClient): else: raise ValueError("Invalid filter \"{}\"".format(filter)) - return pyrogram.ChatMembers.parse( + return pyrogram.ChatMembers._parse( self, self.send( functions.channels.GetParticipants( diff --git a/pyrogram/client/methods/chats/get_chat_members_count.py b/pyrogram/client/methods/chats/get_chat_members_count.py index ec5aaae0..01bbd835 100644 --- a/pyrogram/client/methods/chats/get_chat_members_count.py +++ b/pyrogram/client/methods/chats/get_chat_members_count.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class GetChatMembersCount(BaseClient): - def get_chat_members_count(self, chat_id: int or str): + def get_chat_members_count(self, + chat_id: Union[int, str]): """Use this method to get the number of members in a chat. Args: diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index f8bed7c8..87cd5be2 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=None, + offset_dialog: "pyrogram.Dialog" = None, limit: int = 100, pinned_only: bool = False): """Use this method to get the user's dialogs @@ -64,4 +64,4 @@ class GetDialogs(BaseClient): ) ) - return pyrogram.Dialogs.parse(self, r) + return pyrogram.Dialogs._parse(self, r) diff --git a/pyrogram/client/methods/chats/join_chat.py b/pyrogram/client/methods/chats/join_chat.py index f5e3953c..2f14c617 100644 --- a/pyrogram/client/methods/chats/join_chat.py +++ b/pyrogram/client/methods/chats/join_chat.py @@ -21,7 +21,8 @@ from ...ext import BaseClient class JoinChat(BaseClient): - def join_chat(self, chat_id: str): + def join_chat(self, + chat_id: str): """Use this method to join a group chat or channel. Args: diff --git a/pyrogram/client/methods/chats/kick_chat_member.py b/pyrogram/client/methods/chats/kick_chat_member.py index 292fdfd6..a7459110 100644 --- a/pyrogram/client/methods/chats/kick_chat_member.py +++ b/pyrogram/client/methods/chats/kick_chat_member.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -23,8 +25,8 @@ from ...ext import BaseClient class KickChatMember(BaseClient): def kick_chat_member(self, - chat_id: int or str, - user_id: int or str, + chat_id: Union[int, str], + user_id: Union[int, str], until_date: int = 0): """Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using @@ -86,7 +88,7 @@ class KickChatMember(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/chats/leave_chat.py b/pyrogram/client/methods/chats/leave_chat.py index 8f070b82..e0ac3bb6 100644 --- a/pyrogram/client/methods/chats/leave_chat.py +++ b/pyrogram/client/methods/chats/leave_chat.py @@ -16,12 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class LeaveChat(BaseClient): - def leave_chat(self, chat_id: int or str, delete: bool = False): + def leave_chat(self, + chat_id: Union[int, str], + delete: bool = False): """Use this method to leave a group chat or channel. Args: diff --git a/pyrogram/client/methods/chats/pin_chat_message.py b/pyrogram/client/methods/chats/pin_chat_message.py index de7e69d4..1ef07d34 100644 --- a/pyrogram/client/methods/chats/pin_chat_message.py +++ b/pyrogram/client/methods/chats/pin_chat_message.py @@ -16,12 +16,17 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class PinChatMessage(BaseClient): - def pin_chat_message(self, chat_id: int or str, message_id: int, disable_notification: bool = None): + def pin_chat_message(self, + chat_id: Union[int, str], + message_id: int, + disable_notification: bool = None): """Use this method to pin a message in a supergroup or a channel. You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel. diff --git a/pyrogram/client/methods/chats/promote_chat_member.py b/pyrogram/client/methods/chats/promote_chat_member.py index 9db7709b..d22a8ea7 100644 --- a/pyrogram/client/methods/chats/promote_chat_member.py +++ b/pyrogram/client/methods/chats/promote_chat_member.py @@ -16,14 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class PromoteChatMember(BaseClient): def promote_chat_member(self, - chat_id: int or str, - user_id: int or str, + chat_id: Union[int, str], + user_id: Union[int, str], can_change_info: bool = True, can_post_messages: bool = False, can_edit_messages: bool = False, diff --git a/pyrogram/client/methods/chats/restrict_chat_member.py b/pyrogram/client/methods/chats/restrict_chat_member.py index 35db5e59..c772d100 100644 --- a/pyrogram/client/methods/chats/restrict_chat_member.py +++ b/pyrogram/client/methods/chats/restrict_chat_member.py @@ -16,14 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class RestrictChatMember(BaseClient): def restrict_chat_member(self, - chat_id: int or str, - user_id: int or str, + chat_id: Union[int, str], + user_id: Union[int, str], until_date: int = 0, can_send_messages: bool = False, can_send_media_messages: bool = False, diff --git a/pyrogram/client/methods/chats/set_chat_description.py b/pyrogram/client/methods/chats/set_chat_description.py index 5f5ead7f..1606fe7e 100644 --- a/pyrogram/client/methods/chats/set_chat_description.py +++ b/pyrogram/client/methods/chats/set_chat_description.py @@ -16,12 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class SetChatDescription(BaseClient): - def set_chat_description(self, chat_id: int or str, description: str): + def set_chat_description(self, + chat_id: Union[int, str], + description: str): """Use this method to change the description of a supergroup or a channel. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/set_chat_photo.py b/pyrogram/client/methods/chats/set_chat_photo.py index 51045a8a..ed058000 100644 --- a/pyrogram/client/methods/chats/set_chat_photo.py +++ b/pyrogram/client/methods/chats/set_chat_photo.py @@ -19,13 +19,16 @@ import os from base64 import b64decode from struct import unpack +from typing import Union from pyrogram.api import functions, types from ...ext import BaseClient class SetChatPhoto(BaseClient): - def set_chat_photo(self, chat_id: int or str, photo: str): + def set_chat_photo(self, + chat_id: Union[int, str], + photo: str): """Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/set_chat_title.py b/pyrogram/client/methods/chats/set_chat_title.py index c7176c82..58d4179c 100644 --- a/pyrogram/client/methods/chats/set_chat_title.py +++ b/pyrogram/client/methods/chats/set_chat_title.py @@ -16,12 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class SetChatTitle(BaseClient): - def set_chat_title(self, chat_id: int or str, title: str): + def set_chat_title(self, + chat_id: Union[int, str], + title: str): """Use this method to change the title of a chat. Titles can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/unban_chat_member.py b/pyrogram/client/methods/chats/unban_chat_member.py index c8b20131..428e6fe8 100644 --- a/pyrogram/client/methods/chats/unban_chat_member.py +++ b/pyrogram/client/methods/chats/unban_chat_member.py @@ -16,14 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class UnbanChatMember(BaseClient): def unban_chat_member(self, - chat_id: int or str, - user_id: int or str): + chat_id: Union[int, str], + user_id: Union[int, str]): """Use this method to unban a previously kicked user in a supergroup or channel. The user will **not** return to the group or channel automatically, but will be able to join via link, etc. You must be an administrator for this to work. diff --git a/pyrogram/client/methods/chats/unpin_chat_message.py b/pyrogram/client/methods/chats/unpin_chat_message.py index 9bbb1021..bd6a22f8 100644 --- a/pyrogram/client/methods/chats/unpin_chat_message.py +++ b/pyrogram/client/methods/chats/unpin_chat_message.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions, types from ...ext import BaseClient class UnpinChatMessage(BaseClient): - def unpin_chat_message(self, chat_id: int or str): + def unpin_chat_message(self, + chat_id: Union[int, str]): """Use this method to unpin a message in a supergroup or a channel. You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel. diff --git a/pyrogram/client/methods/contacts/add_contacts.py b/pyrogram/client/methods/contacts/add_contacts.py index 3a1dcc92..a5f06050 100644 --- a/pyrogram/client/methods/contacts/add_contacts.py +++ b/pyrogram/client/methods/contacts/add_contacts.py @@ -16,17 +16,21 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + +import pyrogram from pyrogram.api import functions from ...ext import BaseClient class AddContacts(BaseClient): - def add_contacts(self, contacts: list): + def add_contacts(self, + contacts: List["pyrogram.InputPhoneContact"]): """Use this method to add contacts to your Telegram address book. Args: - contacts (``list``): - A list of :obj:`InputPhoneContact ` + contacts (List of :obj:`InputPhoneContact `): + The contact list to be added Returns: On success, the added contacts are returned. diff --git a/pyrogram/client/methods/contacts/delete_contacts.py b/pyrogram/client/methods/contacts/delete_contacts.py index 74f08dd1..2c18c6d8 100644 --- a/pyrogram/client/methods/contacts/delete_contacts.py +++ b/pyrogram/client/methods/contacts/delete_contacts.py @@ -16,17 +16,20 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import List + from pyrogram.api import functions, types from pyrogram.api.errors import PeerIdInvalid from ...ext import BaseClient class DeleteContacts(BaseClient): - def delete_contacts(self, ids: list): + def delete_contacts(self, + ids: List[int]): """Use this method to delete contacts from your Telegram address book Args: - ids (``list``): + ids (List of ``int``): A list of unique identifiers for the target users. Can be an ID (int), a username (string) or phone number (string). diff --git a/pyrogram/client/methods/decorators/on_callback_query.py b/pyrogram/client/methods/decorators/on_callback_query.py index 51a6df2e..f34119e2 100644 --- a/pyrogram/client/methods/decorators/on_callback_query.py +++ b/pyrogram/client/methods/decorators/on_callback_query.py @@ -22,7 +22,9 @@ from ...ext import BaseClient class OnCallbackQuery(BaseClient): - def on_callback_query(self=None, filters=None, group: int = 0): + def on_callback_query(self=None, + filters=None, + group: int = 0): """Use this decorator to automatically register a function for handling callback queries. This does the same thing as :meth:`add_handler` using the :class:`CallbackQueryHandler`. diff --git a/pyrogram/client/methods/decorators/on_deleted_messages.py b/pyrogram/client/methods/decorators/on_deleted_messages.py index c23b7594..406563f2 100644 --- a/pyrogram/client/methods/decorators/on_deleted_messages.py +++ b/pyrogram/client/methods/decorators/on_deleted_messages.py @@ -22,7 +22,9 @@ from ...ext import BaseClient class OnDeletedMessages(BaseClient): - def on_deleted_messages(self=None, filters=None, group: int = 0): + def on_deleted_messages(self=None, + filters=None, + group: int = 0): """Use this decorator to automatically register a function for handling deleted messages. This does the same thing as :meth:`add_handler` using the :class:`DeletedMessagesHandler`. diff --git a/pyrogram/client/methods/decorators/on_message.py b/pyrogram/client/methods/decorators/on_message.py index a098cfa2..ec0e11e8 100644 --- a/pyrogram/client/methods/decorators/on_message.py +++ b/pyrogram/client/methods/decorators/on_message.py @@ -22,7 +22,9 @@ from ...ext import BaseClient class OnMessage(BaseClient): - def on_message(self=None, filters=None, group: int = 0): + def on_message(self=None, + filters=None, + group: int = 0): """Use this decorator to automatically register a function for handling messages. This does the same thing as :meth:`add_handler` using the :class:`MessageHandler`. diff --git a/pyrogram/client/methods/decorators/on_raw_update.py b/pyrogram/client/methods/decorators/on_raw_update.py index 52728e1c..8b2723df 100644 --- a/pyrogram/client/methods/decorators/on_raw_update.py +++ b/pyrogram/client/methods/decorators/on_raw_update.py @@ -21,7 +21,8 @@ from ...ext import BaseClient class OnRawUpdate(BaseClient): - def on_raw_update(self=None, group: int = 0): + def on_raw_update(self=None, + group: int = 0): """Use this decorator to automatically register a function for handling raw updates. This does the same thing as :meth:`add_handler` using the :class:`RawUpdateHandler`. diff --git a/pyrogram/client/methods/decorators/on_user_status.py b/pyrogram/client/methods/decorators/on_user_status.py index 2ca41e56..6ae47a95 100644 --- a/pyrogram/client/methods/decorators/on_user_status.py +++ b/pyrogram/client/methods/decorators/on_user_status.py @@ -22,7 +22,9 @@ from ...ext import BaseClient class OnUserStatus(BaseClient): - def on_user_status(self=None, filters=None, group: int = 0): + def on_user_status(self=None, + filters=None, + group: int = 0): """Use this decorator to automatically register a function for handling user status updates. This does the same thing as :meth:`add_handler` using the :class:`UserStatusHandler`. diff --git a/pyrogram/client/methods/messages/delete_messages.py b/pyrogram/client/methods/messages/delete_messages.py index f824a4b6..6aaf710a 100644 --- a/pyrogram/client/methods/messages/delete_messages.py +++ b/pyrogram/client/methods/messages/delete_messages.py @@ -16,14 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union, Iterable + from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient class DeleteMessages(BaseClient): def delete_messages(self, - chat_id: int or str, - message_ids, + chat_id: Union[int, str], + message_ids: Iterable[int], revoke: bool = True): """Use this method to delete messages, including service messages, with the following limitations: diff --git a/pyrogram/client/methods/messages/edit_message_caption.py b/pyrogram/client/methods/messages/edit_message_caption.py index a79c21f4..938e7af5 100644 --- a/pyrogram/client/methods/messages/edit_message_caption.py +++ b/pyrogram/client/methods/messages/edit_message_caption.py @@ -16,19 +16,20 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -import pyrogram +from typing import Union +import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient class EditMessageCaption(BaseClient): def edit_message_caption(self, - chat_id: int or str, + chat_id: Union[int, str], message_id: int, caption: str, parse_mode: str = "", - reply_markup=None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None): """Use this method to edit captions of messages. Args: @@ -70,7 +71,7 @@ class EditMessageCaption(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_media.py b/pyrogram/client/methods/messages/edit_message_media.py index 5d06c31b..bdfbf353 100644 --- a/pyrogram/client/methods/messages/edit_message_media.py +++ b/pyrogram/client/methods/messages/edit_message_media.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,14 +30,15 @@ from pyrogram.client.types import ( InputMediaPhoto, InputMediaVideo, InputMediaAudio, InputMediaAnimation, InputMediaDocument ) +from pyrogram.client.types.input_media import InputMedia class EditMessageMedia(BaseClient): def edit_message_media(self, - chat_id: int or str, + chat_id: Union[int, str], message_id: int, - media, - reply_markup=None): + media: InputMedia, + reply_markup: "pyrogram.InlineKeyboardMarkup" = None): """Use this method to edit audio, document, photo, or video messages. If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, @@ -334,7 +336,7 @@ class EditMessageMedia(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_reply_markup.py b/pyrogram/client/methods/messages/edit_message_reply_markup.py index 3b301dcb..31d8f0b7 100644 --- a/pyrogram/client/methods/messages/edit_message_reply_markup.py +++ b/pyrogram/client/methods/messages/edit_message_reply_markup.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient @@ -23,9 +25,9 @@ from pyrogram.client.ext import BaseClient class EditMessageReplyMarkup(BaseClient): def edit_message_reply_markup(self, - chat_id: int or str, + chat_id: Union[int, str], message_id: int, - reply_markup=None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None): """Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). Args: @@ -58,7 +60,7 @@ class EditMessageReplyMarkup(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/edit_message_text.py b/pyrogram/client/methods/messages/edit_message_text.py index 991ff049..688c7ce2 100644 --- a/pyrogram/client/methods/messages/edit_message_text.py +++ b/pyrogram/client/methods/messages/edit_message_text.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient @@ -23,12 +25,12 @@ from pyrogram.client.ext import BaseClient class EditMessageText(BaseClient): def edit_message_text(self, - chat_id: int or str, + chat_id: Union[int, str], message_id: int, text: str, parse_mode: str = "", disable_web_page_preview: bool = None, - reply_markup=None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None): """Use this method to edit text messages. Args: @@ -74,7 +76,7 @@ class EditMessageText(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/forward_messages.py b/pyrogram/client/methods/messages/forward_messages.py index e2e10c93..cafa468b 100644 --- a/pyrogram/client/methods/messages/forward_messages.py +++ b/pyrogram/client/methods/messages/forward_messages.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union, Iterable + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -23,9 +25,9 @@ from ...ext import BaseClient class ForwardMessages(BaseClient): def forward_messages(self, - chat_id: int or str, - from_chat_id: int or str, - message_ids, + chat_id: Union[int, str], + from_chat_id: Union[int, str], + message_ids: Iterable[int], disable_notification: bool = None): """Use this method to forward messages of any kind. @@ -78,7 +80,7 @@ class ForwardMessages(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): messages.append( - pyrogram.Message.parse( + pyrogram.Message._parse( self, i.message, users, chats ) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index bc272ce3..2a36d46c 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions from ...ext import BaseClient @@ -23,7 +25,7 @@ from ...ext import BaseClient class GetHistory(BaseClient): def get_history(self, - chat_id: int or str, + chat_id: Union[int, str], offset: int = 0, limit: int = 100, offset_id: int = 0, @@ -59,7 +61,7 @@ class GetHistory(BaseClient): :class:`Error ` in case of a Telegram RPC error. """ - return pyrogram.Messages.parse( + return pyrogram.Messages._parse( self, self.send( functions.messages.GetHistory( diff --git a/pyrogram/client/methods/messages/get_messages.py b/pyrogram/client/methods/messages/get_messages.py index 2e3930ae..9f67d9a0 100644 --- a/pyrogram/client/methods/messages/get_messages.py +++ b/pyrogram/client/methods/messages/get_messages.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union, Iterable + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -23,9 +25,9 @@ from ...ext import BaseClient class GetMessages(BaseClient): def get_messages(self, - chat_id: int or str, - message_ids: int or list = None, - reply_to_message_ids: int or list = None, + chat_id: Union[int, str], + message_ids: Union[int, Iterable[int]] = None, + reply_to_message_ids: Union[int, Iterable[int]] = None, replies: int = 1): """Use this method to get one or more messages that belong to a specific chat. You can retrieve up to 200 messages at once. @@ -76,6 +78,6 @@ class GetMessages(BaseClient): else: rpc = functions.messages.GetMessages(id=ids) - messages = pyrogram.Messages.parse(self, self.send(rpc)) + messages = pyrogram.Messages._parse(self, self.send(rpc)) return messages if is_iterable else messages.messages[0] diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py index c86fb867..8e240f5b 100644 --- a/pyrogram/client/methods/messages/send_animation.py +++ b/pyrogram/client/methods/messages/send_animation.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,7 +30,7 @@ from pyrogram.client.ext import BaseClient, utils class SendAnimation(BaseClient): def send_animation(self, - chat_id: int or str, + chat_id: Union[int, str], animation: str, caption: str = "", parse_mode: str = "", @@ -39,7 +40,10 @@ class SendAnimation(BaseClient): thumb: str = None, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send animation files (animation or H.264/MPEG-4 AVC video without sound). @@ -185,7 +189,7 @@ class SendAnimation(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 12c65dec..405d0561 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,7 +30,7 @@ from pyrogram.client.ext import BaseClient, utils class SendAudio(BaseClient): def send_audio(self, - chat_id: int or str, + chat_id: Union[int, str], audio: str, caption: str = "", parse_mode: str = "", @@ -39,7 +40,10 @@ class SendAudio(BaseClient): thumb: str = None, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send audio files. @@ -185,7 +189,7 @@ class SendAudio(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_chat_action.py b/pyrogram/client/methods/messages/send_chat_action.py index c519f67b..61e9994f 100644 --- a/pyrogram/client/methods/messages/send_chat_action.py +++ b/pyrogram/client/methods/messages/send_chat_action.py @@ -16,14 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + from pyrogram.api import functions from pyrogram.client.ext import BaseClient, ChatAction class SendChatAction(BaseClient): def send_chat_action(self, - chat_id: int or str, - action: ChatAction or str, + chat_id: Union[int, str], + action: Union[ChatAction, str], progress: int = 0): """Use this method when you need to tell the other party that something is happening on your side. diff --git a/pyrogram/client/methods/messages/send_contact.py b/pyrogram/client/methods/messages/send_contact.py index a5d88f97..d7f93743 100644 --- a/pyrogram/client/methods/messages/send_contact.py +++ b/pyrogram/client/methods/messages/send_contact.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient @@ -23,14 +25,17 @@ from pyrogram.client.ext import BaseClient class SendContact(BaseClient): def send_contact(self, - chat_id: int or str, + chat_id: Union[int, str], phone_number: str, first_name: str, last_name: str = "", vcard: str = "", disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None): + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None): """Use this method to send phone contacts. Args: @@ -87,7 +92,7 @@ class SendContact(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py index 66a65e9d..d7bb62d1 100644 --- a/pyrogram/client/methods/messages/send_document.py +++ b/pyrogram/client/methods/messages/send_document.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,14 +30,17 @@ from pyrogram.client.ext import BaseClient, utils class SendDocument(BaseClient): def send_document(self, - chat_id: int or str, + chat_id: Union[int, str], document: str, thumb: str = None, caption: str = "", parse_mode: str = "", disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send general files. @@ -166,7 +170,7 @@ class SendDocument(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_location.py b/pyrogram/client/methods/messages/send_location.py index baa84966..0efdb1df 100644 --- a/pyrogram/client/methods/messages/send_location.py +++ b/pyrogram/client/methods/messages/send_location.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient @@ -23,12 +25,15 @@ from pyrogram.client.ext import BaseClient class SendLocation(BaseClient): def send_location(self, - chat_id: int or str, + chat_id: Union[int, str], latitude: float, longitude: float, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None): + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None): """Use this method to send points on the map. Args: @@ -79,7 +84,7 @@ class SendLocation(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_media_group.py b/pyrogram/client/methods/messages/send_media_group.py index bc9f6971..d5549089 100644 --- a/pyrogram/client/methods/messages/send_media_group.py +++ b/pyrogram/client/methods/messages/send_media_group.py @@ -20,10 +20,11 @@ import binascii import mimetypes import os import struct +from typing import Union, List +import pyrogram from pyrogram.api import functions, types from pyrogram.api.errors import FileIdInvalid -from pyrogram.client import types as pyrogram_types from pyrogram.client.ext import BaseClient, utils @@ -32,8 +33,8 @@ class SendMediaGroup(BaseClient): # TODO: Return new Message object # TODO: Figure out how to send albums using URLs def send_media_group(self, - chat_id: int or str, - media: list, + chat_id: Union[int, str], + media: List[Union["pyrogram.InputMediaPhoto", "pyrogram.InputMediaVideo"]], disable_notification: bool = None, reply_to_message_id: int = None): """Use this method to send a group of photos or videos as an album. @@ -62,7 +63,7 @@ class SendMediaGroup(BaseClient): for i in media: style = self.html if i.parse_mode.lower() == "html" else self.markdown - if isinstance(i, pyrogram_types.InputMediaPhoto): + if isinstance(i, pyrogram.InputMediaPhoto): if os.path.exists(i.media): media = self.send( functions.messages.UploadMedia( @@ -101,7 +102,7 @@ class SendMediaGroup(BaseClient): access_hash=unpacked[3] ) ) - elif isinstance(i, pyrogram_types.InputMediaVideo): + elif isinstance(i, pyrogram.InputMediaVideo): if os.path.exists(i.media): media = self.send( functions.messages.UploadMedia( diff --git a/pyrogram/client/methods/messages/send_message.py b/pyrogram/client/methods/messages/send_message.py index 77af4db8..a04a67f2 100644 --- a/pyrogram/client/methods/messages/send_message.py +++ b/pyrogram/client/methods/messages/send_message.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from ...ext import BaseClient @@ -23,13 +25,16 @@ from ...ext import BaseClient class SendMessage(BaseClient): def send_message(self, - chat_id: int or str, + chat_id: Union[int, str], text: str, parse_mode: str = "", disable_web_page_preview: bool = None, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None): + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None): """Use this method to send text messages. Args: @@ -99,7 +104,7 @@ class SendMessage(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py index 323c2fa5..5b7b3268 100644 --- a/pyrogram/client/methods/messages/send_photo.py +++ b/pyrogram/client/methods/messages/send_photo.py @@ -19,6 +19,7 @@ import binascii import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -28,14 +29,17 @@ from pyrogram.client.ext import BaseClient, utils class SendPhoto(BaseClient): def send_photo(self, - chat_id: int or str, + chat_id: Union[int, str], photo: str, caption: str = "", parse_mode: str = "", ttl_seconds: int = None, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send photos. @@ -161,7 +165,7 @@ class SendPhoto(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_sticker.py b/pyrogram/client/methods/messages/send_sticker.py index fccfb0b5..550bcddb 100644 --- a/pyrogram/client/methods/messages/send_sticker.py +++ b/pyrogram/client/methods/messages/send_sticker.py @@ -19,6 +19,7 @@ import binascii import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -28,11 +29,14 @@ from pyrogram.client.ext import BaseClient, utils class SendSticker(BaseClient): def send_sticker(self, - chat_id: int or str, + chat_id: Union[int, str], sticker: str, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send .webp stickers. @@ -145,7 +149,7 @@ class SendSticker(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_venue.py b/pyrogram/client/methods/messages/send_venue.py index 1155ca79..35d725d1 100644 --- a/pyrogram/client/methods/messages/send_venue.py +++ b/pyrogram/client/methods/messages/send_venue.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient @@ -23,7 +25,7 @@ from pyrogram.client.ext import BaseClient class SendVenue(BaseClient): def send_venue(self, - chat_id: int or str, + chat_id: Union[int, str], latitude: float, longitude: float, title: str, @@ -32,7 +34,10 @@ class SendVenue(BaseClient): foursquare_type: str = "", disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None): + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None): """Use this method to send information about a venue. Args: @@ -101,7 +106,7 @@ class SendVenue(BaseClient): for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index 47b15d59..c6936cff 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,7 +30,7 @@ from pyrogram.client.ext import BaseClient, utils class SendVideo(BaseClient): def send_video(self, - chat_id: int or str, + chat_id: Union[int, str], video: str, caption: str = "", parse_mode: str = "", @@ -40,7 +41,10 @@ class SendVideo(BaseClient): supports_streaming: bool = True, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send video files. @@ -188,7 +192,7 @@ class SendVideo(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_video_note.py b/pyrogram/client/methods/messages/send_video_note.py index 61ae0017..75345607 100644 --- a/pyrogram/client/methods/messages/send_video_note.py +++ b/pyrogram/client/methods/messages/send_video_note.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,14 +30,17 @@ from pyrogram.client.ext import BaseClient, utils class SendVideoNote(BaseClient): def send_video_note(self, - chat_id: int or str, + chat_id: Union[int, str], video_note: str, duration: int = 0, length: int = 1, thumb: str = None, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send video messages. @@ -164,7 +168,7 @@ class SendVideoNote(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py index 4882a87f..52cbc833 100644 --- a/pyrogram/client/methods/messages/send_voice.py +++ b/pyrogram/client/methods/messages/send_voice.py @@ -20,6 +20,7 @@ import binascii import mimetypes import os import struct +from typing import Union import pyrogram from pyrogram.api import functions, types @@ -29,14 +30,17 @@ from pyrogram.client.ext import BaseClient, utils class SendVoice(BaseClient): def send_voice(self, - chat_id: int or str, + chat_id: Union[int, str], voice: str, caption: str = "", parse_mode: str = "", duration: int = 0, disable_notification: bool = None, reply_to_message_id: int = None, - reply_markup=None, + reply_markup: Union["pyrogram.InlineKeyboardMarkup", + "pyrogram.ReplyKeyboardMarkup", + "pyrogram.ReplyKeyboardRemove", + "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = ()): """Use this method to send audio files. @@ -164,7 +168,7 @@ class SendVoice(BaseClient): else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): - return pyrogram.Message.parse( + return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} diff --git a/pyrogram/client/methods/password/change_cloud_password.py b/pyrogram/client/methods/password/change_cloud_password.py index 4b5e86b3..600f58fa 100644 --- a/pyrogram/client/methods/password/change_cloud_password.py +++ b/pyrogram/client/methods/password/change_cloud_password.py @@ -24,7 +24,10 @@ from ...ext import BaseClient class ChangeCloudPassword(BaseClient): - def change_cloud_password(self, current_password: str, new_password: str, new_hint: str = ""): + def change_cloud_password(self, + current_password: str, + new_password: str, + new_hint: str = ""): """Use this method to change your Two-Step Verification password (Cloud Password) with a new one. Args: diff --git a/pyrogram/client/methods/password/enable_cloud_password.py b/pyrogram/client/methods/password/enable_cloud_password.py index 80d527c4..77c731dd 100644 --- a/pyrogram/client/methods/password/enable_cloud_password.py +++ b/pyrogram/client/methods/password/enable_cloud_password.py @@ -24,7 +24,10 @@ from ...ext import BaseClient class EnableCloudPassword(BaseClient): - def enable_cloud_password(self, password: str, hint: str = "", email: str = ""): + def enable_cloud_password(self, + password: str, + hint: str = "", + email: str = ""): """Use this method to enable the Two-Step Verification security feature (Cloud Password) on your account. This password will be asked when you log in on a new device in addition to the SMS code. diff --git a/pyrogram/client/methods/password/remove_cloud_password.py b/pyrogram/client/methods/password/remove_cloud_password.py index 5a9875ff..c82436b7 100644 --- a/pyrogram/client/methods/password/remove_cloud_password.py +++ b/pyrogram/client/methods/password/remove_cloud_password.py @@ -23,7 +23,8 @@ from ...ext import BaseClient class RemoveCloudPassword(BaseClient): - def remove_cloud_password(self, password: str): + def remove_cloud_password(self, + password: str): """Use this method to turn off the Two-Step Verification security feature (Cloud Password) on your account. Args: diff --git a/pyrogram/client/methods/users/delete_user_profile_photos.py b/pyrogram/client/methods/users/delete_user_profile_photos.py index 6f13e17f..4f3c5fde 100644 --- a/pyrogram/client/methods/users/delete_user_profile_photos.py +++ b/pyrogram/client/methods/users/delete_user_profile_photos.py @@ -18,13 +18,15 @@ from base64 import b64decode from struct import unpack +from typing import List, Union from pyrogram.api import functions, types from ...ext import BaseClient class DeleteUserProfilePhotos(BaseClient): - def delete_user_profile_photos(self, id: str or list): + def delete_user_profile_photos(self, + id: Union[str, List[str]]): """Use this method to delete your own profile photos Args: diff --git a/pyrogram/client/methods/users/get_me.py b/pyrogram/client/methods/users/get_me.py index c7d32968..9072a909 100644 --- a/pyrogram/client/methods/users/get_me.py +++ b/pyrogram/client/methods/users/get_me.py @@ -31,7 +31,7 @@ class GetMe(BaseClient): Raises: :class:`Error ` in case of a Telegram RPC error. """ - return pyrogram.User.parse( + return pyrogram.User._parse( self, self.send( functions.users.GetFullUser( diff --git a/pyrogram/client/methods/users/get_user_profile_photos.py b/pyrogram/client/methods/users/get_user_profile_photos.py index 1403eb79..86ecb74d 100644 --- a/pyrogram/client/methods/users/get_user_profile_photos.py +++ b/pyrogram/client/methods/users/get_user_profile_photos.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Union + import pyrogram from pyrogram.api import functions from ...ext import BaseClient @@ -23,7 +25,7 @@ from ...ext import BaseClient class GetUserProfilePhotos(BaseClient): def get_user_profile_photos(self, - user_id: int or str, + user_id: Union[int, str], offset: int = 0, limit: int = 100): """Use this method to get a list of profile pictures for a user. @@ -48,7 +50,7 @@ class GetUserProfilePhotos(BaseClient): Raises: :class:`Error ` in case of a Telegram RPC error. """ - return pyrogram.UserProfilePhotos.parse( + return pyrogram.UserProfilePhotos._parse( self, self.send( functions.photos.GetUserPhotos( diff --git a/pyrogram/client/methods/users/get_users.py b/pyrogram/client/methods/users/get_users.py index 7251476a..075814ca 100644 --- a/pyrogram/client/methods/users/get_users.py +++ b/pyrogram/client/methods/users/get_users.py @@ -16,13 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Iterable, Union + import pyrogram from pyrogram.api import functions from ...ext import BaseClient class GetUsers(BaseClient): - def get_users(self, user_ids): + def get_users(self, + user_ids: Iterable[Union[int, str]]): """Use this method to get information about a user. You can retrieve up to 200 users at once. @@ -53,6 +56,6 @@ class GetUsers(BaseClient): users = [] for i in r: - users.append(pyrogram.User.parse(self, i)) + users.append(pyrogram.User._parse(self, i)) return users if is_iterable else users[0] diff --git a/pyrogram/client/methods/users/set_user_profile_photo.py b/pyrogram/client/methods/users/set_user_profile_photo.py index b3ab66b1..68e174d6 100644 --- a/pyrogram/client/methods/users/set_user_profile_photo.py +++ b/pyrogram/client/methods/users/set_user_profile_photo.py @@ -21,7 +21,8 @@ from ...ext import BaseClient class SetUserProfilePhoto(BaseClient): - def set_user_profile_photo(self, photo: str): + def set_user_profile_photo(self, + photo: str): """Use this method to set a new profile photo. This method only works for Users. diff --git a/pyrogram/client/methods/utilities/download_media.py b/pyrogram/client/methods/utilities/download_media.py index f0ad0396..8c609d98 100644 --- a/pyrogram/client/methods/utilities/download_media.py +++ b/pyrogram/client/methods/utilities/download_media.py @@ -17,14 +17,15 @@ # along with Pyrogram. If not, see . from threading import Event +from typing import Union -from pyrogram.client import types as pyrogram_types +import pyrogram from ...ext import BaseClient class DownloadMedia(BaseClient): def download_media(self, - message: pyrogram_types.Message or str, + message: Union["pyrogram.Message", str], file_name: str = "", block: bool = True, progress: callable = None, @@ -78,9 +79,9 @@ class DownloadMedia(BaseClient): """ error_message = "This message doesn't contain any downloadable media" - if isinstance(message, pyrogram_types.Message): + if isinstance(message, pyrogram.Message): if message.photo: - media = pyrogram_types.Document( + media = pyrogram.Document( file_id=message.photo.sizes[-1].file_id, file_size=message.photo.sizes[-1].file_size, mime_type="", @@ -104,18 +105,18 @@ class DownloadMedia(BaseClient): else: raise ValueError(error_message) elif isinstance(message, ( - pyrogram_types.Photo, - pyrogram_types.PhotoSize, - pyrogram_types.Audio, - pyrogram_types.Document, - pyrogram_types.Video, - pyrogram_types.Voice, - pyrogram_types.VideoNote, - pyrogram_types.Sticker, - pyrogram_types.Animation + pyrogram.Photo, + pyrogram.PhotoSize, + pyrogram.Audio, + pyrogram.Document, + pyrogram.Video, + pyrogram.Voice, + pyrogram.VideoNote, + pyrogram.Sticker, + pyrogram.Animation )): - if isinstance(message, pyrogram_types.Photo): - media = pyrogram_types.Document( + if isinstance(message, pyrogram.Photo): + media = pyrogram.Document( file_id=message.sizes[-1].file_id, file_size=message.sizes[-1].file_size, mime_type="", @@ -125,7 +126,7 @@ class DownloadMedia(BaseClient): else: media = message elif isinstance(message, str): - media = pyrogram_types.Document( + media = pyrogram.Document( file_id=message, file_size=0, mime_type="", diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 0dede950..25c35456 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -483,7 +483,7 @@ class Message(PyrogramType): else: video = pyrogram.Video._parse(client, doc, video_attributes, file_name) elif types.DocumentAttributeSticker in attributes: - sticker = pyrogram.Sticker.parse( + sticker = pyrogram.Sticker._parse( client, doc, attributes.get(types.DocumentAttributeImageSize, None), attributes[types.DocumentAttributeSticker], From bf0b947253f7814c97e5c23efcb3a7c56281beec Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 14:55:48 +0100 Subject: [PATCH 60/62] Type hint all Client methods --- pyrogram/client/client.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 1f06d064..f5aa33ea 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -37,6 +37,7 @@ from importlib import import_module from pathlib import Path from signal import signal, SIGINT, SIGTERM, SIGABRT from threading import Thread +from typing import Union, List from pyrogram.api import functions, types from pyrogram.api.core import Object @@ -152,7 +153,7 @@ class Client(Methods, BaseClient): def __init__(self, session_name: str, - api_id: int or str = None, + api_id: Union[int, str] = None, api_hash: str = None, app_version: str = None, device_model: str = None, @@ -162,7 +163,7 @@ class Client(Methods, BaseClient): proxy: dict = None, test_mode: bool = False, phone_number: str = None, - phone_code: str or callable = None, + phone_code: Union[str, callable] = None, password: str = None, force_sms: bool = False, first_name: str = None, @@ -367,7 +368,7 @@ class Client(Methods, BaseClient): self.start() self.idle() - def add_handler(self, handler, group: int = 0): + def add_handler(self, handler: Handler, group: int = 0): """Use this method to register an update handler. You can register multiple handlers, but at most one handler within a group @@ -391,7 +392,7 @@ class Client(Methods, BaseClient): return handler, group - def remove_handler(self, handler, group: int = 0): + def remove_handler(self, handler: Handler, group: int = 0): """Removes a previously-added update handler. Make sure to provide the right group that the handler was added in. You can use @@ -624,7 +625,9 @@ class Client(Methods, BaseClient): print("Logged in successfully as {}".format(r.user.first_name)) - def fetch_peers(self, entities: list): + def fetch_peers(self, entities: List[Union[types.User, + types.Chat, types.ChatForbidden, + types.Channel, types.ChannelForbidden]]): for entity in entities: if isinstance(entity, types.User): user_id = entity.id @@ -886,7 +889,10 @@ class Client(Methods, BaseClient): log.debug("{} stopped".format(name)) - def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): + 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. @@ -1045,7 +1051,8 @@ class Client(Methods, BaseClient): indent=4 ) - def get_initial_dialogs_chunk(self, offset_date: int = 0): + def get_initial_dialogs_chunk(self, + offset_date: int = 0): while True: try: r = self.send( @@ -1077,7 +1084,8 @@ class Client(Methods, BaseClient): self.get_initial_dialogs_chunk() - def resolve_peer(self, peer_id: int or str): + def resolve_peer(self, + peer_id: Union[int, str]): """Use this method to get the InputPeer of a known peer_id. This is a utility method intended to be used only when working with Raw Functions (i.e: a Telegram API method From a0355a5cc44cd6a8f86298f635a1f64345317a9f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 19 Dec 2018 15:59:15 +0100 Subject: [PATCH 61/62] Type hint all methods return values --- pyrogram/client/methods/chats/delete_chat_photo.py | 2 +- .../client/methods/chats/export_chat_invite_link.py | 2 +- pyrogram/client/methods/chats/get_chat.py | 2 +- pyrogram/client/methods/chats/get_chat_member.py | 11 ++--------- pyrogram/client/methods/chats/get_chat_members.py | 2 +- .../client/methods/chats/get_chat_members_count.py | 2 +- pyrogram/client/methods/chats/get_dialogs.py | 2 +- pyrogram/client/methods/chats/kick_chat_member.py | 2 +- pyrogram/client/methods/chats/pin_chat_message.py | 2 +- pyrogram/client/methods/chats/promote_chat_member.py | 2 +- pyrogram/client/methods/chats/restrict_chat_member.py | 2 +- pyrogram/client/methods/chats/set_chat_description.py | 2 +- pyrogram/client/methods/chats/set_chat_photo.py | 2 +- pyrogram/client/methods/chats/set_chat_title.py | 2 +- pyrogram/client/methods/chats/unban_chat_member.py | 2 +- pyrogram/client/methods/chats/unpin_chat_message.py | 2 +- .../client/methods/decorators/on_callback_query.py | 7 +++++-- .../client/methods/decorators/on_deleted_messages.py | 7 +++++-- pyrogram/client/methods/decorators/on_disconnect.py | 5 +++-- pyrogram/client/methods/decorators/on_message.py | 2 +- pyrogram/client/methods/decorators/on_raw_update.py | 7 +++++-- pyrogram/client/methods/decorators/on_user_status.py | 7 +++++-- pyrogram/client/methods/messages/delete_messages.py | 2 +- .../client/methods/messages/edit_message_caption.py | 2 +- .../client/methods/messages/edit_message_media.py | 2 +- .../methods/messages/edit_message_reply_markup.py | 2 +- pyrogram/client/methods/messages/edit_message_text.py | 2 +- pyrogram/client/methods/messages/forward_messages.py | 10 +++++++--- pyrogram/client/methods/messages/get_messages.py | 2 +- pyrogram/client/methods/messages/send_animation.py | 2 +- pyrogram/client/methods/messages/send_audio.py | 2 +- pyrogram/client/methods/messages/send_contact.py | 2 +- pyrogram/client/methods/messages/send_document.py | 2 +- pyrogram/client/methods/messages/send_location.py | 2 +- pyrogram/client/methods/messages/send_message.py | 2 +- pyrogram/client/methods/messages/send_photo.py | 2 +- pyrogram/client/methods/messages/send_sticker.py | 2 +- pyrogram/client/methods/messages/send_venue.py | 2 +- pyrogram/client/methods/messages/send_video.py | 2 +- pyrogram/client/methods/messages/send_video_note.py | 2 +- pyrogram/client/methods/messages/send_voice.py | 2 +- .../client/methods/password/change_cloud_password.py | 2 +- .../client/methods/password/enable_cloud_password.py | 2 +- .../client/methods/password/remove_cloud_password.py | 2 +- .../methods/users/delete_user_profile_photos.py | 2 +- pyrogram/client/methods/users/get_me.py | 2 +- .../client/methods/users/get_user_profile_photos.py | 2 +- pyrogram/client/methods/users/get_users.py | 8 ++++---- .../client/methods/users/set_user_profile_photo.py | 2 +- pyrogram/client/methods/utilities/download_media.py | 2 +- pyrogram/client/types/user_and_chats/chat_member.py | 2 ++ 51 files changed, 80 insertions(+), 68 deletions(-) diff --git a/pyrogram/client/methods/chats/delete_chat_photo.py b/pyrogram/client/methods/chats/delete_chat_photo.py index d4658e4d..a33cae05 100644 --- a/pyrogram/client/methods/chats/delete_chat_photo.py +++ b/pyrogram/client/methods/chats/delete_chat_photo.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class DeleteChatPhoto(BaseClient): def delete_chat_photo(self, - chat_id: Union[int, str]): + chat_id: Union[int, str]) -> bool: """Use this method to delete a chat photo. Photos can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/export_chat_invite_link.py b/pyrogram/client/methods/chats/export_chat_invite_link.py index 8f068586..f458e91e 100644 --- a/pyrogram/client/methods/chats/export_chat_invite_link.py +++ b/pyrogram/client/methods/chats/export_chat_invite_link.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class ExportChatInviteLink(BaseClient): def export_chat_invite_link(self, - chat_id: Union[int, str]): + chat_id: Union[int, str]) -> str: """Use this method to generate a new invite link for a chat; any previously generated link is revoked. You must be an administrator in the chat for this to work and have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/get_chat.py b/pyrogram/client/methods/chats/get_chat.py index 24651d37..f5bf19a9 100644 --- a/pyrogram/client/methods/chats/get_chat.py +++ b/pyrogram/client/methods/chats/get_chat.py @@ -25,7 +25,7 @@ from ...ext import BaseClient class GetChat(BaseClient): def get_chat(self, - chat_id: Union[int, str]): + chat_id: Union[int, str]) -> "pyrogram.Chat": """Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.) diff --git a/pyrogram/client/methods/chats/get_chat_member.py b/pyrogram/client/methods/chats/get_chat_member.py index 7708f6fc..9f13eac2 100644 --- a/pyrogram/client/methods/chats/get_chat_member.py +++ b/pyrogram/client/methods/chats/get_chat_member.py @@ -26,7 +26,7 @@ from ...ext import BaseClient class GetChatMember(BaseClient): def get_chat_member(self, chat_id: Union[int, str], - user_id: Union[int, str]): + user_id: Union[int, str]) -> "pyrogram.ChatMember": """Use this method to get information about one member of a chat. Args: @@ -67,13 +67,6 @@ class GetChatMember(BaseClient): ) ) - return pyrogram.ChatMembers._parse( - self, - types.channels.ChannelParticipants( - count=1, - participants=[r.participant], - users=r.users - ) - ).chat_members[0] + return pyrogram.ChatMember._parse(self, r.participant, r.users[0]) else: raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id)) diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index 031ea46b..8173423b 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -38,7 +38,7 @@ class GetChatMembers(BaseClient): offset: int = 0, limit: int = 200, query: str = "", - filter: str = Filters.ALL): + filter: str = Filters.ALL) -> "pyrogram.ChatMembers": """Use this method to get the members list of a chat. A chat can be either a basic group, a supergroup or a channel. diff --git a/pyrogram/client/methods/chats/get_chat_members_count.py b/pyrogram/client/methods/chats/get_chat_members_count.py index 01bbd835..37eb69cb 100644 --- a/pyrogram/client/methods/chats/get_chat_members_count.py +++ b/pyrogram/client/methods/chats/get_chat_members_count.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class GetChatMembersCount(BaseClient): def get_chat_members_count(self, - chat_id: Union[int, str]): + chat_id: Union[int, str]) -> int: """Use this method to get the number of members in a chat. Args: diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index 87cd5be2..0e04423c 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -25,7 +25,7 @@ class GetDialogs(BaseClient): def get_dialogs(self, offset_dialog: "pyrogram.Dialog" = None, limit: int = 100, - pinned_only: bool = False): + pinned_only: bool = False) -> "pyrogram.Dialogs": """Use this method to get the user's dialogs You can get up to 100 dialogs at once. diff --git a/pyrogram/client/methods/chats/kick_chat_member.py b/pyrogram/client/methods/chats/kick_chat_member.py index a7459110..4ea4d6bb 100644 --- a/pyrogram/client/methods/chats/kick_chat_member.py +++ b/pyrogram/client/methods/chats/kick_chat_member.py @@ -27,7 +27,7 @@ class KickChatMember(BaseClient): def kick_chat_member(self, chat_id: Union[int, str], user_id: Union[int, str], - until_date: int = 0): + until_date: int = 0) -> "pyrogram.Message": """Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. You must be an administrator in the chat for this to work and must diff --git a/pyrogram/client/methods/chats/pin_chat_message.py b/pyrogram/client/methods/chats/pin_chat_message.py index 1ef07d34..5a4ab50d 100644 --- a/pyrogram/client/methods/chats/pin_chat_message.py +++ b/pyrogram/client/methods/chats/pin_chat_message.py @@ -26,7 +26,7 @@ class PinChatMessage(BaseClient): def pin_chat_message(self, chat_id: Union[int, str], message_id: int, - disable_notification: bool = None): + disable_notification: bool = None) -> bool: """Use this method to pin a message in a supergroup or a channel. You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel. diff --git a/pyrogram/client/methods/chats/promote_chat_member.py b/pyrogram/client/methods/chats/promote_chat_member.py index d22a8ea7..18453b58 100644 --- a/pyrogram/client/methods/chats/promote_chat_member.py +++ b/pyrogram/client/methods/chats/promote_chat_member.py @@ -33,7 +33,7 @@ class PromoteChatMember(BaseClient): can_invite_users: bool = True, can_restrict_members: bool = True, can_pin_messages: bool = False, - can_promote_members: bool = False): + can_promote_members: bool = False) -> bool: """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. Pass False for all boolean parameters to demote a user. diff --git a/pyrogram/client/methods/chats/restrict_chat_member.py b/pyrogram/client/methods/chats/restrict_chat_member.py index c772d100..f9670250 100644 --- a/pyrogram/client/methods/chats/restrict_chat_member.py +++ b/pyrogram/client/methods/chats/restrict_chat_member.py @@ -30,7 +30,7 @@ class RestrictChatMember(BaseClient): can_send_messages: bool = False, can_send_media_messages: bool = False, can_send_other_messages: bool = False, - can_add_web_page_previews: bool = False): + can_add_web_page_previews: bool = False) -> bool: """Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user. diff --git a/pyrogram/client/methods/chats/set_chat_description.py b/pyrogram/client/methods/chats/set_chat_description.py index 1606fe7e..7cf91da5 100644 --- a/pyrogram/client/methods/chats/set_chat_description.py +++ b/pyrogram/client/methods/chats/set_chat_description.py @@ -25,7 +25,7 @@ from ...ext import BaseClient class SetChatDescription(BaseClient): def set_chat_description(self, chat_id: Union[int, str], - description: str): + description: str) -> bool: """Use this method to change the description of a supergroup or a channel. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/set_chat_photo.py b/pyrogram/client/methods/chats/set_chat_photo.py index ed058000..1d4ab5e5 100644 --- a/pyrogram/client/methods/chats/set_chat_photo.py +++ b/pyrogram/client/methods/chats/set_chat_photo.py @@ -28,7 +28,7 @@ from ...ext import BaseClient class SetChatPhoto(BaseClient): def set_chat_photo(self, chat_id: Union[int, str], - photo: str): + photo: str) -> bool: """Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/set_chat_title.py b/pyrogram/client/methods/chats/set_chat_title.py index 58d4179c..af2b6e77 100644 --- a/pyrogram/client/methods/chats/set_chat_title.py +++ b/pyrogram/client/methods/chats/set_chat_title.py @@ -25,7 +25,7 @@ from ...ext import BaseClient class SetChatTitle(BaseClient): def set_chat_title(self, chat_id: Union[int, str], - title: str): + title: str) -> bool: """Use this method to change the title of a chat. Titles can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. diff --git a/pyrogram/client/methods/chats/unban_chat_member.py b/pyrogram/client/methods/chats/unban_chat_member.py index 428e6fe8..3513f38d 100644 --- a/pyrogram/client/methods/chats/unban_chat_member.py +++ b/pyrogram/client/methods/chats/unban_chat_member.py @@ -25,7 +25,7 @@ from ...ext import BaseClient class UnbanChatMember(BaseClient): def unban_chat_member(self, chat_id: Union[int, str], - user_id: Union[int, str]): + user_id: Union[int, str]) -> bool: """Use this method to unban a previously kicked user in a supergroup or channel. The user will **not** return to the group or channel automatically, but will be able to join via link, etc. You must be an administrator for this to work. diff --git a/pyrogram/client/methods/chats/unpin_chat_message.py b/pyrogram/client/methods/chats/unpin_chat_message.py index bd6a22f8..435f38d7 100644 --- a/pyrogram/client/methods/chats/unpin_chat_message.py +++ b/pyrogram/client/methods/chats/unpin_chat_message.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class UnpinChatMessage(BaseClient): def unpin_chat_message(self, - chat_id: Union[int, str]): + chat_id: Union[int, str]) -> bool: """Use this method to unpin a message in a supergroup or a channel. You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel. diff --git a/pyrogram/client/methods/decorators/on_callback_query.py b/pyrogram/client/methods/decorators/on_callback_query.py index f34119e2..8c152706 100644 --- a/pyrogram/client/methods/decorators/on_callback_query.py +++ b/pyrogram/client/methods/decorators/on_callback_query.py @@ -16,15 +16,18 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Tuple + import pyrogram from pyrogram.client.filters.filter import Filter +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient class OnCallbackQuery(BaseClient): def on_callback_query(self=None, filters=None, - group: int = 0): + group: int = 0) -> callable: """Use this decorator to automatically register a function for handling callback queries. This does the same thing as :meth:`add_handler` using the :class:`CallbackQueryHandler`. @@ -45,7 +48,7 @@ class OnCallbackQuery(BaseClient): The group identifier, defaults to 0. """ - def decorator(func): + def decorator(func: callable) -> Tuple[Handler, int]: if isinstance(func, tuple): func = func[0].callback diff --git a/pyrogram/client/methods/decorators/on_deleted_messages.py b/pyrogram/client/methods/decorators/on_deleted_messages.py index 406563f2..84abc92e 100644 --- a/pyrogram/client/methods/decorators/on_deleted_messages.py +++ b/pyrogram/client/methods/decorators/on_deleted_messages.py @@ -16,15 +16,18 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Tuple + import pyrogram from pyrogram.client.filters.filter import Filter +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient class OnDeletedMessages(BaseClient): def on_deleted_messages(self=None, filters=None, - group: int = 0): + group: int = 0) -> callable: """Use this decorator to automatically register a function for handling deleted messages. This does the same thing as :meth:`add_handler` using the :class:`DeletedMessagesHandler`. @@ -45,7 +48,7 @@ class OnDeletedMessages(BaseClient): The group identifier, defaults to 0. """ - def decorator(func): + def decorator(func: callable) -> Tuple[Handler, int]: if isinstance(func, tuple): func = func[0].callback diff --git a/pyrogram/client/methods/decorators/on_disconnect.py b/pyrogram/client/methods/decorators/on_disconnect.py index e2288619..56796bf5 100644 --- a/pyrogram/client/methods/decorators/on_disconnect.py +++ b/pyrogram/client/methods/decorators/on_disconnect.py @@ -17,17 +17,18 @@ # along with Pyrogram. If not, see . import pyrogram +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient class OnDisconnect(BaseClient): - def on_disconnect(self=None): + def on_disconnect(self=None) -> callable: """Use this decorator to automatically register a function for handling disconnections. This does the same thing as :meth:`add_handler` using the :class:`DisconnectHandler`. """ - def decorator(func): + def decorator(func: callable) -> Handler: handler = pyrogram.DisconnectHandler(func) if self is not None: diff --git a/pyrogram/client/methods/decorators/on_message.py b/pyrogram/client/methods/decorators/on_message.py index ec0e11e8..68ed1fab 100644 --- a/pyrogram/client/methods/decorators/on_message.py +++ b/pyrogram/client/methods/decorators/on_message.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class OnMessage(BaseClient): def on_message(self=None, filters=None, - group: int = 0): + group: int = 0) -> callable: """Use this decorator to automatically register a function for handling messages. This does the same thing as :meth:`add_handler` using the :class:`MessageHandler`. diff --git a/pyrogram/client/methods/decorators/on_raw_update.py b/pyrogram/client/methods/decorators/on_raw_update.py index 8b2723df..ce2584d5 100644 --- a/pyrogram/client/methods/decorators/on_raw_update.py +++ b/pyrogram/client/methods/decorators/on_raw_update.py @@ -16,13 +16,16 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Tuple + import pyrogram +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient class OnRawUpdate(BaseClient): def on_raw_update(self=None, - group: int = 0): + group: int = 0) -> callable: """Use this decorator to automatically register a function for handling raw updates. This does the same thing as :meth:`add_handler` using the :class:`RawUpdateHandler`. @@ -39,7 +42,7 @@ class OnRawUpdate(BaseClient): The group identifier, defaults to 0. """ - def decorator(func): + def decorator(func: callable) -> Tuple[Handler, int]: if isinstance(func, tuple): func = func[0].callback diff --git a/pyrogram/client/methods/decorators/on_user_status.py b/pyrogram/client/methods/decorators/on_user_status.py index 6ae47a95..c552e706 100644 --- a/pyrogram/client/methods/decorators/on_user_status.py +++ b/pyrogram/client/methods/decorators/on_user_status.py @@ -16,15 +16,18 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Tuple + import pyrogram from pyrogram.client.filters.filter import Filter +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient class OnUserStatus(BaseClient): def on_user_status(self=None, filters=None, - group: int = 0): + group: int = 0) -> callable: """Use this decorator to automatically register a function for handling user status updates. This does the same thing as :meth:`add_handler` using the :class:`UserStatusHandler`. @@ -44,7 +47,7 @@ class OnUserStatus(BaseClient): The group identifier, defaults to 0. """ - def decorator(func): + def decorator(func: callable) -> Tuple[Handler, int]: if isinstance(func, tuple): func = func[0].callback diff --git a/pyrogram/client/methods/messages/delete_messages.py b/pyrogram/client/methods/messages/delete_messages.py index 6aaf710a..030a1663 100644 --- a/pyrogram/client/methods/messages/delete_messages.py +++ b/pyrogram/client/methods/messages/delete_messages.py @@ -26,7 +26,7 @@ class DeleteMessages(BaseClient): def delete_messages(self, chat_id: Union[int, str], message_ids: Iterable[int], - revoke: bool = True): + revoke: bool = True) -> bool: """Use this method to delete messages, including service messages, with the following limitations: - A message can only be deleted if it was sent less than 48 hours ago. diff --git a/pyrogram/client/methods/messages/edit_message_caption.py b/pyrogram/client/methods/messages/edit_message_caption.py index 938e7af5..7709a3d8 100644 --- a/pyrogram/client/methods/messages/edit_message_caption.py +++ b/pyrogram/client/methods/messages/edit_message_caption.py @@ -29,7 +29,7 @@ class EditMessageCaption(BaseClient): message_id: int, caption: str, parse_mode: str = "", - reply_markup: "pyrogram.InlineKeyboardMarkup" = None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None) -> "pyrogram.Message": """Use this method to edit captions of messages. Args: diff --git a/pyrogram/client/methods/messages/edit_message_media.py b/pyrogram/client/methods/messages/edit_message_media.py index bdfbf353..50fd1f48 100644 --- a/pyrogram/client/methods/messages/edit_message_media.py +++ b/pyrogram/client/methods/messages/edit_message_media.py @@ -38,7 +38,7 @@ class EditMessageMedia(BaseClient): chat_id: Union[int, str], message_id: int, media: InputMedia, - reply_markup: "pyrogram.InlineKeyboardMarkup" = None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None) -> "pyrogram.Message": """Use this method to edit audio, document, photo, or video messages. If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, diff --git a/pyrogram/client/methods/messages/edit_message_reply_markup.py b/pyrogram/client/methods/messages/edit_message_reply_markup.py index 31d8f0b7..f46dd2f4 100644 --- a/pyrogram/client/methods/messages/edit_message_reply_markup.py +++ b/pyrogram/client/methods/messages/edit_message_reply_markup.py @@ -27,7 +27,7 @@ class EditMessageReplyMarkup(BaseClient): def edit_message_reply_markup(self, chat_id: Union[int, str], message_id: int, - reply_markup: "pyrogram.InlineKeyboardMarkup" = None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None) -> "pyrogram.Message": """Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). Args: diff --git a/pyrogram/client/methods/messages/edit_message_text.py b/pyrogram/client/methods/messages/edit_message_text.py index 688c7ce2..173caa93 100644 --- a/pyrogram/client/methods/messages/edit_message_text.py +++ b/pyrogram/client/methods/messages/edit_message_text.py @@ -30,7 +30,7 @@ class EditMessageText(BaseClient): text: str, parse_mode: str = "", disable_web_page_preview: bool = None, - reply_markup: "pyrogram.InlineKeyboardMarkup" = None): + reply_markup: "pyrogram.InlineKeyboardMarkup" = None) -> "pyrogram.Message": """Use this method to edit text messages. Args: diff --git a/pyrogram/client/methods/messages/forward_messages.py b/pyrogram/client/methods/messages/forward_messages.py index cafa468b..f8379c0c 100644 --- a/pyrogram/client/methods/messages/forward_messages.py +++ b/pyrogram/client/methods/messages/forward_messages.py @@ -28,7 +28,7 @@ class ForwardMessages(BaseClient): chat_id: Union[int, str], from_chat_id: Union[int, str], message_ids: Iterable[int], - disable_notification: bool = None): + disable_notification: bool = None) -> "pyrogram.Messages": """Use this method to forward messages of any kind. Args: @@ -51,7 +51,7 @@ class ForwardMessages(BaseClient): Users will receive a notification with no sound. Returns: - On success and in case *message_ids* was a list, the returned value will be a list of the forwarded + On success and in case *message_ids* was an iterable, the returned value will be a list of the forwarded :obj:`Messages ` even if a list contains just one element, otherwise if *message_ids* was an integer, the single forwarded :obj:`Message ` is returned. @@ -86,4 +86,8 @@ class ForwardMessages(BaseClient): ) ) - return messages if is_iterable else messages[0] + return pyrogram.Messages( + client=self, + total_count=len(messages), + messages=messages + ) if is_iterable else messages[0] diff --git a/pyrogram/client/methods/messages/get_messages.py b/pyrogram/client/methods/messages/get_messages.py index 9f67d9a0..da2f6578 100644 --- a/pyrogram/client/methods/messages/get_messages.py +++ b/pyrogram/client/methods/messages/get_messages.py @@ -28,7 +28,7 @@ class GetMessages(BaseClient): chat_id: Union[int, str], message_ids: Union[int, Iterable[int]] = None, reply_to_message_ids: Union[int, Iterable[int]] = None, - replies: int = 1): + replies: int = 1) -> "pyrogram.Messages": """Use this method to get one or more messages that belong to a specific chat. You can retrieve up to 200 messages at once. diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py index 8e240f5b..7b1467f2 100644 --- a/pyrogram/client/methods/messages/send_animation.py +++ b/pyrogram/client/methods/messages/send_animation.py @@ -45,7 +45,7 @@ class SendAnimation(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send animation files (animation or H.264/MPEG-4 AVC video without sound). Args: diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 405d0561..685e2a2b 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -45,7 +45,7 @@ class SendAudio(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send audio files. For sending voice messages, use the :obj:`send_voice()` method instead. diff --git a/pyrogram/client/methods/messages/send_contact.py b/pyrogram/client/methods/messages/send_contact.py index d7f93743..5312f0ac 100644 --- a/pyrogram/client/methods/messages/send_contact.py +++ b/pyrogram/client/methods/messages/send_contact.py @@ -35,7 +35,7 @@ class SendContact(BaseClient): reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", - "pyrogram.ForceReply"] = None): + "pyrogram.ForceReply"] = None) -> "pyrogram.Message": """Use this method to send phone contacts. Args: diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py index d7bb62d1..da61aaea 100644 --- a/pyrogram/client/methods/messages/send_document.py +++ b/pyrogram/client/methods/messages/send_document.py @@ -42,7 +42,7 @@ class SendDocument(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send general files. Args: diff --git a/pyrogram/client/methods/messages/send_location.py b/pyrogram/client/methods/messages/send_location.py index 0efdb1df..dbd04e7a 100644 --- a/pyrogram/client/methods/messages/send_location.py +++ b/pyrogram/client/methods/messages/send_location.py @@ -33,7 +33,7 @@ class SendLocation(BaseClient): reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", - "pyrogram.ForceReply"] = None): + "pyrogram.ForceReply"] = None) -> "pyrogram.Message": """Use this method to send points on the map. Args: diff --git a/pyrogram/client/methods/messages/send_message.py b/pyrogram/client/methods/messages/send_message.py index a04a67f2..982ce1c7 100644 --- a/pyrogram/client/methods/messages/send_message.py +++ b/pyrogram/client/methods/messages/send_message.py @@ -34,7 +34,7 @@ class SendMessage(BaseClient): reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", - "pyrogram.ForceReply"] = None): + "pyrogram.ForceReply"] = None) -> "pyrogram.Message": """Use this method to send text messages. Args: diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py index 5b7b3268..00d06ea1 100644 --- a/pyrogram/client/methods/messages/send_photo.py +++ b/pyrogram/client/methods/messages/send_photo.py @@ -41,7 +41,7 @@ class SendPhoto(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send photos. Args: diff --git a/pyrogram/client/methods/messages/send_sticker.py b/pyrogram/client/methods/messages/send_sticker.py index 550bcddb..045e1b21 100644 --- a/pyrogram/client/methods/messages/send_sticker.py +++ b/pyrogram/client/methods/messages/send_sticker.py @@ -38,7 +38,7 @@ class SendSticker(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send .webp stickers. Args: diff --git a/pyrogram/client/methods/messages/send_venue.py b/pyrogram/client/methods/messages/send_venue.py index 35d725d1..fca07898 100644 --- a/pyrogram/client/methods/messages/send_venue.py +++ b/pyrogram/client/methods/messages/send_venue.py @@ -37,7 +37,7 @@ class SendVenue(BaseClient): reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", - "pyrogram.ForceReply"] = None): + "pyrogram.ForceReply"] = None) -> "pyrogram.Message": """Use this method to send information about a venue. Args: diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index c6936cff..fa0a51a6 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -46,7 +46,7 @@ class SendVideo(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send video files. Args: diff --git a/pyrogram/client/methods/messages/send_video_note.py b/pyrogram/client/methods/messages/send_video_note.py index 75345607..dd64c14a 100644 --- a/pyrogram/client/methods/messages/send_video_note.py +++ b/pyrogram/client/methods/messages/send_video_note.py @@ -42,7 +42,7 @@ class SendVideoNote(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send video messages. Args: diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py index 52cbc833..27621fa0 100644 --- a/pyrogram/client/methods/messages/send_voice.py +++ b/pyrogram/client/methods/messages/send_voice.py @@ -42,7 +42,7 @@ class SendVoice(BaseClient): "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> "pyrogram.Message": """Use this method to send audio files. Args: diff --git a/pyrogram/client/methods/password/change_cloud_password.py b/pyrogram/client/methods/password/change_cloud_password.py index 600f58fa..d7d0e1fa 100644 --- a/pyrogram/client/methods/password/change_cloud_password.py +++ b/pyrogram/client/methods/password/change_cloud_password.py @@ -27,7 +27,7 @@ class ChangeCloudPassword(BaseClient): def change_cloud_password(self, current_password: str, new_password: str, - new_hint: str = ""): + new_hint: str = "") -> bool: """Use this method to change your Two-Step Verification password (Cloud Password) with a new one. Args: diff --git a/pyrogram/client/methods/password/enable_cloud_password.py b/pyrogram/client/methods/password/enable_cloud_password.py index 77c731dd..4eb8df6a 100644 --- a/pyrogram/client/methods/password/enable_cloud_password.py +++ b/pyrogram/client/methods/password/enable_cloud_password.py @@ -27,7 +27,7 @@ class EnableCloudPassword(BaseClient): def enable_cloud_password(self, password: str, hint: str = "", - email: str = ""): + email: str = "") -> bool: """Use this method to enable the Two-Step Verification security feature (Cloud Password) on your account. This password will be asked when you log in on a new device in addition to the SMS code. diff --git a/pyrogram/client/methods/password/remove_cloud_password.py b/pyrogram/client/methods/password/remove_cloud_password.py index c82436b7..3dab720d 100644 --- a/pyrogram/client/methods/password/remove_cloud_password.py +++ b/pyrogram/client/methods/password/remove_cloud_password.py @@ -24,7 +24,7 @@ from ...ext import BaseClient class RemoveCloudPassword(BaseClient): def remove_cloud_password(self, - password: str): + password: str) -> bool: """Use this method to turn off the Two-Step Verification security feature (Cloud Password) on your account. Args: diff --git a/pyrogram/client/methods/users/delete_user_profile_photos.py b/pyrogram/client/methods/users/delete_user_profile_photos.py index 4f3c5fde..764c3f3e 100644 --- a/pyrogram/client/methods/users/delete_user_profile_photos.py +++ b/pyrogram/client/methods/users/delete_user_profile_photos.py @@ -26,7 +26,7 @@ from ...ext import BaseClient class DeleteUserProfilePhotos(BaseClient): def delete_user_profile_photos(self, - id: Union[str, List[str]]): + id: Union[str, List[str]]) -> bool: """Use this method to delete your own profile photos Args: diff --git a/pyrogram/client/methods/users/get_me.py b/pyrogram/client/methods/users/get_me.py index 9072a909..3f3e85c3 100644 --- a/pyrogram/client/methods/users/get_me.py +++ b/pyrogram/client/methods/users/get_me.py @@ -22,7 +22,7 @@ from ...ext import BaseClient class GetMe(BaseClient): - def get_me(self): + def get_me(self) -> pyrogram.User: """A simple method for testing your authorization. Requires no parameters. Returns: diff --git a/pyrogram/client/methods/users/get_user_profile_photos.py b/pyrogram/client/methods/users/get_user_profile_photos.py index 86ecb74d..c097aced 100644 --- a/pyrogram/client/methods/users/get_user_profile_photos.py +++ b/pyrogram/client/methods/users/get_user_profile_photos.py @@ -27,7 +27,7 @@ class GetUserProfilePhotos(BaseClient): def get_user_profile_photos(self, user_id: Union[int, str], offset: int = 0, - limit: int = 100): + limit: int = 100) -> pyrogram.UserProfilePhotos: """Use this method to get a list of profile pictures for a user. Args: diff --git a/pyrogram/client/methods/users/get_users.py b/pyrogram/client/methods/users/get_users.py index 075814ca..3ee1bc61 100644 --- a/pyrogram/client/methods/users/get_users.py +++ b/pyrogram/client/methods/users/get_users.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Iterable, Union +from typing import Iterable, Union, List import pyrogram from pyrogram.api import functions @@ -25,7 +25,7 @@ from ...ext import BaseClient class GetUsers(BaseClient): def get_users(self, - user_ids: Iterable[Union[int, str]]): + user_ids: Iterable[Union[int, str]]) -> Union[pyrogram.User or List[pyrogram.User]]: """Use this method to get information about a user. You can retrieve up to 200 users at once. @@ -36,9 +36,9 @@ class GetUsers(BaseClient): Iterators and Generators are also accepted. Returns: - On success and in case *user_ids* was a list, the returned value will be a list of the requested + On success and in case *user_ids* was an iterable, the returned value will be a list of the requested :obj:`Users ` even if a list contains just one element, otherwise if - *user_ids* was an integer, the single requested :obj:`User` is returned. + *user_ids* was an integer or string, the single requested :obj:`User` is returned. Raises: :class:`Error ` in case of a Telegram RPC error. diff --git a/pyrogram/client/methods/users/set_user_profile_photo.py b/pyrogram/client/methods/users/set_user_profile_photo.py index 68e174d6..f2a2c302 100644 --- a/pyrogram/client/methods/users/set_user_profile_photo.py +++ b/pyrogram/client/methods/users/set_user_profile_photo.py @@ -22,7 +22,7 @@ from ...ext import BaseClient class SetUserProfilePhoto(BaseClient): def set_user_profile_photo(self, - photo: str): + photo: str) -> bool: """Use this method to set a new profile photo. This method only works for Users. diff --git a/pyrogram/client/methods/utilities/download_media.py b/pyrogram/client/methods/utilities/download_media.py index 8c609d98..33d72f82 100644 --- a/pyrogram/client/methods/utilities/download_media.py +++ b/pyrogram/client/methods/utilities/download_media.py @@ -29,7 +29,7 @@ class DownloadMedia(BaseClient): file_name: str = "", block: bool = True, progress: callable = None, - progress_args: tuple = ()): + progress_args: tuple = ()) -> Union[str, None]: """Use this method to download the media from a Message. Args: diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index b778d4d8..fa43f526 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -121,6 +121,8 @@ class ChatMember(PyrogramType): @staticmethod def _parse(client, member, user) -> "ChatMember": + user = pyrogram.User._parse(client, user) + if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)): return ChatMember(user=user, status="member", client=client) From 4bf6831be85ab07d56924eaf320b9090019004c3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 22 Dec 2018 11:22:58 +0100 Subject: [PATCH 62/62] Fix some non-importable types for type hint --- pyrogram/client/methods/users/get_me.py | 2 +- pyrogram/client/methods/users/get_user_profile_photos.py | 2 +- pyrogram/client/methods/users/get_users.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/users/get_me.py b/pyrogram/client/methods/users/get_me.py index 3f3e85c3..211e2ee9 100644 --- a/pyrogram/client/methods/users/get_me.py +++ b/pyrogram/client/methods/users/get_me.py @@ -22,7 +22,7 @@ from ...ext import BaseClient class GetMe(BaseClient): - def get_me(self) -> pyrogram.User: + def get_me(self) -> "pyrogram.User": """A simple method for testing your authorization. Requires no parameters. Returns: diff --git a/pyrogram/client/methods/users/get_user_profile_photos.py b/pyrogram/client/methods/users/get_user_profile_photos.py index c097aced..2108f2d6 100644 --- a/pyrogram/client/methods/users/get_user_profile_photos.py +++ b/pyrogram/client/methods/users/get_user_profile_photos.py @@ -27,7 +27,7 @@ class GetUserProfilePhotos(BaseClient): def get_user_profile_photos(self, user_id: Union[int, str], offset: int = 0, - limit: int = 100) -> pyrogram.UserProfilePhotos: + limit: int = 100) -> "pyrogram.UserProfilePhotos": """Use this method to get a list of profile pictures for a user. Args: diff --git a/pyrogram/client/methods/users/get_users.py b/pyrogram/client/methods/users/get_users.py index 3ee1bc61..d7b80579 100644 --- a/pyrogram/client/methods/users/get_users.py +++ b/pyrogram/client/methods/users/get_users.py @@ -25,7 +25,7 @@ from ...ext import BaseClient class GetUsers(BaseClient): def get_users(self, - user_ids: Iterable[Union[int, str]]) -> Union[pyrogram.User or List[pyrogram.User]]: + user_ids: Iterable[Union[int, str]]) -> Union["pyrogram.User", List["pyrogram.User"]]: """Use this method to get information about a user. You can retrieve up to 200 users at once.