2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Add PyrogramType

This commit is contained in:
Dan 2018-12-16 15:24:51 +01:00
parent 5ecbfbf25f
commit 6ab1c87050
21 changed files with 88 additions and 79 deletions

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode 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). """This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
Args: Args:
@ -56,8 +56,6 @@ class Animation(Object):
Date the animation was sent in Unix time. Date the animation was sent in Unix time.
""" """
ID = 0xb0700025
def __init__(self, file_id: str, width: int, height: int, duration: int, *, 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, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None,
client=None, raw=None): client=None, raw=None):

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode 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. """This object represents an audio file to be treated as music by the Telegram clients.
Args: Args:
@ -56,8 +56,6 @@ class Audio(Object):
Title of the audio as defined by sender or by audio tags. Title of the audio as defined by sender or by audio tags.
""" """
ID = 0xb0700006
def __init__(self, file_id: str, duration: int, *, 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, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None,
performer: str = None, title: str = None, performer: str = None, title: str = None,

View File

@ -17,10 +17,10 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types 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. """This object represents a phone contact.
Args: Args:
@ -40,8 +40,6 @@ class Contact(Object):
Additional data about the contact in the form of a vCard. Additional data about the contact in the form of a vCard.
""" """
ID = 0xb0700011
def __init__(self, phone_number: str, first_name: str, *, def __init__(self, phone_number: str, first_name: str, *,
last_name: str = None, user_id: int = None, vcard: str = None, last_name: str = None, user_id: int = None, vcard: str = None,
client=None, raw=None): client=None, raw=None):

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode 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, ...). """This object represents a general file (as opposed to photos, voice messages, audio files, ...).
Args: Args:
@ -47,8 +47,6 @@ class Document(Object):
Date the document was sent in Unix time. Date the document was sent in Unix time.
""" """
ID = 0xb0700007
def __init__(self, file_id: str, *, def __init__(self, file_id: str, *,
thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None,
client=None, raw=None): client=None, raw=None):

View File

@ -17,10 +17,10 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types 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. """This object represents a point on the map.
Args: Args:
@ -31,8 +31,6 @@ class Location(Object):
Latitude as defined by sender. Latitude as defined by sender.
""" """
ID = 0xb0700012
def __init__(self, longitude: float, latitude: float, *, def __init__(self, longitude: float, latitude: float, *,
client=None, raw=None): client=None, raw=None):
self.longitude = longitude self.longitude = longitude

View File

@ -18,18 +18,18 @@
import pyrogram import pyrogram
from pyrogram.api import types, functions from pyrogram.api import types, functions
from pyrogram.api.core import Object
from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid
from .contact import Contact from .contact import Contact
from .location import Location from .location import Location
from .message_entity import MessageEntity from .message_entity import MessageEntity
from ..messages_and_media.photo import Photo from ..messages_and_media.photo import Photo
from ..pyrogram_type import PyrogramType
from ..user_and_chats.chat import Chat from ..user_and_chats.chat import Chat
from ..user_and_chats.user import User from ..user_and_chats.user import User
from ...ext.utils import Str from ...ext.utils import Str
class Message(Object): class Message(PyrogramType):
"""This object represents a message. """This object represents a message.
Args: Args:
@ -224,7 +224,6 @@ class Message(Object):
""" """
# TODO: Add game missing field. Also invoice, successful_payment, connected_website # TODO: Add game missing field. Also invoice, successful_payment, connected_website
ID = 0xb0700003
def __init__(self, message_id: int, *, def __init__(self, message_id: int, *,
date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, 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 set_name = None
sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes, sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes,
set_name, sticker_attribute, file_name) set_name, sticker_attribute, file_name)
else: else:
document = pyrogram.Document.parse(client, doc, file_name) document = pyrogram.Document.parse(client, doc, file_name)
elif isinstance(media, types.MessageMediaWebPage): elif isinstance(media, types.MessageMediaWebPage):

View File

@ -17,11 +17,11 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object from ..pyrogram_type import PyrogramType
from ..user_and_chats.user import User from ..user_and_chats.user import User
class MessageEntity(Object): class MessageEntity(PyrogramType):
"""This object represents one special entity in a text message. """This object represents one special entity in a text message.
For example, hashtags, usernames, URLs, etc. For example, hashtags, usernames, URLs, etc.
@ -45,8 +45,6 @@ class MessageEntity(Object):
For "text_mention" only, the mentioned user. For "text_mention" only, the mentioned user.
""" """
ID = 0xb0700004
ENTITIES = { ENTITIES = {
types.MessageEntityMention.ID: "mention", types.MessageEntityMention.ID: "mention",
types.MessageEntityHashtag.ID: "hashtag", types.MessageEntityHashtag.ID: "hashtag",

View File

@ -16,10 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api.core import Object from ..pyrogram_type import PyrogramType
class Messages(Object): class Messages(PyrogramType):
"""This object represents a chat's messages. """This object represents a chat's messages.
Args: Args:
@ -30,8 +30,6 @@ class Messages(Object):
Requested messages. Requested messages.
""" """
ID = 0xb0700026
def __init__(self, total_count: int, messages: list): def __init__(self, total_count: int, messages: list):
self.total_count = total_count self.total_count = total_count
self.messages = messages self.messages = messages

View File

@ -20,12 +20,12 @@ from base64 import b64encode
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode from ...ext.utils import encode
class Photo(Object): class Photo(PyrogramType):
"""This object represents a Photo. """This object represents a Photo.
Args: Args:
@ -39,8 +39,6 @@ class Photo(Object):
Available sizes of this photo. Available sizes of this photo.
""" """
ID = 0xb0700027
def __init__(self, id: str, date: int, sizes: list, *, def __init__(self, id: str, date: int, sizes: list, *,
client=None, raw=None): client=None, raw=None):
self.id = id self.id = id

View File

@ -19,11 +19,11 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from pyrogram.client.ext.utils import encode 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. """This object represents one size of a photo or a file/sticker thumbnail.
Args: Args:
@ -40,8 +40,6 @@ class PhotoSize(Object):
File size. File size.
""" """
ID = 0xb0700005
def __init__(self, file_id: str, width: int, height: int, file_size: int, *, def __init__(self, file_id: str, width: int, height: int, file_size: int, *,
client=None, raw=None): client=None, raw=None):
self.file_id = file_id self.file_id = file_id

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode from ...ext.utils import encode
class Sticker(Object): class Sticker(PyrogramType):
"""This object represents a sticker. """This object represents a sticker.
Args: Args:
@ -60,7 +60,6 @@ class Sticker(Object):
""" """
# TODO: Add mask position # TODO: Add mask position
ID = 0xb0700017
def __init__(self, file_id: str, width: int, height: int, *, 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, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None,

View File

@ -16,10 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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. """This object represents a user's profile pictures.
Args: Args:
@ -30,8 +30,6 @@ class UserProfilePhotos(Object):
Requested profile pictures. Requested profile pictures.
""" """
ID = 0xb0700014
def __init__(self, total_count: int, photos: list): def __init__(self, total_count: int, photos: list):
self.total_count = total_count self.total_count = total_count
self.photos = photos self.photos = photos

View File

@ -17,11 +17,11 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .location import Location from .location import Location
from ..pyrogram_type import PyrogramType
class Venue(Object): class Venue(PyrogramType):
"""This object represents a venue. """This object represents a venue.
Args: Args:
@ -43,8 +43,6 @@ class Venue(Object):
""" """
ID = 0xb0700013
def __init__(self, location, title: str, address: str, *, def __init__(self, location, title: str, address: str, *,
foursquare_id: str = None, foursquare_type: str = None, foursquare_id: str = None, foursquare_type: str = None,
client=None, raw=None): client=None, raw=None):

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode from ...ext.utils import encode
class Video(Object): class Video(PyrogramType):
"""This object represents a video file. """This object represents a video file.
Args: Args:
@ -56,8 +56,6 @@ class Video(Object):
Date the video was sent in Unix time. Date the video was sent in Unix time.
""" """
ID = 0xb0700008
def __init__(self, file_id: str, width: int, height: int, duration: int, *, 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, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None,
client=None, raw=None): client=None, raw=None):

View File

@ -19,12 +19,12 @@
from struct import pack from struct import pack
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .photo_size import PhotoSize from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode 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). """This object represents a video message (available in Telegram apps as of v.4.0).
Args: Args:
@ -50,8 +50,6 @@ class VideoNote(Object):
Date the video note was sent in Unix time. Date the video note was sent in Unix time.
""" """
ID = 0xb0700010
def __init__(self, file_id: str, length: int, duration: int, *, def __init__(self, file_id: str, length: int, duration: int, *,
thumb=None, mime_type: str = None, file_size: int = None, date: int = None, thumb=None, mime_type: str = None, file_size: int = None, date: int = None,
client=None, raw=None): client=None, raw=None):

View File

@ -16,13 +16,14 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types
from pyrogram.api.core import Object
from ...ext.utils import encode
from struct import pack 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. """This object represents a voice note.
Args: Args:
@ -45,8 +46,6 @@ class Voice(Object):
Date the voice was sent in Unix time. Date the voice was sent in Unix time.
""" """
ID = 0xb0700009
def __init__(self, file_id: str, duration: int, *, def __init__(self, file_id: str, duration: int, *,
waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None, waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None,
client=None, raw=None): client=None, raw=None):

View File

@ -0,0 +1,44 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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()]
)

View File

@ -17,11 +17,11 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .chat_photo import ChatPhoto from .chat_photo import ChatPhoto
from ..pyrogram_type import PyrogramType
class Chat(Object): class Chat(PyrogramType):
"""This object represents a chat. """This object represents a chat.
Args: Args:
@ -76,8 +76,6 @@ class Chat(Object):
The reason why this chat might be unavailable to some users. The reason why this chat might be unavailable to some users.
""" """
ID = 0xb0700002
def __init__(self, id: int, type: str, *, def __init__(self, id: int, type: str, *,
title: str = None, username: str = None, first_name: str = None, last_name: str = None, 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, all_members_are_administrators: bool = None, photo=None, description: str = None,

View File

@ -42,7 +42,7 @@ class ChatPhoto(Object):
self.big_file_id = big_file_id self.big_file_id = big_file_id
self._client = client self._client = client
self.raw = raw self._raw = raw
@staticmethod @staticmethod
def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto):

View File

@ -17,12 +17,12 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types from pyrogram.api import types
from pyrogram.api.core import Object
from .user_status import UserStatus
from .chat_photo import ChatPhoto 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. """This object represents a Telegram user or bot.
Args: Args:
@ -69,8 +69,6 @@ class User(Object):
The reason why this bot might be unavailable to some users. 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, def __init__(self, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool,
is_bot: bool, first_name: str, *, is_bot: bool, first_name: str, *,
last_name: str = None, status=None, username: str = None, language_code: str = None, last_name: str = None, status=None, username: str = None, language_code: str = None,

View File

@ -17,10 +17,10 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram.api import types 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). """This object represents a User status (Last Seen privacy).
.. note:: .. note::
@ -62,8 +62,6 @@ class UserStatus(Object):
always shown to blocked users), None otherwise. always shown to blocked users), None otherwise.
""" """
ID = 0xb0700031
def __init__(self, user_id: int, *, def __init__(self, user_id: int, *,
online: bool = None, offline: bool = None, date: int = None, recently: bool = 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, within_week: bool = None, within_month: bool = None, long_time_ago: bool = None,