python-telegram-bot/telegram/message.py

147 lines
4.6 KiB
Python
Raw Normal View History

2015-07-07 21:50:36 +02:00
#!/usr/bin/env python
class Message(object):
def __init__(self, **kwargs):
param_defaults = {
'message_id': None,
'user': None,
'date': None,
'chat': None,
'forward_from': None,
'forward_date': None,
'reply_to_message': None,
'text': None,
'audio': None,
'document': None,
'photo': None,
'sticker': None,
'video': None,
'contact': None,
'location': None,
'new_chat_participant': None,
'left_chat_participant': None,
'new_chat_title': None,
'new_chat_photo': None,
'delete_chat_photo': None,
'group_chat_created': None
}
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@property
def chat_id(self):
return self.chat.id
2015-07-07 21:50:36 +02:00
@staticmethod
def de_json(data):
2015-07-07 21:50:36 +02:00
if 'from' in data: # from on api
from telegram import User
user = User.de_json(data['from'])
2015-07-07 21:50:36 +02:00
else:
user = None
if 'chat' in data:
if 'username' in data['chat']:
from telegram import User
chat = User.de_json(data['chat'])
2015-07-07 21:50:36 +02:00
if 'title' in data['chat']:
from telegram import GroupChat
chat = GroupChat.de_json(data['chat'])
2015-07-07 21:50:36 +02:00
else:
chat = None
if 'forward_from' in data:
from telegram import User
forward_from = User.de_json(data['forward_from'])
2015-07-07 21:50:36 +02:00
else:
forward_from = None
if 'reply_to_message' in data:
reply_to_message = Message.de_json(
2015-07-07 21:50:36 +02:00
data['reply_to_message']
)
else:
reply_to_message = None
2015-07-08 01:52:57 +02:00
if 'audio' in data:
from telegram import Audio
audio = Audio.de_json(data['audio'])
2015-07-08 01:52:57 +02:00
else:
audio = None
2015-07-08 02:12:51 +02:00
if 'document' in data:
from telegram import Document
document = Document.de_json(data['document'])
2015-07-08 02:12:51 +02:00
else:
document = None
2015-07-08 01:52:57 +02:00
if 'photo' in data:
from telegram import PhotoSize
photo = [PhotoSize.de_json(x) for x in data['photo']]
2015-07-08 01:52:57 +02:00
else:
photo = None
2015-07-08 04:52:12 +02:00
if 'sticker' in data:
from telegram import Sticker
sticker = Sticker.de_json(data['sticker'])
2015-07-08 04:52:12 +02:00
else:
sticker = None
2015-07-08 14:17:18 +02:00
if 'video' in data:
from telegram import Video
video = Video.de_json(data['video'])
2015-07-08 14:17:18 +02:00
else:
video = None
2015-07-08 15:20:43 +02:00
if 'contact' in data:
from telegram import Contact
contact = Contact.de_json(data['contact'])
2015-07-08 15:20:43 +02:00
else:
contact = None
if 'location' in data:
from telegram import Location
location = Location.de_json(data['location'])
else:
location = None
2015-07-07 21:50:36 +02:00
if 'new_chat_participant' in data:
from telegram import User
new_chat_participant = User.de_json(
2015-07-07 21:50:36 +02:00
data['new_chat_participant']
)
else:
new_chat_participant = None
if 'left_chat_participant' in data:
from telegram import User
left_chat_participant = User.de_json(
2015-07-07 21:50:36 +02:00
data['left_chat_participant']
)
else:
left_chat_participant = None
return Message(message_id=data.get('message_id', None),
user=user,
date=data.get('date', None),
chat=chat,
forward_from=forward_from,
forward_date=data.get('forward_date', None),
reply_to_message=reply_to_message,
text=data.get('text', None),
2015-07-08 01:52:57 +02:00
audio=audio,
2015-07-08 02:12:51 +02:00
document=document,
2015-07-08 01:52:57 +02:00
photo=photo,
2015-07-08 04:52:12 +02:00
sticker=sticker,
2015-07-08 14:17:18 +02:00
video=video,
2015-07-08 15:20:43 +02:00
contact=contact,
location=location,
2015-07-07 21:50:36 +02:00
new_chat_participant=new_chat_participant,
left_chat_participant=left_chat_participant,
new_chat_title=data.get('new_chat_title', None),
new_chat_photo=data.get('new_chat_photo', None),
delete_chat_photo=data.get('delete_chat_photo', None),
group_chat_created=data.get('group_chat_created', None))