2015-06-26 09:55:13 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Available types
|
|
|
|
|
|
|
|
User
|
|
|
|
GroupChat
|
|
|
|
Message
|
|
|
|
PhotoSize
|
|
|
|
Audio
|
|
|
|
Document
|
|
|
|
Sticker
|
|
|
|
Video
|
|
|
|
Contact
|
|
|
|
Location
|
|
|
|
Update
|
|
|
|
InputFile
|
|
|
|
UserProfilePhotos
|
|
|
|
ReplyKeyboardMarkup
|
|
|
|
ReplyKeyboardHide
|
|
|
|
ForceReply
|
|
|
|
"""
|
|
|
|
|
2015-06-26 10:15:30 +03:00
|
|
|
import json
|
2015-10-08 23:46:15 +03:00
|
|
|
import six
|
2015-06-26 10:15:30 +03:00
|
|
|
|
2015-07-01 23:34:40 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class JsonSerializable:
|
2015-07-01 19:17:33 +03:00
|
|
|
"""
|
|
|
|
Subclasses of this class are guaranteed to be able to be converted to JSON format.
|
|
|
|
All subclasses of this class must override to_json.
|
|
|
|
"""
|
2015-10-12 05:36:58 +03:00
|
|
|
|
2015-07-01 19:17:33 +03:00
|
|
|
def to_json(self):
|
|
|
|
"""
|
|
|
|
Returns a JSON string representation of this class.
|
|
|
|
|
|
|
|
This function must be overridden by subclasses.
|
|
|
|
:return: a JSON formatted string.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2015-06-26 10:15:30 +03:00
|
|
|
|
2015-07-01 23:34:40 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class JsonDeserializable:
|
|
|
|
"""
|
|
|
|
Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string.
|
|
|
|
All subclasses of this class must override de_json.
|
|
|
|
"""
|
2015-10-12 05:36:58 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
@classmethod
|
2015-07-01 23:34:40 +03:00
|
|
|
def de_json(cls, json_type):
|
2015-07-01 23:16:13 +03:00
|
|
|
"""
|
|
|
|
Returns an instance of this class from the given json dict or string.
|
|
|
|
|
|
|
|
This function must be overridden by subclasses.
|
|
|
|
:return: an instance of this class created from the given json dict or string.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
2015-07-01 23:34:40 +03:00
|
|
|
def check_json(json_type):
|
2015-07-01 23:16:13 +03:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
:param json_type:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if type(json_type) == dict:
|
|
|
|
return json_type
|
|
|
|
elif type(json_type) == str:
|
|
|
|
return json.loads(json_type)
|
|
|
|
else:
|
|
|
|
raise ValueError("json_type should be a json dict or string.")
|
|
|
|
|
2015-09-01 22:55:02 +03:00
|
|
|
def __str__(self):
|
|
|
|
d = {}
|
2015-10-08 23:39:22 +03:00
|
|
|
for x, y in six.iteritems(self.__dict__):
|
2015-09-01 22:55:02 +03:00
|
|
|
if hasattr(y, '__dict__'):
|
|
|
|
d[x] = y.__dict__
|
|
|
|
else:
|
|
|
|
d[x] = y
|
|
|
|
|
2015-10-08 23:46:15 +03:00
|
|
|
return six.text_type(d)
|
2015-09-01 22:55:02 +03:00
|
|
|
|
2015-10-12 05:36:58 +03:00
|
|
|
|
2015-09-05 12:58:53 +03:00
|
|
|
class Update(JsonDeserializable):
|
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_type):
|
|
|
|
obj = cls.check_json(json_type)
|
|
|
|
update_id = obj['update_id']
|
|
|
|
message = Message.de_json(obj['message'])
|
2015-10-15 17:53:59 +03:00
|
|
|
return cls(update_id, message)
|
2015-09-05 12:58:53 +03:00
|
|
|
|
|
|
|
def __init__(self, update_id, message):
|
|
|
|
self.update_id = update_id
|
|
|
|
self.message = message
|
2015-07-01 23:16:13 +03:00
|
|
|
|
2015-10-12 05:36:58 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class User(JsonDeserializable):
|
2015-06-26 10:15:30 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-26 10:15:30 +03:00
|
|
|
id = obj['id']
|
|
|
|
first_name = obj['first_name']
|
2015-10-15 17:53:59 +03:00
|
|
|
last_name = obj.get('last_name')
|
|
|
|
username = obj.get('username')
|
|
|
|
return cls(id, first_name, last_name, username)
|
2015-06-26 10:15:30 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, id, first_name, last_name=None, username=None):
|
|
|
|
self.id = id
|
|
|
|
self.first_name = first_name
|
|
|
|
self.username = username
|
|
|
|
self.last_name = last_name
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class GroupChat(JsonDeserializable):
|
2015-06-26 13:02:30 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-26 13:02:30 +03:00
|
|
|
id = obj['id']
|
|
|
|
title = obj['title']
|
2015-10-15 17:53:59 +03:00
|
|
|
return cls(id, title)
|
2015-06-26 13:02:30 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, id, title):
|
|
|
|
self.id = id
|
|
|
|
self.title = title
|
|
|
|
|
|
|
|
|
2015-10-12 05:36:58 +03:00
|
|
|
class Chat(JsonDeserializable):
|
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
|
|
|
obj = cls.check_json(json_string)
|
|
|
|
id = obj['id']
|
|
|
|
type = obj['type']
|
2015-10-15 17:53:59 +03:00
|
|
|
title = obj.get('title')
|
|
|
|
username = obj.get('username')
|
|
|
|
first_name = obj.get('first_name')
|
|
|
|
last_name = obj.get('last_name')
|
|
|
|
return cls(id, type, title, username, first_name, last_name)
|
2015-10-12 05:36:58 +03:00
|
|
|
|
|
|
|
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None):
|
|
|
|
self.type = type
|
|
|
|
self.last_name = last_name
|
|
|
|
self.first_name = first_name
|
|
|
|
self.username = username
|
|
|
|
self.id = id
|
|
|
|
self.title = title
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Message(JsonDeserializable):
|
2015-06-26 13:02:30 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-26 13:02:30 +03:00
|
|
|
message_id = obj['message_id']
|
2015-10-12 05:36:58 +03:00
|
|
|
from_user = None
|
|
|
|
if 'from' in obj:
|
|
|
|
from_user = User.de_json(obj['from'])
|
|
|
|
chat = Chat.de_json(obj['chat'])
|
2015-06-26 13:02:30 +03:00
|
|
|
date = obj['date']
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = None
|
2015-06-26 16:56:49 +03:00
|
|
|
opts = {}
|
2015-07-17 09:40:49 +03:00
|
|
|
if 'forward_from' in obj:
|
|
|
|
opts['forward_from'] = User.de_json(obj['forward_from'])
|
|
|
|
if 'forward_date' in obj:
|
|
|
|
opts['forward_date'] = obj['forward_date']
|
|
|
|
if 'reply_to_message' in obj:
|
|
|
|
opts['reply_to_message'] = Message.de_json(obj['reply_to_message'])
|
2015-06-26 13:02:30 +03:00
|
|
|
if 'text' in obj:
|
2015-06-26 16:56:49 +03:00
|
|
|
opts['text'] = obj['text']
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'text'
|
|
|
|
if 'audio' in obj:
|
2015-07-01 23:34:40 +03:00
|
|
|
opts['audio'] = Audio.de_json(obj['audio'])
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'audio'
|
2015-08-19 13:08:01 +03:00
|
|
|
if 'voice' in obj:
|
|
|
|
opts['voice'] = Audio.de_json(obj['voice'])
|
|
|
|
content_type = 'voice'
|
2015-06-28 12:19:15 +03:00
|
|
|
if 'document' in obj:
|
2015-07-01 23:34:40 +03:00
|
|
|
opts['document'] = Document.de_json(obj['document'])
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'document'
|
|
|
|
if 'photo' in obj:
|
|
|
|
opts['photo'] = Message.parse_photo(obj['photo'])
|
|
|
|
content_type = 'photo'
|
|
|
|
if 'sticker' in obj:
|
2015-07-01 23:34:40 +03:00
|
|
|
opts['sticker'] = Sticker.de_json(obj['sticker'])
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'sticker'
|
|
|
|
if 'video' in obj:
|
2015-07-01 23:34:40 +03:00
|
|
|
opts['video'] = Video.de_json(obj['video'])
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'video'
|
|
|
|
if 'location' in obj:
|
2015-07-01 23:34:40 +03:00
|
|
|
opts['location'] = Location.de_json(obj['location'])
|
2015-06-28 12:19:15 +03:00
|
|
|
content_type = 'location'
|
2015-07-02 06:19:38 +03:00
|
|
|
if 'contact' in obj:
|
|
|
|
opts['contact'] = Contact.de_json(json.dumps(obj['contact']))
|
|
|
|
content_type = 'contact'
|
2015-07-03 04:41:11 +03:00
|
|
|
if 'new_chat_participant' in obj:
|
|
|
|
opts['new_chat_participant'] = User.de_json(obj['new_chat_participant'])
|
|
|
|
content_type = 'new_chat_participant'
|
|
|
|
if 'left_chat_participant' in obj:
|
|
|
|
opts['left_chat_participant'] = User.de_json(obj['left_chat_participant'])
|
|
|
|
content_type = 'left_chat_participant'
|
|
|
|
if 'new_chat_title' in obj:
|
|
|
|
opts['new_chat_title'] = obj['new_chat_title']
|
|
|
|
content_type = 'new_chat_title'
|
|
|
|
if 'new_chat_photo' in obj:
|
|
|
|
opts['new_chat_photo'] = obj['new_chat_photo']
|
|
|
|
content_type = 'new_chat_photo'
|
2015-07-03 04:43:26 +03:00
|
|
|
if 'delete_chat_photo' in obj:
|
|
|
|
opts['delete_chat_photo'] = obj['delete_chat_photo']
|
2015-07-03 05:14:42 +03:00
|
|
|
content_type = 'delete_chat_photo'
|
2015-07-03 04:43:26 +03:00
|
|
|
if 'group_chat_created' in obj:
|
|
|
|
opts['group_chat_created'] = obj['group_chat_created']
|
2015-07-03 05:14:42 +03:00
|
|
|
content_type = 'group_chat_created'
|
2015-07-26 12:19:20 +03:00
|
|
|
if 'caption' in obj:
|
|
|
|
opts['caption'] = obj['caption']
|
2015-10-15 17:53:59 +03:00
|
|
|
return cls(message_id, from_user, date, chat, content_type, opts)
|
2015-06-26 13:02:30 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse_chat(cls, chat):
|
2015-06-28 12:19:15 +03:00
|
|
|
if 'first_name' not in chat:
|
2015-07-01 23:34:40 +03:00
|
|
|
return GroupChat.de_json(chat)
|
2015-06-26 13:02:30 +03:00
|
|
|
else:
|
2015-07-01 23:34:40 +03:00
|
|
|
return User.de_json(chat)
|
2015-06-26 13:02:30 +03:00
|
|
|
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def parse_photo(cls, photo_size_array):
|
|
|
|
ret = []
|
|
|
|
for ps in photo_size_array:
|
2015-07-01 23:34:40 +03:00
|
|
|
ret.append(PhotoSize.de_json(ps))
|
2015-06-28 12:19:15 +03:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def __init__(self, message_id, from_user, date, chat, content_type, options):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.chat = chat
|
|
|
|
self.date = date
|
2015-07-03 20:34:02 +03:00
|
|
|
self.from_user = from_user
|
2015-06-26 09:55:13 +03:00
|
|
|
self.message_id = message_id
|
2015-06-28 12:19:15 +03:00
|
|
|
self.content_type = content_type
|
2015-06-26 13:02:30 +03:00
|
|
|
for key in options:
|
|
|
|
setattr(self, key, options[key])
|
2015-06-26 09:55:13 +03:00
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class PhotoSize(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
file_id = obj['file_id']
|
|
|
|
width = obj['width']
|
|
|
|
height = obj['height']
|
2015-10-15 17:53:59 +03:00
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, width, height, file_size)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
|
|
|
def __init__(self, file_id, width, height, file_size=None):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.file_size = file_size
|
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
self.file_id = file_id
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Audio(JsonDeserializable):
|
2015-08-19 13:08:01 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
|
|
|
obj = cls.check_json(json_string)
|
|
|
|
file_id = obj['file_id']
|
|
|
|
duration = obj['duration']
|
2015-10-15 17:53:59 +03:00
|
|
|
performer = obj.get('performer')
|
|
|
|
title = obj.get('title')
|
|
|
|
mime_type = obj.get('mime_type')
|
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, duration, performer, title, mime_type, file_size)
|
2015-08-19 13:08:01 +03:00
|
|
|
|
|
|
|
def __init__(self, file_id, duration, performer=None, title=None, mime_type=None, file_size=None):
|
|
|
|
self.file_id = file_id
|
|
|
|
self.duration = duration
|
|
|
|
self.performer = performer
|
|
|
|
self.title = title
|
|
|
|
self.mime_type = mime_type
|
|
|
|
self.file_size = file_size
|
|
|
|
|
|
|
|
|
|
|
|
class Voice(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
file_id = obj['file_id']
|
|
|
|
duration = obj['duration']
|
2015-10-15 17:53:59 +03:00
|
|
|
mime_type = obj.get('mime_type')
|
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, duration, mime_type, file_size)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, file_id, duration, mime_type=None, file_size=None):
|
|
|
|
self.file_id = file_id
|
|
|
|
self.duration = duration
|
|
|
|
self.mime_type = mime_type
|
|
|
|
self.file_size = file_size
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Document(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
file_id = obj['file_id']
|
|
|
|
thumb = None
|
2015-10-15 17:53:59 +03:00
|
|
|
if 'thumb' in obj and 'file_id' in obj['thumb']:
|
|
|
|
thumb = PhotoSize.de_json(obj['thumb'])
|
|
|
|
file_name = obj.get('file_name')
|
|
|
|
mime_type = obj.get('mime_type')
|
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, thumb, file_name, mime_type, file_size)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, file_id, thumb, file_name=None, mime_type=None, file_size=None):
|
|
|
|
self.file_id = file_id
|
|
|
|
self.thumb = thumb
|
|
|
|
self.file_name = file_name
|
|
|
|
self.mime_type = mime_type
|
|
|
|
self.file_size = file_size
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Sticker(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
file_id = obj['file_id']
|
|
|
|
width = obj['width']
|
|
|
|
height = obj['height']
|
2015-08-01 04:55:17 +03:00
|
|
|
thumb = None
|
|
|
|
if 'thumb' in obj:
|
|
|
|
thumb = PhotoSize.de_json(obj['thumb'])
|
2015-10-15 17:53:59 +03:00
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, width, height, thumb, file_size)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, file_id, width, height, thumb, file_size=None):
|
|
|
|
self.file_id = file_id
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.thumb = thumb
|
|
|
|
self.file_size = file_size
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Video(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
file_id = obj['file_id']
|
|
|
|
width = obj['width']
|
|
|
|
height = obj['height']
|
|
|
|
duration = obj['duration']
|
2015-10-16 21:30:22 +03:00
|
|
|
thumb = None
|
|
|
|
if 'thumb' in obj:
|
|
|
|
thumb = PhotoSize.de_json(obj['thumb'])
|
2015-10-15 17:53:59 +03:00
|
|
|
mime_type = obj.get('mime_type')
|
|
|
|
file_size = obj.get('file_size')
|
|
|
|
return cls(file_id, width, height, duration, thumb, mime_type, file_size)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
2015-07-26 12:19:20 +03:00
|
|
|
def __init__(self, file_id, width, height, duration, thumb=None, mime_type=None, file_size=None):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.file_id = file_id
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.duration = duration
|
|
|
|
self.thumb = thumb
|
|
|
|
self.mime_type = mime_type
|
|
|
|
self.file_size = file_size
|
|
|
|
|
|
|
|
|
2015-07-02 15:12:10 +03:00
|
|
|
class Contact(JsonDeserializable):
|
2015-07-02 06:19:38 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-02 15:12:10 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-07-02 06:19:38 +03:00
|
|
|
phone_number = obj['phone_number']
|
|
|
|
first_name = obj['first_name']
|
2015-10-15 17:53:59 +03:00
|
|
|
last_name = obj.get('last_name')
|
|
|
|
user_id = obj.get('user_id')
|
|
|
|
return cls(phone_number, first_name, last_name, user_id)
|
2015-07-02 06:19:38 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def __init__(self, phone_number, first_name, last_name=None, user_id=None):
|
|
|
|
self.phone_number = phone_number
|
|
|
|
self.first_name = first_name
|
|
|
|
self.last_name = last_name
|
|
|
|
self.user_id = user_id
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class Location(JsonDeserializable):
|
2015-06-28 12:19:15 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-28 12:19:15 +03:00
|
|
|
longitude = obj['longitude']
|
|
|
|
latitude = obj['latitude']
|
2015-10-15 17:53:59 +03:00
|
|
|
return cls(longitude, latitude)
|
2015-06-28 12:19:15 +03:00
|
|
|
|
2015-06-26 10:15:30 +03:00
|
|
|
def __init__(self, longitude, latitude):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.longitude = longitude
|
|
|
|
self.latitude = latitude
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class UserProfilePhotos(JsonDeserializable):
|
2015-06-30 17:40:44 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_string):
|
2015-07-01 23:16:13 +03:00
|
|
|
obj = cls.check_json(json_string)
|
2015-06-30 17:40:44 +03:00
|
|
|
total_count = obj['total_count']
|
2015-07-01 23:34:40 +03:00
|
|
|
photos = [[PhotoSize.de_json(y) for y in x] for x in obj['photos']]
|
2015-10-15 17:53:59 +03:00
|
|
|
return cls(total_count, photos)
|
2015-06-30 17:40:44 +03:00
|
|
|
|
2015-06-26 10:15:30 +03:00
|
|
|
def __init__(self, total_count, photos):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.total_count = total_count
|
|
|
|
self.photos = photos
|
|
|
|
|
2015-09-18 21:31:29 +03:00
|
|
|
|
2015-10-12 05:36:58 +03:00
|
|
|
class File(JsonDeserializable):
|
2015-09-18 21:31:29 +03:00
|
|
|
@classmethod
|
|
|
|
def de_json(cls, json_type):
|
|
|
|
obj = cls.check_json(json_type)
|
|
|
|
file_id = obj['file_id']
|
2015-10-15 17:53:59 +03:00
|
|
|
file_size = obj.get('file_size')
|
|
|
|
file_path = obj.get('file_path')
|
|
|
|
return cls(file_id, file_size, file_path)
|
2015-09-18 21:31:29 +03:00
|
|
|
|
|
|
|
def __init__(self, file_id, file_size, file_path):
|
|
|
|
self.file_id = file_id
|
|
|
|
self.file_size = file_size
|
|
|
|
self.file_path = file_path
|
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class ForceReply(JsonSerializable):
|
2015-07-01 19:17:33 +03:00
|
|
|
def __init__(self, selective=None):
|
|
|
|
self.selective = selective
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
json_dict = {'force_reply': True}
|
|
|
|
if self.selective:
|
|
|
|
json_dict['selective'] = True
|
|
|
|
return json.dumps(json_dict)
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class ReplyKeyboardHide(JsonSerializable):
|
2015-07-01 19:17:33 +03:00
|
|
|
def __init__(self, selective=None):
|
|
|
|
self.selective = selective
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
json_dict = {'hide_keyboard': True}
|
|
|
|
if self.selective:
|
|
|
|
json_dict['selective'] = True
|
|
|
|
return json.dumps(json_dict)
|
|
|
|
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
class ReplyKeyboardMarkup(JsonSerializable):
|
2015-06-30 06:54:04 +03:00
|
|
|
def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3):
|
2015-06-26 09:55:13 +03:00
|
|
|
self.resize_keyboard = resize_keyboard
|
|
|
|
self.one_time_keyboard = one_time_keyboard
|
|
|
|
self.selective = selective
|
2015-06-30 03:00:47 +03:00
|
|
|
self.row_width = row_width
|
|
|
|
|
|
|
|
self.keyboard = []
|
|
|
|
|
|
|
|
def add(self, *args):
|
|
|
|
"""
|
|
|
|
This function adds strings to the keyboard, while not exceeding row_width.
|
|
|
|
E.g. ReplyKeyboardMarkup#add("A", "B", "C") yields the json result {keyboard: [["A"], ["B"], ["C"]]}
|
|
|
|
when row_width is set to 1.
|
|
|
|
When row_width is set to 2, the following is the result of this function: {keyboard: [["A", "B"], ["C"]]}
|
|
|
|
See https://core.telegram.org/bots/api#replykeyboardmarkup
|
|
|
|
:param args: strings to append to the keyboard
|
|
|
|
"""
|
|
|
|
i = 1
|
|
|
|
row = []
|
|
|
|
for string in args:
|
|
|
|
row.append(string)
|
|
|
|
if i % self.row_width == 0:
|
|
|
|
self.keyboard.append(row)
|
|
|
|
row = []
|
|
|
|
i += 1
|
|
|
|
if len(row) > 0:
|
|
|
|
self.keyboard.append(row)
|
|
|
|
|
|
|
|
def row(self, *args):
|
|
|
|
"""
|
|
|
|
Adds a list of strings to the keyboard. This function does not consider row_width.
|
|
|
|
ReplyKeyboardMarkup#row("A")#row("B", "C")#to_json() outputs '{keyboard: [["A"], ["B", "C"]]}'
|
|
|
|
See https://core.telegram.org/bots/api#replykeyboardmarkup
|
|
|
|
:param args: strings
|
|
|
|
:return: self, to allow function chaining.
|
|
|
|
"""
|
|
|
|
self.keyboard.append(args)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
"""
|
|
|
|
Converts this object to its json representation following the Telegram API guidelines described here:
|
|
|
|
https://core.telegram.org/bots/api#replykeyboardmarkup
|
|
|
|
:return:
|
|
|
|
"""
|
2015-06-30 06:54:04 +03:00
|
|
|
json_dict = {'keyboard': self.keyboard}
|
|
|
|
if self.one_time_keyboard:
|
2015-06-30 03:00:47 +03:00
|
|
|
json_dict['one_time_keyboard'] = True
|
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
if self.resize_keyboard:
|
2015-06-30 03:00:47 +03:00
|
|
|
json_dict['resize_keyboard'] = True
|
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
if self.selective:
|
2015-06-30 03:00:47 +03:00
|
|
|
json_dict['selective'] = True
|
|
|
|
|
|
|
|
return json.dumps(json_dict)
|