mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
* dict.get(key) returns None if key is not in the dictionary
* used the class method's class reference to instantiate type classes
This commit is contained in:
parent
bf9939d40e
commit
5175803d0b
132
telebot/types.py
132
telebot/types.py
@ -88,7 +88,7 @@ class Update(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_type)
|
obj = cls.check_json(json_type)
|
||||||
update_id = obj['update_id']
|
update_id = obj['update_id']
|
||||||
message = Message.de_json(obj['message'])
|
message = Message.de_json(obj['message'])
|
||||||
return Update(update_id, message)
|
return cls(update_id, message)
|
||||||
|
|
||||||
def __init__(self, update_id, message):
|
def __init__(self, update_id, message):
|
||||||
self.update_id = update_id
|
self.update_id = update_id
|
||||||
@ -101,13 +101,9 @@ class User(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
id = obj['id']
|
id = obj['id']
|
||||||
first_name = obj['first_name']
|
first_name = obj['first_name']
|
||||||
last_name = None
|
last_name = obj.get('last_name')
|
||||||
username = None
|
username = obj.get('username')
|
||||||
if 'last_name' in obj:
|
return cls(id, first_name, last_name, username)
|
||||||
last_name = obj['last_name']
|
|
||||||
if 'username' in obj:
|
|
||||||
username = obj['username']
|
|
||||||
return User(id, first_name, last_name, username)
|
|
||||||
|
|
||||||
def __init__(self, id, first_name, last_name=None, username=None):
|
def __init__(self, id, first_name, last_name=None, username=None):
|
||||||
self.id = id
|
self.id = id
|
||||||
@ -122,7 +118,7 @@ class GroupChat(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
id = obj['id']
|
id = obj['id']
|
||||||
title = obj['title']
|
title = obj['title']
|
||||||
return GroupChat(id, title)
|
return cls(id, title)
|
||||||
|
|
||||||
def __init__(self, id, title):
|
def __init__(self, id, title):
|
||||||
self.id = id
|
self.id = id
|
||||||
@ -135,19 +131,11 @@ class Chat(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
id = obj['id']
|
id = obj['id']
|
||||||
type = obj['type']
|
type = obj['type']
|
||||||
title = None
|
title = obj.get('title')
|
||||||
username = None
|
username = obj.get('username')
|
||||||
first_name = None
|
first_name = obj.get('first_name')
|
||||||
last_name = None
|
last_name = obj.get('last_name')
|
||||||
if 'title' in obj:
|
return cls(id, type, title, username, first_name, last_name)
|
||||||
title = obj['title']
|
|
||||||
if 'username' in obj:
|
|
||||||
username = obj['username']
|
|
||||||
if 'first_name'in obj:
|
|
||||||
first_name = obj['first_name']
|
|
||||||
if 'last_name' in obj:
|
|
||||||
last_name = obj['last_name']
|
|
||||||
return Chat(id, type, title, username, first_name, last_name)
|
|
||||||
|
|
||||||
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None):
|
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None):
|
||||||
self.type = type
|
self.type = type
|
||||||
@ -223,7 +211,7 @@ class Message(JsonDeserializable):
|
|||||||
content_type = 'group_chat_created'
|
content_type = 'group_chat_created'
|
||||||
if 'caption' in obj:
|
if 'caption' in obj:
|
||||||
opts['caption'] = obj['caption']
|
opts['caption'] = obj['caption']
|
||||||
return Message(message_id, from_user, date, chat, content_type, opts)
|
return cls(message_id, from_user, date, chat, content_type, opts)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_chat(cls, chat):
|
def parse_chat(cls, chat):
|
||||||
@ -256,10 +244,8 @@ class PhotoSize(JsonDeserializable):
|
|||||||
file_id = obj['file_id']
|
file_id = obj['file_id']
|
||||||
width = obj['width']
|
width = obj['width']
|
||||||
height = obj['height']
|
height = obj['height']
|
||||||
file_size = None
|
file_size = obj.get('file_size')
|
||||||
if 'file_size' in obj:
|
return cls(file_id, width, height, file_size)
|
||||||
file_size = obj['file_size']
|
|
||||||
return PhotoSize(file_id, width, height, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, width, height, file_size=None):
|
def __init__(self, file_id, width, height, file_size=None):
|
||||||
self.file_size = file_size
|
self.file_size = file_size
|
||||||
@ -274,19 +260,11 @@ class Audio(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
file_id = obj['file_id']
|
file_id = obj['file_id']
|
||||||
duration = obj['duration']
|
duration = obj['duration']
|
||||||
performer = None
|
performer = obj.get('performer')
|
||||||
title = None
|
title = obj.get('title')
|
||||||
mime_type = None
|
mime_type = obj.get('mime_type')
|
||||||
file_size = None
|
file_size = obj.get('file_size')
|
||||||
if 'mime_type' in obj:
|
return cls(file_id, duration, performer, title, mime_type, file_size)
|
||||||
mime_type = obj['mime_type']
|
|
||||||
if 'file_size' in obj:
|
|
||||||
file_size = obj['file_size']
|
|
||||||
if 'performer' in obj:
|
|
||||||
performer = obj['performer']
|
|
||||||
if 'title' in obj:
|
|
||||||
title = obj['title']
|
|
||||||
return Audio(file_id, duration, performer, title, mime_type, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, duration, performer=None, title=None, mime_type=None, file_size=None):
|
def __init__(self, file_id, duration, performer=None, title=None, mime_type=None, file_size=None):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
@ -303,13 +281,9 @@ class Voice(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
file_id = obj['file_id']
|
file_id = obj['file_id']
|
||||||
duration = obj['duration']
|
duration = obj['duration']
|
||||||
mime_type = None
|
mime_type = obj.get('mime_type')
|
||||||
file_size = None
|
file_size = obj.get('file_size')
|
||||||
if 'mime_type' in obj:
|
return cls(file_id, duration, mime_type, file_size)
|
||||||
mime_type = obj['mime_type']
|
|
||||||
if 'file_size' in obj:
|
|
||||||
file_size = obj['file_size']
|
|
||||||
return Voice(file_id, duration, mime_type, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, duration, mime_type=None, file_size=None):
|
def __init__(self, file_id, duration, mime_type=None, file_size=None):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
@ -324,19 +298,12 @@ class Document(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
file_id = obj['file_id']
|
file_id = obj['file_id']
|
||||||
thumb = None
|
thumb = None
|
||||||
if 'thumb' in obj:
|
if 'thumb' in obj and 'file_id' in obj['thumb']:
|
||||||
if 'file_id' in obj['thumb']:
|
thumb = PhotoSize.de_json(obj['thumb'])
|
||||||
thumb = PhotoSize.de_json(obj['thumb'])
|
file_name = obj.get('file_name')
|
||||||
file_name = None
|
mime_type = obj.get('mime_type')
|
||||||
mime_type = None
|
file_size = obj.get('file_size')
|
||||||
file_size = None
|
return cls(file_id, thumb, file_name, mime_type, file_size)
|
||||||
if 'file_name' in obj:
|
|
||||||
file_name = obj['file_name']
|
|
||||||
if 'mime_type' in obj:
|
|
||||||
mime_type = obj['mime_type']
|
|
||||||
if 'file_size' in obj:
|
|
||||||
file_size = obj['file_size']
|
|
||||||
return Document(file_id, thumb, file_name, mime_type, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, thumb, file_name=None, mime_type=None, file_size=None):
|
def __init__(self, file_id, thumb, file_name=None, mime_type=None, file_size=None):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
@ -356,10 +323,8 @@ class Sticker(JsonDeserializable):
|
|||||||
thumb = None
|
thumb = None
|
||||||
if 'thumb' in obj:
|
if 'thumb' in obj:
|
||||||
thumb = PhotoSize.de_json(obj['thumb'])
|
thumb = PhotoSize.de_json(obj['thumb'])
|
||||||
file_size = None
|
file_size = obj.get('file_size')
|
||||||
if 'file_size' in obj:
|
return cls(file_id, width, height, thumb, file_size)
|
||||||
file_size = obj['file_size']
|
|
||||||
return Sticker(file_id, width, height, thumb, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, width, height, thumb, file_size=None):
|
def __init__(self, file_id, width, height, thumb, file_size=None):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
@ -377,16 +342,10 @@ class Video(JsonDeserializable):
|
|||||||
width = obj['width']
|
width = obj['width']
|
||||||
height = obj['height']
|
height = obj['height']
|
||||||
duration = obj['duration']
|
duration = obj['duration']
|
||||||
thumb = None
|
thumb = obj.get('thumb')
|
||||||
mime_type = None
|
mime_type = obj.get('mime_type')
|
||||||
file_size = None
|
file_size = obj.get('file_size')
|
||||||
if 'thumb' in obj:
|
return cls(file_id, width, height, duration, thumb, mime_type, file_size)
|
||||||
thumb = PhotoSize.de_json(obj['thumb'])
|
|
||||||
if 'mime_type' in obj:
|
|
||||||
mime_type = obj['mime_type']
|
|
||||||
if 'file_size' in obj:
|
|
||||||
file_size = obj['file_size']
|
|
||||||
return Video(file_id, width, height, duration, thumb, mime_type, file_size)
|
|
||||||
|
|
||||||
def __init__(self, file_id, width, height, duration, thumb=None, mime_type=None, file_size=None):
|
def __init__(self, file_id, width, height, duration, thumb=None, mime_type=None, file_size=None):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
@ -404,13 +363,9 @@ class Contact(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
phone_number = obj['phone_number']
|
phone_number = obj['phone_number']
|
||||||
first_name = obj['first_name']
|
first_name = obj['first_name']
|
||||||
last_name = None
|
last_name = obj.get('last_name')
|
||||||
user_id = None
|
user_id = obj.get('user_id')
|
||||||
if 'last_name' in obj:
|
return cls(phone_number, first_name, last_name, user_id)
|
||||||
last_name = obj['last_name']
|
|
||||||
if 'user_id' in obj:
|
|
||||||
user_id = obj['user_id']
|
|
||||||
return Contact(phone_number, first_name, last_name, user_id)
|
|
||||||
|
|
||||||
def __init__(self, phone_number, first_name, last_name=None, user_id=None):
|
def __init__(self, phone_number, first_name, last_name=None, user_id=None):
|
||||||
self.phone_number = phone_number
|
self.phone_number = phone_number
|
||||||
@ -425,7 +380,7 @@ class Location(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
longitude = obj['longitude']
|
longitude = obj['longitude']
|
||||||
latitude = obj['latitude']
|
latitude = obj['latitude']
|
||||||
return Location(longitude, latitude)
|
return cls(longitude, latitude)
|
||||||
|
|
||||||
def __init__(self, longitude, latitude):
|
def __init__(self, longitude, latitude):
|
||||||
self.longitude = longitude
|
self.longitude = longitude
|
||||||
@ -438,7 +393,7 @@ class UserProfilePhotos(JsonDeserializable):
|
|||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
total_count = obj['total_count']
|
total_count = obj['total_count']
|
||||||
photos = [[PhotoSize.de_json(y) for y in x] for x in obj['photos']]
|
photos = [[PhotoSize.de_json(y) for y in x] for x in obj['photos']]
|
||||||
return UserProfilePhotos(total_count, photos)
|
return cls(total_count, photos)
|
||||||
|
|
||||||
def __init__(self, total_count, photos):
|
def __init__(self, total_count, photos):
|
||||||
self.total_count = total_count
|
self.total_count = total_count
|
||||||
@ -450,14 +405,9 @@ class File(JsonDeserializable):
|
|||||||
def de_json(cls, json_type):
|
def de_json(cls, json_type):
|
||||||
obj = cls.check_json(json_type)
|
obj = cls.check_json(json_type)
|
||||||
file_id = obj['file_id']
|
file_id = obj['file_id']
|
||||||
|
file_size = obj.get('file_size')
|
||||||
file_size = None
|
file_path = obj.get('file_path')
|
||||||
file_path = None
|
return cls(file_id, file_size, file_path)
|
||||||
if 'file_size' in obj:
|
|
||||||
file_size = obj['file_size']
|
|
||||||
if 'file_path' in obj:
|
|
||||||
file_path = obj['file_path']
|
|
||||||
return File(file_id, file_size, file_path)
|
|
||||||
|
|
||||||
def __init__(self, file_id, file_size, file_path):
|
def __init__(self, file_id, file_size, file_path):
|
||||||
self.file_id = file_id
|
self.file_id = file_id
|
||||||
|
Loading…
Reference in New Issue
Block a user