mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-14 10:53:23 +00:00
Rename data -> raw_data
This commit is contained in:
parent
44bf906110
commit
0d6af5bc8d
19 changed files with 227 additions and 240 deletions
|
|
@ -19,10 +19,18 @@ class Deserializable:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
return getattr(self, 'data', {})
|
result = {}
|
||||||
|
for item in self.__slots__ or list(self.__dict__.keys()):
|
||||||
|
attr = getattr(self, item)
|
||||||
|
if not attr:
|
||||||
|
continue
|
||||||
|
if hasattr(attr, 'to_json'):
|
||||||
|
attr = getattr(attr, 'to_json')()
|
||||||
|
result[item] = attr
|
||||||
|
return result
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
"""
|
"""
|
||||||
Returns an instance of this class from the given json dict or string.
|
Returns an instance of this class from the given json dict or string.
|
||||||
|
|
||||||
|
|
@ -32,18 +40,18 @@ class Deserializable:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def check_json(data) -> dict:
|
def check_json(raw_data) -> dict:
|
||||||
"""
|
"""
|
||||||
Checks whether json_type is a dict or a string. If it is already a dict, it is returned as-is.
|
Checks whether json_type is a dict or a string. If it is already a dict, it is returned as-is.
|
||||||
If it is not, it is converted to a dict by means of json.loads(json_type)
|
If it is not, it is converted to a dict by means of json.loads(json_type)
|
||||||
:param data:
|
:param raw_data:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(data, dict):
|
if isinstance(raw_data, dict):
|
||||||
return data
|
return raw_data
|
||||||
elif isinstance(data, str):
|
elif isinstance(raw_data, str):
|
||||||
return json.loads(data)
|
return json.loads(raw_data)
|
||||||
else:
|
else:
|
||||||
raise ValueError("data should be a json dict or string.")
|
raise ValueError("data should be a json dict or string.")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Audio(Deserializable):
|
class Audio(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'duration', 'performer', 'title', 'mime_type', 'file_size')
|
__slots__ = ('file_id', 'duration', 'performer', 'title', 'mime_type', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, duration, performer, title, mime_type, file_size):
|
def __init__(self, file_id, duration, performer, title, mime_type, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
self.performer = performer
|
self.performer = performer
|
||||||
|
|
@ -14,14 +13,14 @@ class Audio(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
duration = data.get('duration')
|
duration = raw_data.get('duration')
|
||||||
performer = data.get('performer')
|
performer = raw_data.get('performer')
|
||||||
title = data.get('title')
|
title = raw_data.get('title')
|
||||||
mime_type = data.get('mime_type')
|
mime_type = raw_data.get('mime_type')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return Audio(data, file_id, duration, performer, title, mime_type, file_size)
|
return Audio(file_id, duration, performer, title, mime_type, file_size)
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class CallbackQuery(Deserializable):
|
class CallbackQuery(Deserializable):
|
||||||
__slots__ = ('data', 'id', 'from', 'message', 'inline_message_id', 'chat_instance', 'data', 'game_short_name')
|
__slots__ = ('id', 'from', 'message', 'inline_message_id', 'chat_instance', 'data', 'game_short_name')
|
||||||
|
|
||||||
def __init__(self, data, id, from_user, message, inline_message_id, chat_instance, data, game_short_name):
|
def __init__(self, id, from_user, message, inline_message_id, chat_instance, data, game_short_name):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.id = id
|
self.id = id
|
||||||
self.from_user = from_user
|
self.from_user = from_user
|
||||||
|
|
@ -15,15 +15,15 @@ class CallbackQuery(Deserializable):
|
||||||
self.game_short_name = game_short_name
|
self.game_short_name = game_short_name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
id = data.get('id')
|
id = raw_data.get('id')
|
||||||
from_user = data.get('from')
|
from_user = raw_data.get('from')
|
||||||
message = data.get('message')
|
message = raw_data.get('message')
|
||||||
inline_message_id = data.get('inline_message_id')
|
inline_message_id = raw_data.get('inline_message_id')
|
||||||
chat_instance = data.get('chat_instance')
|
chat_instance = raw_data.get('chat_instance')
|
||||||
data = data.get('data')
|
data = raw_data.get('data')
|
||||||
game_short_name = data.get('game_short_name')
|
game_short_name = raw_data.get('game_short_name')
|
||||||
|
|
||||||
return CallbackQuery(data, id, from_user, message, inline_message_id, chat_instance, data, game_short_name)
|
return CallbackQuery(id, from_user, message, inline_message_id, chat_instance, data, game_short_name)
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,7 @@ from . import Deserializable
|
||||||
class Chat(Deserializable):
|
class Chat(Deserializable):
|
||||||
__slots__ = ('id', 'type', 'title', 'username', 'first_name', 'last_name', 'all_members_are_administrators')
|
__slots__ = ('id', 'type', 'title', 'username', 'first_name', 'last_name', 'all_members_are_administrators')
|
||||||
|
|
||||||
def __init__(self, data, id, type, title, username, first_name, last_name, all_members_are_administrators):
|
def __init__(self, id, type, title, username, first_name, last_name, all_members_are_administrators):
|
||||||
self.data = data
|
|
||||||
|
|
||||||
self.id: int = id
|
self.id: int = id
|
||||||
self.type: str = type
|
self.type: str = type
|
||||||
self.title: str = title
|
self.title: str = title
|
||||||
|
|
@ -16,7 +14,7 @@ class Chat(Deserializable):
|
||||||
self.all_members_are_administrators: bool = all_members_are_administrators
|
self.all_members_are_administrators: bool = all_members_are_administrators
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data) -> 'Chat':
|
def de_json(cls, raw_data) -> 'Chat':
|
||||||
"""
|
"""
|
||||||
id Integer Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
|
id Integer Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
|
||||||
type String Type of chat, can be either “private”, “group”, “supergroup” or “channel”
|
type String Type of chat, can be either “private”, “group”, “supergroup” or “channel”
|
||||||
|
|
@ -25,20 +23,20 @@ class Chat(Deserializable):
|
||||||
first_name String Optional. First name of the other party in a private chat
|
first_name String Optional. First name of the other party in a private chat
|
||||||
last_name String Optional. Last name of the other party in a private chat
|
last_name String Optional. Last name of the other party in a private chat
|
||||||
all_members_are_administrators Boolean Optional. True if a group has ‘All Members Are Admins’ enabled.
|
all_members_are_administrators Boolean Optional. True if a group has ‘All Members Are Admins’ enabled.
|
||||||
:param data:
|
:param raw_data:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
id: int = data.get('id')
|
id: int = raw_data.get('id')
|
||||||
type: str = data.get('type')
|
type: str = raw_data.get('type')
|
||||||
title: str = data.get('title')
|
title: str = raw_data.get('title')
|
||||||
username: str = data.get('username')
|
username: str = raw_data.get('username')
|
||||||
first_name: str = data.get('first_name')
|
first_name: str = raw_data.get('first_name')
|
||||||
last_name: str = data.get('last_name')
|
last_name: str = raw_data.get('last_name')
|
||||||
all_members_are_administrators: bool = data.get('all_members_are_administrators', False)
|
all_members_are_administrators: bool = raw_data.get('all_members_are_administrators', False)
|
||||||
|
|
||||||
return Chat(data, id, type, title, username, first_name, last_name, all_members_are_administrators)
|
return Chat(id, type, title, username, first_name, last_name, all_members_are_administrators)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,21 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Contact(Deserializable):
|
class Contact(Deserializable):
|
||||||
__slots__ = ('data', 'phone_number', 'first_name', 'last_name', 'user_id')
|
__slots__ = ('phone_number', 'first_name', 'last_name', 'user_id')
|
||||||
|
|
||||||
def __init__(self, data, phone_number, first_name, last_name, user_id):
|
def __init__(self, phone_number, first_name, last_name, user_id):
|
||||||
self.data = data
|
|
||||||
self.phone_number = phone_number
|
self.phone_number = phone_number
|
||||||
self.first_name = first_name
|
self.first_name = first_name
|
||||||
self.last_name = last_name
|
self.last_name = last_name
|
||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
phone_number = data.get('phone_number')
|
phone_number = raw_data.get('phone_number')
|
||||||
first_name = data.get('first_name')
|
first_name = raw_data.get('first_name')
|
||||||
last_name = data.get('last_name')
|
last_name = raw_data.get('last_name')
|
||||||
user_id = data.get('user_id')
|
user_id = raw_data.get('user_id')
|
||||||
|
|
||||||
return Contact(data, phone_number, first_name, last_name, user_id)
|
return Contact(phone_number, first_name, last_name, user_id)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Document(Deserializable):
|
class Document(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'thumb', 'file_name', 'mime_type', 'file_size')
|
__slots__ = ('file_id', 'thumb', 'file_name', 'mime_type', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, thumb, file_name, mime_type, file_size):
|
def __init__(self, file_id, thumb, file_name, mime_type, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.thumb = thumb
|
self.thumb = thumb
|
||||||
self.file_name = file_name
|
self.file_name = file_name
|
||||||
|
|
@ -13,13 +12,13 @@ class Document(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
thumb = data.get('thumb')
|
thumb = raw_data.get('thumb')
|
||||||
file_name = data.get('file_name')
|
file_name = raw_data.get('file_name')
|
||||||
mime_type = data.get('mime_type')
|
mime_type = raw_data.get('mime_type')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return Document(data, file_id, thumb, file_name, mime_type, file_size)
|
return Document(file_id, thumb, file_name, mime_type, file_size)
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,19 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class File(Deserializable):
|
class File(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'file_size', 'file_path')
|
__slots__ = ('file_id', 'file_size', 'file_path')
|
||||||
|
|
||||||
def __init__(self, data, file_id, file_size, file_path):
|
def __init__(self, file_id, file_size, file_path):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
file_path = data.get('file_path')
|
file_path = raw_data.get('file_path')
|
||||||
|
|
||||||
return File(data, file_id, file_size, file_path)
|
return File(file_id, file_size, file_path)
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,17 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Location(Deserializable):
|
class Location(Deserializable):
|
||||||
__slots__ = ('data', 'longitude', 'latitude')
|
__slots__ = ('longitude', 'latitude')
|
||||||
|
|
||||||
def __init__(self, data, longitude, latitude):
|
def __init__(self, data, longitude, latitude):
|
||||||
self.data = data
|
|
||||||
self.longitude = longitude
|
self.longitude = longitude
|
||||||
self.latitude = latitude
|
self.latitude = latitude
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
longitude = data.get('longitude')
|
longitude = raw_data.get('longitude')
|
||||||
latitude = data.get('latitude')
|
latitude = raw_data.get('latitude')
|
||||||
|
|
||||||
return Location(data, longitude, latitude)
|
return Location(longitude, latitude)
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,19 @@ from .user import User
|
||||||
|
|
||||||
class Message(Deserializable):
|
class Message(Deserializable):
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
'data', 'message_id', 'from', 'date', 'chat', 'forward_from', 'forward_from_chat', 'forward_from_message_id',
|
'message_id', 'from', 'date', 'chat', 'forward_from', 'forward_from_chat', 'forward_from_message_id',
|
||||||
'forward_date', 'reply_to_message', 'edit_date', 'text', 'entities', 'audio', 'document', 'game', 'photo',
|
'forward_date', 'reply_to_message', 'edit_date', 'text', 'entities', 'audio', 'document', 'game', 'photo',
|
||||||
'sticker', 'video', 'voice', 'video_note', 'new_chat_members', 'caption', 'contact', 'location', 'venue',
|
'sticker', 'video', 'voice', 'video_note', 'new_chat_members', 'caption', 'contact', 'location', 'venue',
|
||||||
'new_chat_member', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo',
|
'new_chat_member', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo',
|
||||||
'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id',
|
'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id',
|
||||||
'migrate_from_chat_id', 'pinned_message', 'invoice', 'successful_payment', 'content_type')
|
'migrate_from_chat_id', 'pinned_message', 'invoice', 'successful_payment', 'content_type')
|
||||||
|
|
||||||
def __init__(self, data, message_id, from_user, date, chat, forward_from, forward_from_chat,
|
def __init__(self, message_id, from_user, date, chat, forward_from, forward_from_chat,
|
||||||
forward_from_message_id, forward_date, reply_to_message, edit_date, text, entities, audio, document,
|
forward_from_message_id, forward_date, reply_to_message, edit_date, text, entities, audio, document,
|
||||||
game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact, location, venue,
|
game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact, location, venue,
|
||||||
new_chat_member, left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo,
|
new_chat_member, left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo,
|
||||||
group_chat_created, supergroup_chat_created, channel_chat_created, migrate_to_chat_id,
|
group_chat_created, supergroup_chat_created, channel_chat_created, migrate_to_chat_id,
|
||||||
migrate_from_chat_id, pinned_message, invoice, successful_payment, content_type):
|
migrate_from_chat_id, pinned_message, invoice, successful_payment, content_type):
|
||||||
self.data = data
|
|
||||||
|
|
||||||
self.message_id: int = message_id
|
self.message_id: int = message_id
|
||||||
self.from_user: User = from_user
|
self.from_user: User = from_user
|
||||||
self.date: datetime.datetime = date
|
self.date: datetime.datetime = date
|
||||||
|
|
@ -86,48 +84,48 @@ class Message(Deserializable):
|
||||||
return [MessageEntity.de_json(entity) for entity in entities] if entities else None
|
return [MessageEntity.de_json(entity) for entity in entities] if entities else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
message_id = data.get('message_id')
|
message_id = raw_data.get('message_id')
|
||||||
from_user = cls._parse_user(data.get('from'))
|
from_user = cls._parse_user(raw_data.get('from'))
|
||||||
date = cls._parse_date(data.get('date', 0))
|
date = cls._parse_date(raw_data.get('date', 0))
|
||||||
chat = cls._parse_chat(data.get('chat', {}))
|
chat = cls._parse_chat(raw_data.get('chat', {}))
|
||||||
forward_from = cls._parse_user(data.get('forward_from', {}))
|
forward_from = cls._parse_user(raw_data.get('forward_from', {}))
|
||||||
forward_from_chat = cls._parse_chat(data.get('forward_from_chat', {}))
|
forward_from_chat = cls._parse_chat(raw_data.get('forward_from_chat', {}))
|
||||||
forward_from_message_id = data.get('forward_from_message_id')
|
forward_from_message_id = raw_data.get('forward_from_message_id')
|
||||||
forward_date = cls._parse_date(data.get('forward_date', 0))
|
forward_date = cls._parse_date(raw_data.get('forward_date', 0))
|
||||||
reply_to_message = cls._parse_message(data.get('reply_to_message', {}))
|
reply_to_message = cls._parse_message(raw_data.get('reply_to_message', {}))
|
||||||
edit_date = cls._parse_date(data.get('edit_date', 0))
|
edit_date = cls._parse_date(raw_data.get('edit_date', 0))
|
||||||
text = data.get('text')
|
text = raw_data.get('text')
|
||||||
entities = cls._parse_entities(data.get('entities'))
|
entities = cls._parse_entities(raw_data.get('entities'))
|
||||||
|
|
||||||
audio = data.get('audio')
|
audio = raw_data.get('audio')
|
||||||
document = data.get('document')
|
document = raw_data.get('document')
|
||||||
game = data.get('game')
|
game = raw_data.get('game')
|
||||||
photo = data.get('photo')
|
photo = raw_data.get('photo')
|
||||||
sticker = data.get('sticker')
|
sticker = raw_data.get('sticker')
|
||||||
video = data.get('video')
|
video = raw_data.get('video')
|
||||||
voice = data.get('voice')
|
voice = raw_data.get('voice')
|
||||||
video_note = data.get('video_note')
|
video_note = raw_data.get('video_note')
|
||||||
new_chat_members = data.get('new_chat_members')
|
new_chat_members = raw_data.get('new_chat_members')
|
||||||
caption = data.get('caption')
|
caption = raw_data.get('caption')
|
||||||
contact = data.get('contact')
|
contact = raw_data.get('contact')
|
||||||
location = data.get('location')
|
location = raw_data.get('location')
|
||||||
venue = data.get('venue')
|
venue = raw_data.get('venue')
|
||||||
new_chat_member = data.get('new_chat_member')
|
new_chat_member = raw_data.get('new_chat_member')
|
||||||
left_chat_member = data.get('left_chat_member')
|
left_chat_member = raw_data.get('left_chat_member')
|
||||||
new_chat_title = data.get('new_chat_title')
|
new_chat_title = raw_data.get('new_chat_title')
|
||||||
new_chat_photo = data.get('new_chat_photo')
|
new_chat_photo = raw_data.get('new_chat_photo')
|
||||||
delete_chat_photo = data.get('delete_chat_photo')
|
delete_chat_photo = raw_data.get('delete_chat_photo')
|
||||||
group_chat_created = data.get('group_chat_created')
|
group_chat_created = raw_data.get('group_chat_created')
|
||||||
supergroup_chat_created = data.get('supergroup_chat_created')
|
supergroup_chat_created = raw_data.get('supergroup_chat_created')
|
||||||
channel_chat_created = data.get('channel_chat_created')
|
channel_chat_created = raw_data.get('channel_chat_created')
|
||||||
migrate_to_chat_id = data.get('migrate_to_chat_id')
|
migrate_to_chat_id = raw_data.get('migrate_to_chat_id')
|
||||||
migrate_from_chat_id = data.get('migrate_from_chat_id')
|
migrate_from_chat_id = raw_data.get('migrate_from_chat_id')
|
||||||
pinned_message = data.get('pinned_message')
|
pinned_message = raw_data.get('pinned_message')
|
||||||
invoice = data.get('invoice')
|
invoice = raw_data.get('invoice')
|
||||||
successful_payment = data.get('successful_payment')
|
successful_payment = raw_data.get('successful_payment')
|
||||||
|
|
||||||
if text:
|
if text:
|
||||||
content_type = ContentType.TEXT
|
content_type = ContentType.TEXT
|
||||||
|
|
@ -156,7 +154,7 @@ class Message(Deserializable):
|
||||||
else:
|
else:
|
||||||
content_type = ContentType.UNKNOWN
|
content_type = ContentType.UNKNOWN
|
||||||
|
|
||||||
return Message(data, message_id, from_user, date, chat, forward_from, forward_from_chat,
|
return Message(message_id, from_user, date, chat, forward_from, forward_from_chat,
|
||||||
forward_from_message_id, forward_date, reply_to_message, edit_date, text, entities, audio,
|
forward_from_message_id, forward_date, reply_to_message, edit_date, text, entities, audio,
|
||||||
document, game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact,
|
document, game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact,
|
||||||
location, venue, new_chat_member, left_chat_member, new_chat_title, new_chat_photo,
|
location, venue, new_chat_member, left_chat_member, new_chat_title, new_chat_photo,
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,9 @@ from .user import User
|
||||||
|
|
||||||
|
|
||||||
class MessageEntity(Deserializable):
|
class MessageEntity(Deserializable):
|
||||||
__slots__ = ('data', 'type', 'offset', 'length', 'url', 'user')
|
__slots__ = ('type', 'offset', 'length', 'url', 'user')
|
||||||
|
|
||||||
def __init__(self, data, type, offset, length, url, user):
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
|
def __init__(self, type, offset, length, url, user):
|
||||||
self.type = type
|
self.type = type
|
||||||
self.offset = offset
|
self.offset = offset
|
||||||
self.length = length
|
self.length = length
|
||||||
|
|
@ -19,16 +17,16 @@ class MessageEntity(Deserializable):
|
||||||
return User.de_json(user) if user else None
|
return User.de_json(user) if user else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
type = data.get('type')
|
type = raw_data.get('type')
|
||||||
offset = data.get('offset')
|
offset = raw_data.get('offset')
|
||||||
length = data.get('length')
|
length = raw_data.get('length')
|
||||||
url = data.get('url')
|
url = raw_data.get('url')
|
||||||
user = cls._parse_user(data.get('user'))
|
user = cls._parse_user(raw_data.get('user'))
|
||||||
|
|
||||||
return MessageEntity(data, type, offset, length, url, user)
|
return MessageEntity(type, offset, length, url, user)
|
||||||
|
|
||||||
|
|
||||||
class MessageEntityType:
|
class MessageEntityType:
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,12 @@ class PhotoSize(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
width = data.get('width')
|
width = raw_data.get('width')
|
||||||
height = data.get('height')
|
height = raw_data.get('height')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return PhotoSize(file_id, width, height, file_size)
|
return PhotoSize(file_id, width, height, file_size)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Sticker(Deserializable):
|
class Sticker(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'width', 'height', 'thumb', 'emoji', 'file_size')
|
__slots__ = ('file_id', 'width', 'height', 'thumb', 'emoji', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, width, height, thumb, emoji, file_size):
|
def __init__(self, file_id, width, height, thumb, emoji, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
|
|
@ -14,14 +13,14 @@ class Sticker(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
width = data.get('width')
|
width = raw_data.get('width')
|
||||||
height = data.get('height')
|
height = raw_data.get('height')
|
||||||
thumb = data.get('thumb')
|
thumb = raw_data.get('thumb')
|
||||||
emoji = data.get('emoji')
|
emoji = raw_data.get('emoji')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return Sticker(data, file_id, width, height, thumb, emoji, file_size)
|
return Sticker(file_id, width, height, thumb, emoji, file_size)
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,8 @@ class Update(Deserializable):
|
||||||
__slots__ = ('update_id', 'message', 'edited_message', 'channel_post', 'edited_channel_post', 'inline_query',
|
__slots__ = ('update_id', 'message', 'edited_message', 'channel_post', 'edited_channel_post', 'inline_query',
|
||||||
'chosen_inline_result', 'callback_query', 'shipping_query', 'pre_checkout_query')
|
'chosen_inline_result', 'callback_query', 'shipping_query', 'pre_checkout_query')
|
||||||
|
|
||||||
def __init__(self, data, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query):
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query):
|
||||||
self.data = data
|
|
||||||
|
|
||||||
self.update_id = update_id
|
self.update_id = update_id
|
||||||
self.message: Message = message
|
self.message: Message = message
|
||||||
self.edited_message: Message = edited_message
|
self.edited_message: Message = edited_message
|
||||||
|
|
@ -26,7 +24,7 @@ class Update(Deserializable):
|
||||||
return Message.de_json(message) if message else None
|
return Message.de_json(message) if message else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
"""
|
"""
|
||||||
update_id Integer The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
|
update_id Integer The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
|
||||||
message Message Optional. New incoming message of any kind — text, photo, sticker, etc.
|
message Message Optional. New incoming message of any kind — text, photo, sticker, etc.
|
||||||
|
|
@ -38,22 +36,22 @@ class Update(Deserializable):
|
||||||
callback_query CallbackQuery Optional. New incoming callback query
|
callback_query CallbackQuery Optional. New incoming callback query
|
||||||
shipping_query ShippingQuery Optional. New incoming shipping query. Only for invoices with flexible price
|
shipping_query ShippingQuery Optional. New incoming shipping query. Only for invoices with flexible price
|
||||||
pre_checkout_query PreCheckoutQuery Optional. New incoming pre-checkout query. Contains full information about checkout
|
pre_checkout_query PreCheckoutQuery Optional. New incoming pre-checkout query. Contains full information about checkout
|
||||||
:param data:
|
:param raw_data:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
update_id = data.get('update_id')
|
update_id = raw_data.get('update_id')
|
||||||
message = cls._parse_message(data.get('message'))
|
message = cls._parse_message(raw_data.get('message'))
|
||||||
edited_message = cls._parse_message(data.get('edited_message'))
|
edited_message = cls._parse_message(raw_data.get('edited_message'))
|
||||||
channel_post = cls._parse_message(data.get('channel_post'))
|
channel_post = cls._parse_message(raw_data.get('channel_post'))
|
||||||
edited_channel_post = cls._parse_message(data.get('edited_channel_post'))
|
edited_channel_post = cls._parse_message(raw_data.get('edited_channel_post'))
|
||||||
|
|
||||||
inline_query = data.get('inline_query')
|
inline_query = raw_data.get('inline_query')
|
||||||
chosen_inline_result = data.get('chosen_inline_result')
|
chosen_inline_result = raw_data.get('chosen_inline_result')
|
||||||
callback_query = data.get('callback_query')
|
callback_query = raw_data.get('callback_query')
|
||||||
shipping_query = data.get('shipping_query')
|
shipping_query = raw_data.get('shipping_query')
|
||||||
pre_checkout_query = data.get('pre_checkout_query')
|
pre_checkout_query = raw_data.get('pre_checkout_query')
|
||||||
|
|
||||||
return Update(data, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
return Update(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query)
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query)
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,9 @@ from ..utils.user_language import get_language
|
||||||
|
|
||||||
|
|
||||||
class User(Deserializable):
|
class User(Deserializable):
|
||||||
__slots__ = ('data', 'id', 'first_name', 'last_name', 'username', 'language_code')
|
__slots__ = ('id', 'first_name', 'last_name', 'username', 'language_code')
|
||||||
|
|
||||||
def __init__(self, data, id, first_name, last_name, username, language_code):
|
|
||||||
self.data: dict = data
|
|
||||||
|
|
||||||
|
def __init__(self, id, first_name, last_name, username, language_code):
|
||||||
self.id: int = id
|
self.id: int = id
|
||||||
self.first_name: str = first_name
|
self.first_name: str = first_name
|
||||||
self.last_name: str = last_name
|
self.last_name: str = last_name
|
||||||
|
|
@ -15,25 +13,25 @@ class User(Deserializable):
|
||||||
self.language_code: str = language_code
|
self.language_code: str = language_code
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data: str or dict) -> 'User':
|
def de_json(cls, raw_data: str or dict) -> 'User':
|
||||||
"""
|
"""
|
||||||
id Integer Unique identifier for this user or bot
|
id Integer Unique identifier for this user or bot
|
||||||
first_name String User‘s or bot’s first name
|
first_name String User‘s or bot’s first name
|
||||||
last_name String Optional. User‘s or bot’s last name
|
last_name String Optional. User‘s or bot’s last name
|
||||||
username String Optional. User‘s or bot’s username
|
username String Optional. User‘s or bot’s username
|
||||||
language_code String Optional. IETF language tag of the user's language
|
language_code String Optional. IETF language tag of the user's language
|
||||||
:param data:
|
:param raw_data:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
id = data.get('id')
|
id = raw_data.get('id')
|
||||||
first_name = data.get('first_name')
|
first_name = raw_data.get('first_name')
|
||||||
last_name = data.get('last_name')
|
last_name = raw_data.get('last_name')
|
||||||
username = data.get('username')
|
username = raw_data.get('username')
|
||||||
language_code = data.get('language_code')
|
language_code = raw_data.get('language_code')
|
||||||
|
|
||||||
return User(data, id, first_name, last_name, username, language_code)
|
return User(id, first_name, last_name, username, language_code)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,17 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class UserProfilePhotos(Deserializable):
|
class UserProfilePhotos(Deserializable):
|
||||||
__slots__ = ('data', 'total_count', 'photos')
|
__slots__ = ('total_count', 'photos')
|
||||||
|
|
||||||
def __init__(self, data, total_count, photos):
|
def __init__(self, total_count, photos):
|
||||||
self.data = data
|
|
||||||
self.total_count = total_count
|
self.total_count = total_count
|
||||||
self.photos = photos
|
self.photos = photos
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
total_count = data.get('total_count')
|
total_count = raw_data.get('total_count')
|
||||||
photos = data.get('photos')
|
photos = raw_data.get('photos')
|
||||||
|
|
||||||
return UserProfilePhotos(data, total_count, photos)
|
return UserProfilePhotos(total_count, photos)
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,21 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Venue(Deserializable):
|
class Venue(Deserializable):
|
||||||
__slots__ = ('data', 'location', 'title', 'address', 'foursquare_id')
|
__slots__ = ('location', 'title', 'address', 'foursquare_id')
|
||||||
|
|
||||||
def __init__(self, data, location, title, address, foursquare_id):
|
def __init__(self, location, title, address, foursquare_id):
|
||||||
self.data = data
|
|
||||||
self.location = location
|
self.location = location
|
||||||
self.title = title
|
self.title = title
|
||||||
self.address = address
|
self.address = address
|
||||||
self.foursquare_id = foursquare_id
|
self.foursquare_id = foursquare_id
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
location = data.get('location')
|
location = raw_data.get('location')
|
||||||
title = data.get('title')
|
title = raw_data.get('title')
|
||||||
address = data.get('address')
|
address = raw_data.get('address')
|
||||||
foursquare_id = data.get('foursquare_id')
|
foursquare_id = raw_data.get('foursquare_id')
|
||||||
|
|
||||||
return Venue(data, location, title, address, foursquare_id)
|
return Venue(location, title, address, foursquare_id)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Video(Deserializable):
|
class Video(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'width', 'height', 'duration', 'thumb', 'mime_type', 'file_size')
|
__slots__ = ('file_id', 'width', 'height', 'duration', 'thumb', 'mime_type', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, width, height, duration, thumb, mime_type, file_size):
|
def __init__(self, file_id, width, height, duration, thumb, mime_type, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
|
|
@ -15,15 +14,15 @@ class Video(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
width = data.get('width')
|
width = raw_data.get('width')
|
||||||
height = data.get('height')
|
height = raw_data.get('height')
|
||||||
duration = data.get('duration')
|
duration = raw_data.get('duration')
|
||||||
thumb = data.get('thumb')
|
thumb = raw_data.get('thumb')
|
||||||
mime_type = data.get('mime_type')
|
mime_type = raw_data.get('mime_type')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return Video(data, file_id, width, height, duration, thumb, mime_type, file_size)
|
return Video(file_id, width, height, duration, thumb, mime_type, file_size)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,9 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class VideoNote(Deserializable):
|
class VideoNote(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'length', 'duration', 'thumb', 'file_size')
|
__slots__ = ('file_id', 'length', 'duration', 'thumb', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, length, duration, thumb, file_size):
|
def __init__(self, file_id, length, duration, thumb, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.length = length
|
self.length = length
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
|
|
@ -13,13 +12,13 @@ class VideoNote(Deserializable):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
length = data.get('length')
|
length = raw_data.get('length')
|
||||||
duration = data.get('duration')
|
duration = raw_data.get('duration')
|
||||||
thumb = data.get('thumb')
|
thumb = raw_data.get('thumb')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return VideoNote(data, file_id, length, duration, thumb, file_size)
|
return VideoNote(file_id, length, duration, thumb, file_size)
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,21 @@ from . import Deserializable
|
||||||
|
|
||||||
|
|
||||||
class Voice(Deserializable):
|
class Voice(Deserializable):
|
||||||
__slots__ = ('data', 'file_id', 'duration', 'mime_type', 'file_size')
|
__slots__ = ('file_id', 'duration', 'mime_type', 'file_size')
|
||||||
|
|
||||||
def __init__(self, data, file_id, duration, mime_type, file_size):
|
def __init__(self, file_id, duration, mime_type, file_size):
|
||||||
self.data = data
|
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
self.mime_type = mime_type
|
self.mime_type = mime_type
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, data):
|
def de_json(cls, raw_data):
|
||||||
data = cls.check_json(data)
|
raw_data = cls.check_json(raw_data)
|
||||||
|
|
||||||
file_id = data.get('file_id')
|
file_id = raw_data.get('file_id')
|
||||||
duration = data.get('duration')
|
duration = raw_data.get('duration')
|
||||||
mime_type = data.get('mime_type')
|
mime_type = raw_data.get('mime_type')
|
||||||
file_size = data.get('file_size')
|
file_size = raw_data.get('file_size')
|
||||||
|
|
||||||
return Voice(data, file_id, duration, mime_type, file_size)
|
return Voice(file_id, duration, mime_type, file_size)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue