1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/telebot/types.py

2013 lines
75 KiB
Python
Raw Normal View History

2015-06-26 09:55:13 +03:00
# -*- coding: utf-8 -*-
2016-01-05 04:57:25 +03:00
2016-09-12 11:38:54 +03:00
try:
import ujson as json
except ImportError:
import json
2015-10-08 23:46:15 +03:00
import six
2015-06-26 10:15:30 +03:00
from telebot import util
2015-07-01 23:34:40 +03:00
class JsonSerializable:
"""
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
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
2016-04-14 10:50:55 +03:00
class Dictionaryable:
"""
Subclasses of this class are guaranteed to be able to be converted to dictionary.
All subclasses of this class must override to_dic.
"""
def to_dic(self):
"""
Returns a JSON string representation of this class.
This function must be overridden by subclasses.
:return: a JSON formatted string.
"""
raise NotImplementedError
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
@classmethod
2015-07-01 23:34:40 +03:00
def de_json(cls, json_type):
"""
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):
"""
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:
"""
2016-06-04 19:18:09 +03:00
try:
str_types = (str, unicode)
except NameError:
str_types = (str,)
if type(json_type) == dict:
return json_type
2016-06-04 19:18:09 +03:00
elif type(json_type) in str_types:
return json.loads(json_type)
else:
raise ValueError("json_type should be a json dict or string.")
def __str__(self):
d = {}
2015-10-08 23:39:22 +03:00
for x, y in six.iteritems(self.__dict__):
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-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']
2016-01-04 17:29:04 +03:00
message = None
2016-06-07 14:29:12 +03:00
edited_message = None
2016-12-03 10:17:06 +03:00
channel_post = None
edited_channel_post = None
2016-01-04 17:29:04 +03:00
inline_query = None
chosen_inline_result = None
2016-04-16 09:18:19 +03:00
callback_query = None
shipping_query = None
pre_checkout_query = None
2016-01-04 17:29:04 +03:00
if 'message' in obj:
message = Message.de_json(obj['message'])
2016-06-07 14:29:12 +03:00
if 'edited_message' in obj:
edited_message = Message.de_json(obj['edited_message'])
if 'channel_post' in obj:
channel_post = Message.de_json(obj['channel_post'])
if 'edited_channel_post' in obj:
edited_channel_post = Message.de_json(obj['edited_channel_post'])
2016-01-04 17:29:04 +03:00
if 'inline_query' in obj:
inline_query = InlineQuery.de_json(obj['inline_query'])
2016-01-04 17:53:08 +03:00
if 'chosen_inline_result' in obj:
chosen_inline_result = ChosenInlineResult.de_json(obj['chosen_inline_result'])
2016-04-16 09:18:19 +03:00
if 'callback_query' in obj:
callback_query = CallbackQuery.de_json(obj['callback_query'])
if 'shipping_query' in obj:
shipping_query = ShippingQuery.de_json(obj['shipping_query'])
if 'pre_checkout_query' in obj:
pre_checkout_query = PreCheckoutQuery.de_json(obj['pre_checkout_query'])
return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
chosen_inline_result, callback_query, shipping_query, pre_checkout_query)
2016-01-04 17:29:04 +03:00
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):
2015-09-05 12:58:53 +03:00
self.update_id = update_id
2016-06-07 14:29:12 +03:00
self.edited_message = edited_message
2015-09-05 12:58:53 +03:00
self.message = message
self.edited_message = edited_message
self.channel_post = channel_post
self.edited_channel_post = edited_channel_post
2016-01-04 17:29:04 +03:00
self.inline_query = inline_query
self.chosen_inline_result = chosen_inline_result
2016-04-16 09:18:19 +03:00
self.callback_query = callback_query
self.shipping_query = shipping_query
self.pre_checkout_query = pre_checkout_query
2015-10-12 05:36:58 +03:00
class WebhookInfo(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
url = obj['url']
has_custom_certificate = obj['has_custom_certificate']
pending_update_count = obj['pending_update_count']
last_error_date = None
last_error_message = None
2017-05-30 12:17:29 +03:00
max_connections = None
allowed_updates = None
if 'last_error_message' in obj:
last_error_date = obj['last_error_date']
if 'last_error_message' in obj:
last_error_message = obj['last_error_message']
if 'max_connections' in obj:
max_connections = obj['max_connections']
if 'allowed_updates' in obj:
allowed_updates = obj['allowed_updates']
2017-05-21 16:45:12 +03:00
return cls(url, has_custom_certificate, pending_update_count, last_error_date, last_error_message,
max_connections, allowed_updates)
2017-05-21 16:45:12 +03:00
def __init__(self, url, has_custom_certificate, pending_update_count, last_error_date, last_error_message,
max_connections, allowed_updates):
self.url = url
self.has_custom_certificate = has_custom_certificate
self.pending_update_count = pending_update_count
self.last_error_date = last_error_date
self.last_error_message = last_error_message
self.max_connections = max_connections
self.allowed_updates = allowed_updates
class User(JsonDeserializable):
2015-06-26 10:15:30 +03:00
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
2015-06-26 10:15:30 +03:00
id = obj['id']
is_bot = obj['is_bot']
2015-06-26 10:15:30 +03:00
first_name = obj['first_name']
last_name = obj.get('last_name')
username = obj.get('username')
2017-05-19 00:40:10 +03:00
language_code = obj.get('language_code')
return cls(id, is_bot, first_name, last_name, username, language_code)
2015-06-26 10:15:30 +03:00
def __init__(self, id, is_bot, first_name, last_name=None, username=None, language_code=None):
2015-06-26 09:55:13 +03:00
self.id = id
self.is_bot = is_bot
2015-06-26 09:55:13 +03:00
self.first_name = first_name
self.username = username
self.last_name = last_name
2017-05-19 00:40:10 +03:00
self.language_code = language_code
2015-06-26 09:55:13 +03:00
class GroupChat(JsonDeserializable):
2015-06-26 13:02:30 +03:00
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
2015-06-26 13:02:30 +03:00
id = obj['id']
title = obj['title']
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']
title = obj.get('title')
username = obj.get('username')
first_name = obj.get('first_name')
last_name = obj.get('last_name')
all_members_are_administrators = obj.get('all_members_are_administrators')
2017-07-01 06:05:14 +03:00
photo = None
2017-06-30 19:47:09 +03:00
if 'photo' in obj:
photo = ChatPhoto.de_json(obj['photo'])
description = obj.get('description')
invite_link = obj.get('invite_link')
pinned_message = None
if 'pinned_message' in obj:
pinned_message = Message.de_json(obj['pinned_message'])
sticker_set_name = obj.get('sticker_set_name')
can_set_sticker_set = obj.get('can_set_sticker_set')
2017-06-30 19:47:09 +03:00
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators,
photo, description, invite_link, pinned_message, sticker_set_name, can_set_sticker_set)
2015-10-12 05:36:58 +03:00
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None,
all_members_are_administrators=None, photo=None, description=None, invite_link=None,
pinned_message=None, sticker_set_name=None, can_set_sticker_set=None):
2015-10-12 05:36:58 +03:00
self.type = type
self.last_name = last_name
self.first_name = first_name
self.username = username
self.id = id
self.title = title
self.all_members_are_administrators = all_members_are_administrators
2017-06-30 19:47:09 +03:00
self.photo = photo
self.description = description
self.invite_link = invite_link
self.pinned_message = pinned_message
self.sticker_set_name = sticker_set_name
self.can_set_sticker_set = can_set_sticker_set
2015-10-12 05:36:58 +03:00
class Message(JsonDeserializable):
2015-06-26 13:02:30 +03:00
@classmethod
def de_json(cls, json_string):
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'])
2015-06-26 13:02:30 +03:00
date = obj['date']
2016-04-14 05:57:18 +03:00
chat = Chat.de_json(obj['chat'])
content_type = None
2015-06-26 16:56:49 +03:00
opts = {}
if 'forward_from' in obj:
opts['forward_from'] = User.de_json(obj['forward_from'])
if 'forward_from_chat' in obj:
opts['forward_from_chat'] = Chat.de_json(obj['forward_from_chat'])
if 'forward_from_message_id' in obj:
opts['forward_from_message_id'] = obj.get('forward_from_message_id')
if 'forward_signature' in obj:
opts['forward_signature'] = obj.get('forward_signature')
if 'forward_date' in obj:
opts['forward_date'] = obj.get('forward_date')
if 'reply_to_message' in obj:
opts['reply_to_message'] = Message.de_json(obj['reply_to_message'])
if 'edit_date' in obj:
opts['edit_date'] = obj.get('edit_date')
if 'author_signature' in obj:
opts['author_signature'] = obj.get('author_signature')
2015-06-26 13:02:30 +03:00
if 'text' in obj:
2015-06-26 16:56:49 +03:00
opts['text'] = obj['text']
content_type = 'text'
2016-04-14 05:57:18 +03:00
if 'entities' in obj:
opts['entities'] = Message.parse_entities(obj['entities'])
if 'caption_entities' in obj:
opts['caption_entities'] = Message.parse_entities(obj['caption_entities'])
if 'audio' in obj:
2015-07-01 23:34:40 +03:00
opts['audio'] = Audio.de_json(obj['audio'])
content_type = 'audio'
if 'document' in obj:
2015-07-01 23:34:40 +03:00
opts['document'] = Document.de_json(obj['document'])
content_type = 'document'
if 'game' in obj:
2016-10-29 23:23:39 +03:00
opts['game'] = Game.de_json(obj['game'])
content_type = 'game'
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'])
content_type = 'sticker'
if 'video' in obj:
2015-07-01 23:34:40 +03:00
opts['video'] = Video.de_json(obj['video'])
content_type = 'video'
if 'video_note' in obj:
opts['video_note'] = VideoNote.de_json(obj['video_note'])
content_type = 'video_note'
2016-04-14 05:57:18 +03:00
if 'voice' in obj:
opts['voice'] = Audio.de_json(obj['voice'])
content_type = 'voice'
if 'caption' in obj:
opts['caption'] = obj['caption']
2015-07-02 06:19:38 +03:00
if 'contact' in obj:
opts['contact'] = Contact.de_json(json.dumps(obj['contact']))
content_type = 'contact'
2016-04-14 06:02:19 +03:00
if 'location' in obj:
opts['location'] = Location.de_json(obj['location'])
content_type = 'location'
if 'venue' in obj:
opts['venue'] = Venue.de_json(obj['venue'])
content_type = 'venue'
2016-04-14 06:15:58 +03:00
if 'new_chat_member' in obj:
opts['new_chat_member'] = User.de_json(obj['new_chat_member'])
content_type = 'new_chat_member'
if 'new_chat_members' in obj:
2017-07-02 16:24:19 +03:00
chat_members = obj['new_chat_members']
nms = []
for m in chat_members:
nms.append(User.de_json(m))
opts['new_chat_members'] = nms
content_type = 'new_chat_members'
2016-04-14 06:15:58 +03:00
if 'left_chat_member' in obj:
opts['left_chat_member'] = User.de_json(obj['left_chat_member'])
content_type = 'left_chat_member'
2015-07-03 04:41:11 +03:00
if 'new_chat_title' in obj:
opts['new_chat_title'] = obj['new_chat_title']
2017-11-30 18:34:07 +03:00
content_type = 'new_chat_title'
2015-07-03 04:41:11 +03:00
if 'new_chat_photo' in obj:
2016-04-14 06:15:58 +03:00
opts['new_chat_photo'] = Message.parse_photo(obj['new_chat_photo'])
2017-11-30 18:34:07 +03:00
content_type = 'new_chat_photo'
if 'delete_chat_photo' in obj:
opts['delete_chat_photo'] = obj['delete_chat_photo']
2017-11-30 18:34:07 +03:00
content_type = 'delete_chat_photo'
if 'group_chat_created' in obj:
opts['group_chat_created'] = obj['group_chat_created']
content_type = 'group_chat_created'
2016-04-14 06:15:58 +03:00
if 'supergroup_chat_created' in obj:
opts['supergroup_chat_created'] = obj['supergroup_chat_created']
content_type = 'supergroup_chat_created'
2016-04-14 06:15:58 +03:00
if 'channel_chat_created' in obj:
opts['channel_chat_created'] = obj['channel_chat_created']
content_type = 'channel_chat_created'
2016-04-14 06:15:58 +03:00
if 'migrate_to_chat_id' in obj:
opts['migrate_to_chat_id'] = obj['migrate_to_chat_id']
2017-11-30 18:34:07 +03:00
content_type = 'migrate_to_chat_id'
2016-04-14 06:15:58 +03:00
if 'migrate_from_chat_id' in obj:
opts['migrate_from_chat_id'] = obj['migrate_from_chat_id']
2017-11-30 18:34:07 +03:00
content_type = 'migrate_from_chat_id'
2016-04-14 06:15:58 +03:00
if 'pinned_message' in obj:
opts['pinned_message'] = Message.de_json(obj['pinned_message'])
content_type = 'pinned_message'
if 'invoice' in obj:
opts['invoice'] = Invoice.de_json(obj['invoice'])
content_type = 'invoice'
if 'successful_payment' in obj:
opts['successful_payment'] = SuccessfulPayment.de_json(obj['successful_payment'])
content_type = 'successful_payment'
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):
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
@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))
return ret
2016-04-14 05:57:18 +03:00
@classmethod
def parse_entities(cls, message_entity_array):
ret = []
for me in message_entity_array:
ret.append(MessageEntity.de_json(me))
return ret
def __init__(self, message_id, from_user, date, chat, content_type, options):
self.content_type = content_type
2016-04-14 06:15:58 +03:00
self.message_id = message_id
self.from_user = from_user
self.date = date
self.chat = chat
self.forward_from_chat = None
self.forward_from = None
self.forward_date = None
self.reply_to_message = None
self.edit_date = None
self.author_signature = None
self.text = None
2016-04-14 06:15:58 +03:00
self.entities = None
self.caption_entities = None
self.audio = None
self.document = None
self.photo = None
self.sticker = None
self.video = None
self.video_note = None
2016-04-14 06:15:58 +03:00
self.voice = None
self.caption = None
self.contact = None
2016-04-14 06:15:58 +03:00
self.location = None
self.venue = None
self.new_chat_member = None
2017-07-02 16:24:19 +03:00
self.new_chat_members = None
2016-04-14 06:15:58 +03:00
self.left_chat_member = None
self.new_chat_title = None
self.new_chat_photo = None
self.delete_chat_photo = None
self.group_chat_created = None
2016-04-14 06:15:58 +03:00
self.supergroup_chat_created = None
self.channel_chat_created = None
self.migrate_to_chat_id = None
self.migrate_from_chat_id = None
self.pinned_message = None
self.invoice = None
self.successful_payment = None
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
2016-04-14 05:51:05 +03:00
class MessageEntity(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
type = obj['type']
offset = obj['offset']
length = obj['length']
2016-04-14 07:01:17 +03:00
url = obj.get('url')
2016-05-23 06:12:20 +03:00
user = None
if 'user' in obj:
user = User.de_json(obj['user'])
return cls(type, offset, length, url, user)
2016-04-14 05:51:05 +03:00
2016-05-23 06:12:20 +03:00
def __init__(self, type, offset, length, url=None, user=None):
2016-04-14 05:51:05 +03:00
self.type = type
self.offset = offset
self.length = length
self.url = url
2016-05-23 06:12:20 +03:00
self.user = user
2016-04-14 05:51:05 +03:00
class PhotoSize(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
width = obj['width']
height = obj['height']
file_size = obj.get('file_size')
return cls(file_id, width, height, file_size)
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
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']
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):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
duration = obj['duration']
mime_type = obj.get('mime_type')
file_size = obj.get('file_size')
return cls(file_id, duration, mime_type, file_size)
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
class Document(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
thumb = None
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)
2016-04-14 07:01:17 +03:00
def __init__(self, file_id, thumb=None, file_name=None, mime_type=None, file_size=None):
2015-06-26 09:55:13 +03:00
self.file_id = file_id
self.thumb = thumb
self.file_name = file_name
self.mime_type = mime_type
self.file_size = file_size
class Sticker(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
width = obj['width']
height = obj['height']
thumb = None
if 'thumb' in obj:
thumb = PhotoSize.de_json(obj['thumb'])
emoji = obj.get('emoji')
file_size = obj.get('file_size')
return cls(file_id, width, height, thumb, emoji, file_size)
def __init__(self, file_id, width, height, thumb, emoji=None, file_size=None):
2015-06-26 09:55:13 +03:00
self.file_id = file_id
self.width = width
self.height = height
self.thumb = thumb
self.emoji = emoji
2015-06-26 09:55:13 +03:00
self.file_size = file_size
class Video(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
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'])
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-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
class VideoNote(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
length = obj['length']
duration = obj['duration']
thumb = None
if 'thumb' in obj:
thumb = PhotoSize.de_json(obj['thumb'])
file_size = obj.get('file_size')
return cls(file_id, length, duration, thumb, file_size)
def __init__(self, file_id, length, duration, thumb=None, file_size=None):
self.file_id = file_id
self.length = length
self.duration = duration
self.thumb = thumb
self.file_size = file_size
class Contact(JsonDeserializable):
2015-07-02 06:19:38 +03:00
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
2015-07-02 06:19:38 +03:00
phone_number = obj['phone_number']
first_name = obj['first_name']
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
class Location(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
longitude = obj['longitude']
latitude = obj['latitude']
return cls(longitude, latitude)
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
2016-04-14 06:00:33 +03:00
class Venue(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
2016-09-17 02:38:18 +03:00
location = Location.de_json(obj['location'])
2016-04-14 06:00:33 +03:00
title = obj['title']
address = obj['address']
2016-04-14 07:01:17 +03:00
foursquare_id = obj.get('foursquare_id')
2016-04-14 06:00:33 +03:00
return cls(location, title, address, foursquare_id)
def __init__(self, location, title, address, foursquare_id=None):
self.location = location
self.title = title
self.address = address
self.foursquare_id = foursquare_id
class UserProfilePhotos(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
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']]
return cls(total_count, photos)
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-10-12 05:36:58 +03:00
class File(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
file_id = obj['file_id']
file_size = obj.get('file_size')
file_path = obj.get('file_path')
return cls(file_id, file_size, file_path)
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
class ForceReply(JsonSerializable):
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)
class ReplyKeyboardRemove(JsonSerializable):
def __init__(self, selective=None):
self.selective = selective
def to_json(self):
2016-11-21 08:57:38 +03:00
json_dict = {'remove_keyboard': True}
if self.selective:
json_dict['selective'] = True
return json.dumps(json_dict)
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
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
2016-04-14 07:01:17 +03:00
:param args: KeyboardButton to append to the keyboard
"""
i = 1
row = []
2016-04-14 07:01:17 +03:00
for button in args:
2016-05-19 11:02:59 +03:00
if util.is_string(button):
2016-05-03 15:59:03 +03:00
row.append({'text': button})
elif isinstance(button, bytes):
row.append({'text': button.decode('utf-8')})
2016-05-03 15:59:03 +03:00
else:
row.append(button.to_dic())
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):
"""
2016-04-14 07:01:17 +03:00
Adds a list of KeyboardButton 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.
"""
2016-04-25 18:54:30 +03:00
btn_array = []
for button in args:
2016-05-19 11:02:59 +03:00
if util.is_string(button):
2016-05-03 15:59:03 +03:00
btn_array.append({'text': button})
else:
btn_array.append(button.to_dic())
2016-04-25 18:54:30 +03:00
self.keyboard.append(btn_array)
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:
json_dict['one_time_keyboard'] = True
2015-06-30 06:54:04 +03:00
if self.resize_keyboard:
json_dict['resize_keyboard'] = True
2015-06-30 06:54:04 +03:00
if self.selective:
json_dict['selective'] = True
return json.dumps(json_dict)
2016-01-04 17:24:18 +03:00
2016-04-14 10:50:55 +03:00
class KeyboardButton(Dictionaryable, JsonSerializable):
2016-04-14 07:01:17 +03:00
def __init__(self, text, request_contact=None, request_location=None):
self.text = text
self.request_contact = request_contact
self.request_location = request_location
def to_json(self):
2016-04-14 10:50:55 +03:00
return json.dumps(self.to_dic())
2016-04-14 08:35:18 +03:00
def to_dic(self):
2016-04-14 07:01:17 +03:00
json_dic = {'text': self.text}
if self.request_contact:
json_dic['request_contact'] = self.request_contact
if self.request_location:
json_dic['request_location'] = self.request_location
2016-04-14 08:35:18 +03:00
return json_dic
2016-04-14 07:01:17 +03:00
2016-04-14 10:50:55 +03:00
class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
2016-04-14 07:01:17 +03:00
def __init__(self, row_width=3):
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: KeyboardButton to append to the keyboard
"""
i = 1
row = []
for button in args:
2016-04-14 08:35:18 +03:00
row.append(button.to_dic())
2016-04-14 07:01:17 +03:00
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 KeyboardButton 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#inlinekeyboardmarkup
:param args: strings
:return: self, to allow function chaining.
"""
2016-04-25 18:54:30 +03:00
btn_array = []
for button in args:
btn_array.append(button.to_dic())
self.keyboard.append(btn_array)
2016-04-14 07:01:17 +03:00
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#inlinekeyboardmarkup
:return:
"""
json_dict = {'inline_keyboard': self.keyboard}
return json.dumps(json_dict)
2016-04-14 12:09:12 +03:00
def to_dic(self):
json_dict = {'inline_keyboard': self.keyboard}
return json_dict
2016-04-14 07:01:17 +03:00
class InlineKeyboardButton(JsonSerializable):
def __init__(self, text, url=None, callback_data=None, switch_inline_query=None,
switch_inline_query_current_chat=None, callback_game=None, pay=None):
2016-04-14 07:01:17 +03:00
self.text = text
self.url = url
self.callback_data = callback_data
self.switch_inline_query = switch_inline_query
self.switch_inline_query_current_chat = switch_inline_query_current_chat
self.callback_game = callback_game
2017-05-25 06:48:16 +03:00
self.pay = pay
2016-04-14 07:01:17 +03:00
def to_json(self):
2016-04-14 08:35:18 +03:00
return json.dumps(self.to_dic())
def to_dic(self):
2016-04-14 07:01:17 +03:00
json_dic = {'text': self.text}
if self.url:
json_dic['url'] = self.url
if self.callback_data:
json_dic['callback_data'] = self.callback_data
if self.switch_inline_query is not None:
json_dic['switch_inline_query'] = self.switch_inline_query
if self.switch_inline_query_current_chat is not None:
json_dic['switch_inline_query_current_chat'] = self.switch_inline_query_current_chat
if self.callback_game is not None:
json_dic['callback_game'] = self.callback_game
2017-05-25 06:48:16 +03:00
if self.pay is not None:
json_dic['pay'] = self.pay
2016-04-14 08:35:18 +03:00
return json_dic
2016-04-14 07:01:17 +03:00
2016-04-14 10:50:55 +03:00
2016-04-14 07:01:17 +03:00
class CallbackQuery(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
id = obj['id']
from_user = User.de_json(obj['from'])
message = None
if 'message' in obj:
message = Message.de_json(obj['message'])
inline_message_id = obj.get('inline_message_id')
chat_instance = obj['chat_instance']
data = obj.get('data')
game_short_name = obj.get('game_short_name')
return cls(id, from_user, data, chat_instance, message, inline_message_id, game_short_name)
2016-04-14 07:01:17 +03:00
def __init__(self, id, from_user, data, chat_instance, message=None, inline_message_id=None, game_short_name=None):
self.game_short_name = game_short_name
self.chat_instance = chat_instance
2016-04-14 07:01:17 +03:00
self.id = id
self.from_user = from_user
self.message = message
self.data = data
self.inline_message_id = inline_message_id
2017-06-30 19:47:09 +03:00
class ChatPhoto(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
small_file_id = obj['small_file_id']
big_file_id = obj['big_file_id']
return cls(small_file_id, big_file_id)
def __init__(self, small_file_id, big_file_id):
self.small_file_id = small_file_id
self.big_file_id = big_file_id
2016-06-07 14:00:44 +03:00
class ChatMember(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
user = User.de_json(obj['user'])
status = obj['status']
2017-06-30 19:47:09 +03:00
until_date = obj.get('until_date')
can_be_edited = obj.get('can_be_edited')
can_change_info = obj.get('can_change_info')
can_post_messages = obj.get('can_post_messages')
can_edit_messages = obj.get('can_edit_messages')
can_delete_messages = obj.get('can_delete_messages')
can_invite_users = obj.get('can_invite_users')
can_restrict_members = obj.get('can_restrict_members')
can_pin_messages = obj.get('can_pin_messages')
can_promote_members = obj.get('can_promote_members')
can_send_messages = obj.get('can_send_messages')
can_send_media_messages = obj.get('can_send_media_messages')
can_send_other_messages = obj.get('can_send_other_messages')
can_add_web_page_previews = obj.get('can_add_web_page_previews')
return cls(user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews)
def __init__(self, user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
2017-08-06 07:00:26 +03:00
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews):
2016-06-07 14:00:44 +03:00
self.user = user
self.status = status
2017-06-30 19:47:09 +03:00
self.until_date = until_date
self.can_be_edited = can_be_edited
self.can_change_info = can_change_info
self.can_post_messages = can_post_messages
self.can_edit_messages = can_edit_messages
self.can_delete_messages = can_delete_messages
self.can_invite_users = can_invite_users
self.can_restrict_members = can_restrict_members
self.can_pin_messages = can_pin_messages
self.can_promote_members = can_promote_members
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
2016-06-07 14:00:44 +03:00
2017-08-06 09:25:25 +03:00
2016-04-14 05:51:05 +03:00
# InlineQuery
2016-01-04 17:24:18 +03:00
class InlineQuery(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
id = obj['id']
2016-01-04 17:53:08 +03:00
from_user = User.de_json(obj['from'])
location = None
if 'location' in obj:
location = Location.de_json(obj['location'])
2016-01-04 17:24:18 +03:00
query = obj['query']
offset = obj['offset']
return cls(id, from_user, location, query, offset)
2016-01-04 17:24:18 +03:00
def __init__(self, id, from_user, location, query, offset):
2016-01-04 17:24:18 +03:00
"""
This object represents an incoming inline query.
When the user sends an empty query, your bot could
return some default or trending results.
:param id: string Unique identifier for this query
:param from_user: User Sender
:param location: Sender location, only for bots that request user location
2016-01-04 17:24:18 +03:00
:param query: String Text of the query
:param offset: String Offset of the results to be returned, can be controlled by the bot
:return: InlineQuery Object
"""
self.id = id
self.from_user = from_user
self.location = location
2016-01-04 17:24:18 +03:00
self.query = query
self.offset = offset
2016-01-04 17:53:08 +03:00
2016-04-14 10:50:55 +03:00
class InputTextMessageContent(Dictionaryable):
def __init__(self, message_text, parse_mode=None, disable_web_page_preview=None):
self.message_text = message_text
self.parse_mode = parse_mode
self.disable_web_page_preview = disable_web_page_preview
def to_dic(self):
json_dic = {'message_text': self.message_text}
if self.parse_mode:
json_dic['parse_mode'] = self.parse_mode
if self.disable_web_page_preview:
json_dic['disable_web_page_preview'] = self.disable_web_page_preview
return json_dic
class InputLocationMessageContent(Dictionaryable):
def __init__(self, latitude, longitude, live_period=None):
2016-04-14 10:50:55 +03:00
self.latitude = latitude
self.longitude = longitude
self.live_period = live_period
2016-04-14 10:50:55 +03:00
def to_dic(self):
2017-04-30 14:40:27 +03:00
json_dic = {'latitude': self.latitude, 'longitude': self.longitude}
if self.live_period:
json_dic['live_period'] = self.live_period
2016-04-14 10:50:55 +03:00
return json_dic
class InputVenueMessageContent(Dictionaryable):
def __init__(self, latitude, longitude, title, address, foursquare_id=None):
self.latitude = latitude
self.longitude = longitude
self.title = title
self.address = address
self.foursquare_id = foursquare_id
def to_dic(self):
json_dic = {'latitude': self.latitude, 'longitude': self.longitude, 'title': self.title,
'address': self.address}
if self.foursquare_id:
json_dic['foursquare_id'] = self.foursquare_id
return json_dic
class InputContactMessageContent(Dictionaryable):
def __init__(self, phone_number, first_name, last_name=None):
self.phone_number = phone_number
self.first_name = first_name
self.last_name = last_name
def to_dic(self):
json_dic = {'phone_numbe': self.phone_number, 'first_name': self.first_name}
if self.last_name:
json_dic['last_name'] = self.last_name
return json_dic
2016-01-04 17:53:08 +03:00
class ChosenInlineResult(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
result_id = obj['result_id']
from_user = User.de_json(obj['from'])
query = obj['query']
2016-04-14 11:57:23 +03:00
location = None
if 'location' in obj:
location = Location.de_json(obj['location'])
inline_message_id = obj.get('inline_message_id')
return cls(result_id, from_user, query, location, inline_message_id)
2016-01-04 17:53:08 +03:00
2016-04-14 11:57:23 +03:00
def __init__(self, result_id, from_user, query, location=None, inline_message_id=None):
2016-01-04 17:53:08 +03:00
"""
This object represents a result of an inline query
that was chosen by the user and sent to their chat partner.
:param result_id: string The unique identifier for the result that was chosen.
:param from_user: User The user that chose the result.
:param query: String The query that was used to obtain the result.
:return: ChosenInlineResult Object.
"""
2016-04-14 11:57:23 +03:00
self.result_id = result_id
self.from_user = from_user
self.query = query
self.location = location
self.inline_message_id = inline_message_id
2016-01-05 04:57:25 +03:00
class InlineQueryResultArticle(JsonSerializable):
2016-04-14 11:57:23 +03:00
def __init__(self, id, title, input_message_content, reply_markup=None, url=None,
2016-01-05 04:57:25 +03:00
hide_url=None, description=None, thumb_url=None, thumb_width=None, thumb_height=None):
2016-01-06 09:31:21 +03:00
"""
Represents a link to an article or web page.
:param id: Unique identifier for this result, 1-64 Bytes.
:param title: Title of the result.
2016-04-14 11:57:23 +03:00
:param input_message_content: InputMessageContent : Content of the message to be sent
:param reply_markup: InlineKeyboardMarkup : Inline keyboard attached to the message
2016-01-06 09:31:21 +03:00
:param url: URL of the result.
:param hide_url: Pass True, if you don't want the URL to be shown in the message.
:param description: Short description of the result.
:param thumb_url: Url of the thumbnail for the result.
:param thumb_width: Thumbnail width.
:param thumb_height: Thumbnail height
:return:
"""
2016-01-05 04:57:25 +03:00
self.type = 'article'
2016-01-05 05:41:32 +03:00
self.id = id
2016-01-05 04:57:25 +03:00
self.title = title
2016-04-14 11:57:23 +03:00
self.input_message_content = input_message_content
self.reply_markup = reply_markup
2016-01-05 04:57:25 +03:00
self.url = url
self.hide_url = hide_url
self.description = description
self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def to_json(self):
2016-04-14 11:57:23 +03:00
json_dict = {'type': self.type, 'id': self.id, 'title': self.title,
'input_message_content': self.input_message_content.to_dic()}
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
2016-01-05 04:57:25 +03:00
if self.url:
json_dict['url'] = self.url
if self.hide_url:
json_dict['hide_url'] = self.hide_url
if self.description:
json_dict['description'] = self.description
if self.thumb_url:
json_dict['thumb_url'] = self.thumb_url
if self.thumb_width:
json_dict['thumb_width'] = self.thumb_width
if self.thumb_height:
json_dict['thumb_height'] = self.thumb_height
return json.dumps(json_dict)
2016-01-05 05:24:21 +03:00
class InlineQueryResultPhoto(JsonSerializable):
2016-01-06 10:53:35 +03:00
def __init__(self, id, photo_url, thumb_url, photo_width=None, photo_height=None, title=None,
2016-04-14 11:57:23 +03:00
description=None, caption=None, reply_markup=None, input_message_content=None):
2016-01-06 10:53:35 +03:00
"""
Represents a link to a photo.
:param id: Unique identifier for this result, 1-64 bytes
:param photo_url: A valid URL of the photo. Photo must be in jpeg format. Photo size must not exceed 5MB
:param thumb_url: URL of the thumbnail for the photo
:param photo_width: Width of the photo.
:param photo_height: Height of the photo.
:param title: Title for the result.
:param description: Short description of the result.
:param caption: Caption of the photo to be sent, 0-200 characters.
2016-04-14 11:57:23 +03:00
:param reply_markup: InlineKeyboardMarkup : Inline keyboard attached to the message
:param input_message_content: InputMessageContent : Content of the message to be sent instead of the photo
2016-01-06 10:53:35 +03:00
:return:
"""
2016-01-05 05:24:21 +03:00
self.type = 'photo'
self.id = id
self.photo_url = photo_url
self.photo_width = photo_width
self.photo_height = photo_height
self.thumb_url = thumb_url
self.title = title
self.description = description
self.caption = caption
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
2016-01-05 05:24:21 +03:00
def to_json(self):
2016-01-05 17:23:00 +03:00
json_dict = {'type': self.type, 'id': self.id, 'photo_url': self.photo_url, 'thumb_url': self.thumb_url}
2016-01-05 05:24:21 +03:00
if self.photo_width:
json_dict['photo_width'] = self.photo_width
if self.photo_height:
json_dict['photo_height'] = self.photo_height
if self.title:
json_dict['title'] = self.title
if self.description:
json_dict['description'] = self.description
if self.caption:
json_dict['caption'] = self.caption
2016-04-14 11:57:23 +03:00
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
2016-01-05 05:24:21 +03:00
return json.dumps(json_dict)
2016-01-05 05:41:32 +03:00
class InlineQueryResultGif(JsonSerializable):
2016-01-05 17:23:00 +03:00
def __init__(self, id, gif_url, thumb_url, gif_width=None, gif_height=None, title=None, caption=None,
reply_markup=None, input_message_content=None, gif_duration=None):
2016-01-06 10:53:35 +03:00
"""
Represents a link to an animated GIF file.
:param id: Unique identifier for this result, 1-64 bytes.
:param gif_url: A valid URL for the GIF file. File size must not exceed 1MB
:param thumb_url: URL of the static thumbnail (jpeg or gif) for the result.
:param gif_width: Width of the GIF.
:param gif_height: Height of the GIF.
:param title: Title for the result.
:param caption: Caption of the GIF file to be sent, 0-200 characters
2016-04-14 11:57:23 +03:00
:param reply_markup: InlineKeyboardMarkup : Inline keyboard attached to the message
:param input_message_content: InputMessageContent : Content of the message to be sent instead of the photo
2016-01-06 10:53:35 +03:00
:return:
"""
2016-01-05 05:41:32 +03:00
self.type = 'gif'
self.id = id
self.gif_url = gif_url
self.gif_width = gif_width
self.gif_height = gif_height
self.thumb_url = thumb_url
self.title = title
self.caption = caption
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.gif_duration = gif_duration
2016-01-05 05:41:32 +03:00
def to_json(self):
2016-01-05 17:23:00 +03:00
json_dict = {'type': self.type, 'id': self.id, 'gif_url': self.gif_url, 'thumb_url': self.thumb_url}
2016-01-05 05:41:32 +03:00
if self.gif_height:
json_dict['gif_height'] = self.gif_height
if self.gif_width:
json_dict['gif_width'] = self.gif_width
if self.title:
json_dict['title'] = self.title
if self.caption:
json_dict['caption'] = self.caption
2016-04-14 11:57:23 +03:00
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
if self.gif_duration:
json_dict['gif_duration'] = self.gif_duration
2016-01-05 05:41:32 +03:00
return json.dumps(json_dict)
2016-01-05 05:51:33 +03:00
class InlineQueryResultMpeg4Gif(JsonSerializable):
2016-01-05 17:23:00 +03:00
def __init__(self, id, mpeg4_url, thumb_url, mpeg4_width=None, mpeg4_height=None, title=None, caption=None,
reply_markup=None, input_message_content=None, mpeg4_duration=None):
2016-01-06 11:59:42 +03:00
"""
Represents a link to a video animation (H.264/MPEG-4 AVC video without sound).
:param id: Unique identifier for this result, 1-64 bytes
:param mpeg4_url: A valid URL for the MP4 file. File size must not exceed 1MB
:param thumb_url: URL of the static thumbnail (jpeg or gif) for the result
:param mpeg4_width: Video width
:param mpeg4_height: Video height
:param title: Title for the result
:param caption: Caption of the MPEG-4 file to be sent, 0-200 characters
2016-04-14 11:57:23 +03:00
:param reply_markup: InlineKeyboardMarkup : Inline keyboard attached to the message
:param input_message_content: InputMessageContent : Content of the message to be sent instead of the photo
2016-01-06 11:59:42 +03:00
:return:
"""
2016-01-05 05:51:33 +03:00
self.type = 'mpeg4_gif'
self.id = id
self.mpeg4_url = mpeg4_url
self.mpeg4_width = mpeg4_width
self.mpeg4_height = mpeg4_height
self.thumb_url = thumb_url
self.title = title
self.caption = caption
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.mpeg4_duration = mpeg4_duration
2016-01-05 05:51:33 +03:00
def to_json(self):
2016-01-05 17:23:00 +03:00
json_dict = {'type': self.type, 'id': self.id, 'mpeg4_url': self.mpeg4_url, 'thumb_url': self.thumb_url}
2016-01-05 05:51:33 +03:00
if self.mpeg4_width:
json_dict['mpeg4_width'] = self.mpeg4_width
if self.mpeg4_height:
json_dict['mpeg4_height'] = self.mpeg4_height
if self.title:
json_dict['title'] = self.title
if self.caption:
json_dict['caption'] = self.caption
2016-04-14 11:57:23 +03:00
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
if self.mpeg4_duration:
json_dict['mpeg4_duration '] = self.mpeg4_duration
2016-01-05 05:51:33 +03:00
return json.dumps(json_dict)
2016-01-05 06:03:05 +03:00
class InlineQueryResultVideo(JsonSerializable):
2016-04-14 11:57:23 +03:00
def __init__(self, id, video_url, mime_type, thumb_url, title,
caption=None, video_width=None, video_height=None, video_duration=None, description=None,
reply_markup=None, input_message_content=None):
2016-01-06 11:59:42 +03:00
"""
Represents link to a page containing an embedded video player or a video file.
:param id: Unique identifier for this result, 1-64 bytes
:param video_url: A valid URL for the embedded video player or video file
:param mime_type: Mime type of the content of video url, text/html or video/mp4
:param thumb_url: URL of the thumbnail (jpeg only) for the video
:param title: Title for the result
:param video_width: Video width
:param video_height: Video height
:param video_duration: Video duration in seconds
:param description: Short description of the result
:return:
"""
2016-01-05 06:03:05 +03:00
self.type = 'video'
self.id = id
self.video_url = video_url
self.mime_type = mime_type
self.video_width = video_width
self.video_height = video_height
self.video_duration = video_duration
self.thumb_url = thumb_url
self.title = title
2016-04-14 11:57:23 +03:00
self.caption = caption
2016-01-05 06:03:05 +03:00
self.description = description
2016-04-14 11:57:23 +03:00
self.input_message_content = input_message_content
self.reply_markup = reply_markup
2016-01-05 06:03:05 +03:00
def to_json(self):
2016-01-05 17:23:00 +03:00
json_dict = {'type': self.type, 'id': self.id, 'video_url': self.video_url, 'mime_type': self.mime_type,
2016-04-14 11:57:23 +03:00
'thumb_url': self.thumb_url, 'title': self.title}
2016-01-05 06:03:05 +03:00
if self.video_width:
json_dict['video_width'] = self.video_width
if self.video_height:
json_dict['video_height'] = self.video_height
if self.video_duration:
json_dict['video_duration'] = self.video_duration
if self.description:
json_dict['description'] = self.description
2016-04-14 11:57:23 +03:00
if self.caption:
json_dict['caption'] = self.caption
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultAudio(JsonSerializable):
def __init__(self, id, audio_url, title, caption=None, performer=None, audio_duration=None, reply_markup=None,
2016-04-14 11:57:23 +03:00
input_message_content=None):
self.type = 'audio'
self.id = id
self.audio_url = audio_url
self.title = title
self.caption = caption
2016-04-14 11:57:23 +03:00
self.performer = performer
self.audio_duration = audio_duration
self.reply_markup = reply_markup
self.input_message_content = input_message_content
def to_json(self):
2016-04-15 21:04:08 +03:00
json_dict = {'type': self.type, 'id': self.id, 'audio_url': self.audio_url, 'title': self.title}
if self.caption:
json_dict['caption'] = self.caption
2016-04-14 11:57:23 +03:00
if self.performer:
json_dict['performer'] = self.performer
if self.audio_duration:
json_dict['audio_duration'] = self.audio_duration
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultVoice(JsonSerializable):
def __init__(self, id, voice_url, title, caption=None, performer=None, voice_duration=None, reply_markup=None,
2016-04-14 11:57:23 +03:00
input_message_content=None):
self.type = 'voice'
self.id = id
self.voice_url = voice_url
self.title = title
self.caption = caption
2016-04-14 11:57:23 +03:00
self.performer = performer
self.voice_duration = voice_duration
self.reply_markup = reply_markup
self.input_message_content = input_message_content
def to_json(self):
2016-06-17 12:50:06 +03:00
json_dict = {'type': self.type, 'id': self.id, 'voice_url': self.voice_url, 'title': self.title}
if self.caption:
json_dict['caption'] = self.caption
2016-04-14 11:57:23 +03:00
if self.performer:
json_dict['performer'] = self.performer
if self.voice_duration:
json_dict['voice_duration'] = self.voice_duration
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultDocument(JsonSerializable):
def __init__(self, id, title, document_url, mime_type, caption=None, description=None, reply_markup=None,
input_message_content=None, thumb_url=None, thumb_width=None, thumb_height=None):
self.type = 'document'
self.id = id
self.title = title
self.document_url = document_url
self.mime_type = mime_type
self.caption = caption
self.description = description
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def to_json(self):
2016-06-17 12:50:06 +03:00
json_dict = {'type': self.type, 'id': self.id, 'title': self.title, 'document_url': self.document_url,
'mime_type': self.mime_type}
2016-04-14 11:57:23 +03:00
if self.caption:
json_dict['caption'] = self.caption
if self.description:
json_dict['description'] = self.description
if self.thumb_url:
json_dict['thumb_url'] = self.thumb_url
if self.thumb_width:
json_dict['thumb_width'] = self.thumb_width
if self.thumb_height:
json_dict['thumb_height'] = self.thumb_height
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultLocation(JsonSerializable):
def __init__(self, id, title, latitude, longitude, live_period=None, reply_markup=None,
2016-04-14 11:57:23 +03:00
input_message_content=None, thumb_url=None, thumb_width=None, thumb_height=None):
self.type = 'location'
self.id = id
self.title = title
self.latitude = latitude
self.longitude = longitude
self.live_period = live_period
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def to_json(self):
json_dict = {'type': self.type, 'id': self.id, 'latitude': self.latitude, 'longitude': self.longitude,
'title': self.title}
if self.live_period:
json_dict['live_period'] = self.live_period
2016-04-14 11:57:23 +03:00
if self.thumb_url:
json_dict['thumb_url'] = self.thumb_url
if self.thumb_width:
json_dict['thumb_width'] = self.thumb_width
if self.thumb_height:
json_dict['thumb_height'] = self.thumb_height
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultVenue(JsonSerializable):
def __init__(self, id, title, latitude, longitude, address, foursquare_id=None, reply_markup=None,
input_message_content=None, thumb_url=None, thumb_width=None, thumb_height=None):
self.type = 'venue'
self.id = id
self.title = title
self.latitude = latitude
self.longitude = longitude
self.address = address
self.foursquare_id = foursquare_id
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def to_json(self):
2016-05-20 09:41:00 +03:00
json_dict = {'type': self.type, 'id': self.id, 'title': self.title, 'latitude': self.latitude,
'longitude': self.longitude, 'address': self.address}
2016-04-14 11:57:23 +03:00
if self.foursquare_id:
json_dict['foursquare_id'] = self.foursquare_id
if self.thumb_url:
json_dict['thumb_url'] = self.thumb_url
if self.thumb_width:
json_dict['thumb_width'] = self.thumb_width
if self.thumb_height:
json_dict['thumb_height'] = self.thumb_height
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class InlineQueryResultContact(JsonSerializable):
def __init__(self, id, phone_number, first_name, last_name=None, reply_markup=None,
input_message_content=None, thumb_url=None, thumb_width=None, thumb_height=None):
self.type = 'contact'
self.id = id
self.phone_number = phone_number
self.first_name = first_name
self.last_name = last_name
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def to_json(self):
json_dict = {'type': self.type, 'id': self.id, 'phone_number': self.phone_number, 'first_name': self.first_name}
if self.last_name:
json_dict['last_name'] = self.last_name
if self.thumb_url:
json_dict['thumb_url'] = self.thumb_url
if self.thumb_width:
json_dict['thumb_width'] = self.thumb_width
if self.thumb_height:
json_dict['thumb_height'] = self.thumb_height
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
return json.dumps(json_dict)
class BaseInlineQueryResultCached(JsonSerializable):
def __init__(self):
self.type = None
self.id = None
self.title = None
self.description = None
self.caption = None
self.reply_markup = None
self.input_message_content = None
self.payload_dic = {}
def to_json(self):
json_dict = self.payload_dic
json_dict['type'] = self.type
json_dict['id'] = self.id
if self.title:
json_dict['title'] = self.title
if self.description:
json_dict['description'] = self.description
if self.caption:
json_dict['caption'] = self.caption
if self.reply_markup:
json_dict['reply_markup'] = self.reply_markup.to_dic()
if self.input_message_content:
json_dict['input_message_content'] = self.input_message_content.to_dic()
2016-01-05 06:03:05 +03:00
return json.dumps(json_dict)
2016-04-14 11:57:23 +03:00
class InlineQueryResultCachedPhoto(BaseInlineQueryResultCached):
def __init__(self, id, photo_file_id, title=None, description=None, caption=None, reply_markup=None,
input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'photo'
self.id = id
self.photo_file_id = photo_file_id
self.title = title
self.description = description
self.caption = caption
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['photo_file_id'] = photo_file_id
class InlineQueryResultCachedGif(BaseInlineQueryResultCached):
def __init__(self, id, gif_file_id, title=None, description=None, caption=None, reply_markup=None,
input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'gif'
self.id = id
self.gif_file_id = gif_file_id
self.title = title
self.description = description
self.caption = caption
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['gif_file_id'] = gif_file_id
2016-04-14 12:09:12 +03:00
class InlineQueryResultCachedMpeg4Gif(BaseInlineQueryResultCached):
def __init__(self, id, mpeg4_file_id, title=None, description=None, caption=None, reply_markup=None,
input_message_content=None):
BaseInlineQueryResultCached.__init__(self)
self.type = 'mpeg4_gif'
self.id = id
self.mpeg4_file_id = mpeg4_file_id
self.title = title
self.description = description
self.caption = caption
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['mpeg4_file_id'] = mpeg4_file_id
2016-04-25 18:54:30 +03:00
2016-04-14 11:57:23 +03:00
class InlineQueryResultCachedSticker(BaseInlineQueryResultCached):
def __init__(self, id, sticker_file_id, reply_markup=None, input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'sticker'
self.id = id
self.sticker_file_id = sticker_file_id
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['sticker_file_id'] = sticker_file_id
class InlineQueryResultCachedDocument(BaseInlineQueryResultCached):
def __init__(self, id, document_file_id, title, description=None, caption=None, reply_markup=None,
input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'document'
self.id = id
self.document_file_id = document_file_id
self.title = title
self.description = description
self.caption = caption
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['document_file_id'] = document_file_id
class InlineQueryResultCachedVideo(BaseInlineQueryResultCached):
def __init__(self, id, video_file_id, title, description=None, caption=None, reply_markup=None,
input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'video'
self.id = id
self.video_file_id = video_file_id
self.title = title
self.description = description
self.caption = caption
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['video_file_id'] = video_file_id
class InlineQueryResultCachedVoice(BaseInlineQueryResultCached):
def __init__(self, id, voice_file_id, title, caption=None, reply_markup=None, input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'voice'
self.id = id
self.voice_file_id = voice_file_id
self.title = title
self.caption = caption
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
2016-04-30 19:24:54 +03:00
self.payload_dic['voice_file_id'] = voice_file_id
2016-04-14 11:57:23 +03:00
class InlineQueryResultCachedAudio(BaseInlineQueryResultCached):
def __init__(self, id, audio_file_id, caption=None, reply_markup=None, input_message_content=None):
2016-04-14 12:09:12 +03:00
BaseInlineQueryResultCached.__init__(self)
2016-04-14 11:57:23 +03:00
self.type = 'audio'
self.id = id
self.audio_file_id = audio_file_id
self.caption = caption
2016-04-14 11:57:23 +03:00
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.payload_dic['audio_file_id'] = audio_file_id
# Games
class InlineQueryResultGame(JsonSerializable):
2016-10-31 20:14:28 +03:00
def __init__(self, id, game_short_name, reply_markup=None):
self.type = 'game'
self.id = id
self.game_short_name = game_short_name
self.reply_markup = reply_markup
def to_json(self):
json_dic = {'type': self.type, 'id': self.id, 'game_short_name': self.game_short_name}
if self.reply_markup:
json_dic['reply_markup'] = self.reply_markup.to_dic()
return json.dumps(json_dic)
class Game(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
title = obj['title']
description = obj['description']
photo = Game.parse_photo(obj['photo'])
text = obj.get('text')
text_entities = None
if 'text_entities' in obj:
text_entities = Game.parse_entities(obj['text_entities'])
animation = None
if 'animation' in obj:
animation = Animation.de_json(obj['animation'])
return cls(title, description, photo, text, text_entities, animation)
@classmethod
def parse_photo(cls, photo_size_array):
ret = []
for ps in photo_size_array:
ret.append(PhotoSize.de_json(ps))
return ret
@classmethod
def parse_entities(cls, message_entity_array):
ret = []
for me in message_entity_array:
ret.append(MessageEntity.de_json(me))
return ret
def __init__(self, title, description, photo, text=None, text_entities=None, animation=None):
self.title = title
self.description = description
self.photo = photo
self.text = text
self.text_entities = text_entities
self.animation = animation
class Animation(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
thumb = None
if 'thumb' in obj:
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)
def __init__(self, file_id, thumb=None, 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
class GameHighScore(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
position = obj['position']
user = User.de_json(obj['user'])
score = obj['score']
return cls(position, user, score)
def __init__(self, position, user, score):
self.position = position
self.user = user
self.score = score
2017-05-21 16:45:12 +03:00
# Payments
class LabeledPrice(JsonSerializable):
def __init__(self, label, amount):
self.label = label
self.amount = amount
def to_json(self):
return json.dumps(self.to_dic())
def to_dic(self):
return {'label': self.label, 'amount': self.amount}
class Invoice(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
title = obj['title']
description = obj['description']
start_parameter = obj['start_parameter']
currency = obj['currency']
total_amount = obj['total_amount']
return cls(title, description, start_parameter, currency, total_amount)
def __init__(self, title, description, start_parameter, currency, total_amount):
self.title = title
self.description = description
self.start_parameter = start_parameter
self.currency = currency
self.total_amount = total_amount
class ShippingAddress(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
country_code = obj['country_code']
state = obj['state']
city = obj['city']
street_line1 = obj['street_line1']
street_line2 = obj['street_line2']
post_code = obj['post_code']
return cls(country_code, state, city, street_line1, street_line2, post_code)
def __init__(self, country_code, state, city, street_line1, street_line2, post_code):
self.country_code = country_code
self.state = state
self.city = city
self.street_line1 = street_line1
self.street_line2 = street_line2
self.post_code = post_code
class OrderInfo(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
name = obj.get('name')
phone_number = obj.get('phone_number')
email = obj.get('email')
shipping_address = None
if 'shipping_address' in obj:
shipping_address = ShippingAddress.de_json(obj['shipping_address'])
return cls(name, phone_number, email, shipping_address)
def __init__(self, name, phone_number, email, shipping_address):
self.name = name
self.phone_number = phone_number
self.email = email
self.shipping_address = shipping_address
class ShippingOption(JsonSerializable):
def __init__(self, id, title):
self.id = id
self.title = title
self.prices = []
def add_price(self, *args):
"""
Add LabeledPrice to ShippingOption
:param args: LabeledPrices
"""
for price in args:
self.prices.append(price)
def to_json(self):
price_list = []
for p in self.prices:
price_list.append(p.to_dic())
2017-11-13 05:25:39 +03:00
json_dict = json.dumps({'id': self.id, 'title': self.title, 'prices': price_list})
2017-05-21 16:45:12 +03:00
return json_dict
class SuccessfulPayment(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
currency = obj['currency']
total_amount = obj['total_amount']
invoice_payload = obj['invoice_payload']
shipping_option_id = obj.get('shipping_option_id')
order_info = None
if 'order_info' in obj:
order_info = OrderInfo.de_json(obj['order_info'])
telegram_payment_charge_id = obj['telegram_payment_charge_id']
provider_payment_charge_id = obj['provider_payment_charge_id']
return cls(currency, total_amount, invoice_payload, shipping_option_id, order_info,
telegram_payment_charge_id, provider_payment_charge_id)
def __init__(self, currency, total_amount, invoice_payload, shipping_option_id, order_info,
telegram_payment_charge_id, provider_payment_charge_id):
self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.shipping_option_id = shipping_option_id
self.order_info = order_info
self.telegram_payment_charge_id = telegram_payment_charge_id
self.provider_payment_charge_id = provider_payment_charge_id
class ShippingQuery(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
id = obj['id']
from_user = User.de_json(obj['from'])
invoice_payload = obj['invoice_payload']
shipping_address = ShippingAddress.de_json(obj['shipping_address'])
return cls(id, from_user, invoice_payload, shipping_address)
def __init__(self, id, from_user, invoice_payload, shipping_address):
self.id = id
self.from_user = from_user
self.invoice_payload = invoice_payload
self.shipping_address = shipping_address
class PreCheckoutQuery(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
id = obj['id']
from_user = User.de_json(obj['from'])
currency = obj['currency']
total_amount = obj['total_amount']
invoice_payload = obj['invoice_payload']
shipping_option_id = obj.get('shipping_option_id')
order_info = None
if 'order_info' in obj:
order_info = OrderInfo.de_json(obj['order_info'])
return cls(id, from_user, currency, total_amount, invoice_payload, shipping_option_id, order_info)
def __init__(self, id, from_user, currency, total_amount, invoice_payload, shipping_option_id, order_info):
self.id = id
self.from_user = from_user
self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.shipping_option_id = shipping_option_id
self.order_info = order_info
2017-08-06 07:00:26 +03:00
2017-08-06 09:25:25 +03:00
2017-08-06 07:00:26 +03:00
# Stickers
class StickerSet(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
name = obj['name']
title = obj['title']
contains_masks = obj['contains_masks']
2017-08-06 09:25:25 +03:00
stickers = []
for s in obj['stickers']:
2017-08-06 07:00:26 +03:00
stickers.append(Sticker.de_json(s))
return cls(name, title, contains_masks, stickers)
2017-08-06 09:25:25 +03:00
def __init__(self, name, title, contains_masks, stickers):
2017-08-06 07:00:26 +03:00
self.stickers = stickers
self.contains_masks = contains_masks
self.title = title
self.name = name
class Sticker(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
file_id = obj['file_id']
width = obj['width']
height = obj['height']
thumb = None
if 'thumb' in obj:
thumb = PhotoSize.de_json(obj['thumb'])
emoji = obj.get('emoji')
set_name = obj.get('set_name')
mask_position = None
if 'mask_position' in obj:
mask_position = MaskPosition.de_json(obj['mask_position'])
file_size = obj.get('file_size')
2017-08-06 09:25:25 +03:00
return cls(file_id, width, height, thumb, emoji, set_name, mask_position, file_size)
2017-08-06 07:00:26 +03:00
2017-08-06 09:25:25 +03:00
def __init__(self, file_id, width, height, thumb, emoji, set_name, mask_position, file_size):
2017-08-06 07:00:26 +03:00
self.file_id = file_id
self.width = width
self.height = height
2017-08-06 09:25:25 +03:00
self.thumb = thumb
2017-08-06 07:00:26 +03:00
self.emoji = emoji
self.set_name = set_name
self.mask_position = mask_position
2017-08-06 09:25:25 +03:00
self.file_size = file_size
2017-08-06 07:00:26 +03:00
2017-08-06 09:25:25 +03:00
class MaskPosition(JsonDeserializable, JsonSerializable):
2017-08-06 07:00:26 +03:00
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
point = obj['point']
x_shift = obj['x_shift']
y_shift = obj['y_shift']
scale = obj['scale']
return cls(point, x_shift, y_shift, scale)
def __init__(self, point, x_shift, y_shift, scale):
self.point = point
self.x_shift = x_shift
self.y_shift = y_shift
self.scale = scale
2017-08-06 09:25:25 +03:00
def to_json(self):
return json.dumps(self.to_dic())
def to_dic(self):
return {'point': self.point, 'x_shift': self.x_shift, 'y_shift': self.y_shift, 'scale': self.scale}
2017-11-29 08:45:25 +03:00
# InputMedia
class InputMediaPhoto(JsonSerializable):
def __init__(self, media, caption=None):
self.type = "photo"
self.media = media
self.caption = caption
def to_json(self):
return json.dumps(self.to_dic())
def to_dic(self):
ret = {'type': self.type, 'media': self.media}
if self.caption:
ret['caption'] = self.caption
return ret
class InputMediaVideo(JsonSerializable):
def __init__(self, media, caption=None, width=None, height=None, duration=None):
self.type = "video"
self.media = media
self.caption = caption
self.width = width
self.height = height
self.duration = duration
def to_json(self):
return json.dumps(self.to_dic())
def to_dic(self):
ret = {'type': self.type, 'media': self.media}
if self.caption:
ret['caption'] = self.caption
if self.width:
ret['width'] = self.width
if self.height:
ret['height'] = self.height
if self.duration:
ret['duration'] = self.duration
return ret