python-telegram-bot/telegram/message.py

222 lines
7.8 KiB
Python
Raw Normal View History

2015-07-07 21:50:36 +02:00
#!/usr/bin/env python
2015-08-11 21:58:17 +02:00
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
2015-08-10 18:57:31 +02:00
2015-07-07 21:50:36 +02:00
2015-07-20 12:53:58 +02:00
from telegram import TelegramObject
2015-07-09 16:40:44 +02:00
class Message(TelegramObject):
2015-07-09 02:58:13 +02:00
def __init__(self,
message_id,
from_user,
date,
chat,
forward_from=None,
forward_date=None,
reply_to_message=None,
text=None,
audio=None,
document=None,
photo=None,
sticker=None,
video=None,
2015-08-11 22:32:06 +02:00
caption=None,
2015-07-09 02:58:13 +02:00
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):
self.message_id = message_id
self.from_user = from_user
self.date = date
self.chat = chat
self.forward_from = forward_from
self.forward_date = forward_date
self.reply_to_message = reply_to_message
self.text = text
self.audio = audio
self.document = document
self.photo = photo
self.sticker = sticker
self.video = video
2015-08-11 22:32:06 +02:00
self.caption = caption
2015-07-09 02:58:13 +02:00
self.contact = contact
self.location = location
self.new_chat_participant = new_chat_participant
self.left_chat_participant = left_chat_participant
self.new_chat_title = new_chat_title
self.new_chat_photo = new_chat_photo
self.delete_chat_photo = delete_chat_photo
self.group_chat_created = group_chat_created
@property
def chat_id(self):
return self.chat.id
2015-07-07 21:50:36 +02:00
@staticmethod
def de_json(data):
2015-07-09 16:40:44 +02:00
if 'from' in data: # from is a reserved word, use from_user instead.
2015-07-07 21:50:36 +02:00
from telegram import User
2015-07-09 02:58:13 +02:00
from_user = User.de_json(data['from'])
2015-07-07 21:50:36 +02:00
else:
2015-07-09 02:58:13 +02:00
from_user = None
2015-07-07 21:50:36 +02:00
if 'chat' in data:
if 'first_name' in data['chat']:
2015-07-07 21:50:36 +02:00
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:
2015-07-09 02:58:13 +02:00
reply_to_message = Message.de_json(data['reply_to_message'])
2015-07-07 21:50:36 +02:00
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
2015-07-09 02:58:13 +02:00
new_chat_participant = User.de_json(data['new_chat_participant'])
2015-07-07 21:50:36 +02:00
else:
new_chat_participant = None
if 'left_chat_participant' in data:
from telegram import User
2015-07-09 02:58:13 +02:00
left_chat_participant = User.de_json(data['left_chat_participant'])
2015-07-07 21:50:36 +02:00
else:
left_chat_participant = None
return Message(message_id=data.get('message_id', None),
2015-07-09 02:58:13 +02:00
from_user=from_user,
2015-07-07 21:50:36 +02:00
date=data.get('date', None),
chat=chat,
forward_from=forward_from,
forward_date=data.get('forward_date', None),
reply_to_message=reply_to_message,
2015-08-11 22:32:06 +02:00
text=data.get('text', ''),
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-08-11 22:32:06 +02:00
caption=data.get('caption', ''),
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))
2015-07-09 16:40:44 +02:00
def to_dict(self):
data = {'message_id': self.message_id,
'from': self.from_user.to_dict(),
'date': self.date,
'chat': self.chat.to_dict()}
2015-07-09 16:40:44 +02:00
if self.forward_from:
data['forward_from'] = self.forward_from
2015-07-09 16:40:44 +02:00
if self.forward_date:
data['forward_date'] = self.forward_date
2015-07-09 16:40:44 +02:00
if self.reply_to_message:
data['reply_to_message'] = self.reply_to_message
2015-07-09 16:40:44 +02:00
if self.text:
data['text'] = self.text
2015-07-09 16:40:44 +02:00
if self.audio:
data['audio'] = self.audio.to_dict()
2015-07-09 16:40:44 +02:00
if self.document:
data['document'] = self.document.to_dict()
2015-07-09 16:40:44 +02:00
if self.photo:
data['photo'] = [p.to_dict() for p in self.photo]
2015-07-09 16:40:44 +02:00
if self.sticker:
data['sticker'] = self.sticker.to_dict()
2015-07-09 16:40:44 +02:00
if self.video:
data['video'] = self.video.to_dict()
2015-08-11 22:32:06 +02:00
if self.caption:
data['caption'] = self.caption
2015-07-09 16:40:44 +02:00
if self.contact:
data['contact'] = self.contact.to_dict()
2015-07-09 16:40:44 +02:00
if self.location:
data['location'] = self.location.to_dict()
2015-07-09 16:40:44 +02:00
if self.new_chat_participant:
data['new_chat_participant'] = self.new_chat_participant
2015-07-09 16:40:44 +02:00
if self.left_chat_participant:
data['left_chat_participant'] = self.left_chat_participant
2015-07-09 16:40:44 +02:00
if self.new_chat_title:
data['new_chat_title'] = self.new_chat_title
2015-07-09 16:40:44 +02:00
if self.new_chat_photo:
data['new_chat_photo'] = self.new_chat_photo
2015-07-09 16:40:44 +02:00
if self.delete_chat_photo:
data['delete_chat_photo'] = self.delete_chat_photo
2015-07-09 16:40:44 +02:00
if self.group_chat_created:
data['group_chat_created'] = self.group_chat_created
return data