2015-07-07 21:50:36 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-22 04:15:29 +02:00
|
|
|
# pylint: disable=R0902,R0912,R0913
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2016-01-05 14:12:03 +01:00
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# 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/].
|
2016-01-13 17:09:35 +01:00
|
|
|
"""This module contains a object that represents a Telegram Message."""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-08-14 20:47:31 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from time import mktime
|
2015-07-09 16:40:44 +02:00
|
|
|
|
2016-05-15 03:46:40 +02:00
|
|
|
from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
|
|
|
|
User, Video, Voice, Venue, MessageEntity)
|
2015-08-21 19:49:07 +02:00
|
|
|
|
2015-07-09 16:40:44 +02:00
|
|
|
|
2015-07-20 04:06:04 +02:00
|
|
|
class Message(TelegramObject):
|
2015-08-21 19:49:07 +02:00
|
|
|
"""This object represents a Telegram Message.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
* In Python `from` is a reserved word, use `from_user` instead.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
message_id (int):
|
|
|
|
from_user (:class:`telegram.User`):
|
|
|
|
date (:class:`datetime.datetime`):
|
|
|
|
forward_from (:class:`telegram.User`):
|
2016-05-11 23:22:05 +02:00
|
|
|
forward_from_chat (:class:`telegram.Chat`):
|
2015-08-21 19:49:07 +02:00
|
|
|
forward_date (:class:`datetime.datetime`):
|
|
|
|
reply_to_message (:class:`telegram.Message`):
|
2016-05-24 01:28:36 +02:00
|
|
|
edit_date (:class:`datetime.datetime`):
|
2015-08-21 19:49:07 +02:00
|
|
|
text (str):
|
|
|
|
audio (:class:`telegram.Audio`):
|
|
|
|
document (:class:`telegram.Document`):
|
|
|
|
photo (List[:class:`telegram.PhotoSize`]):
|
|
|
|
sticker (:class:`telegram.Sticker`):
|
|
|
|
video (:class:`telegram.Video`):
|
|
|
|
voice (:class:`telegram.Voice`):
|
|
|
|
caption (str):
|
|
|
|
contact (:class:`telegram.Contact`):
|
|
|
|
location (:class:`telegram.Location`):
|
2016-04-12 05:33:42 +02:00
|
|
|
new_chat_member (:class:`telegram.User`):
|
|
|
|
left_chat_member (:class:`telegram.User`):
|
2015-08-21 19:49:07 +02:00
|
|
|
new_chat_title (str):
|
|
|
|
new_chat_photo (List[:class:`telegram.PhotoSize`]):
|
|
|
|
delete_chat_photo (bool):
|
|
|
|
group_chat_created (bool):
|
2016-03-14 09:54:25 +01:00
|
|
|
supergroup_chat_created (bool):
|
|
|
|
migrate_to_chat_id (int):
|
|
|
|
migrate_from_chat_id (int):
|
|
|
|
channel_chat_created (bool):
|
2015-08-21 19:49:07 +02:00
|
|
|
|
2016-04-22 16:07:44 +02:00
|
|
|
Deprecated: 4.0
|
|
|
|
new_chat_participant (:class:`telegram.User`): Use `new_chat_member`
|
|
|
|
instead.
|
|
|
|
|
|
|
|
left_chat_participant (:class:`telegram.User`): Use `left_chat_member`
|
|
|
|
instead.
|
|
|
|
|
2015-08-21 19:49:07 +02:00
|
|
|
Args:
|
|
|
|
message_id (int):
|
|
|
|
from_user (:class:`telegram.User`):
|
|
|
|
date (:class:`datetime.datetime`):
|
2015-12-17 14:55:20 +01:00
|
|
|
chat (:class:`telegram.Chat`):
|
2015-08-21 19:49:07 +02:00
|
|
|
**kwargs: Arbitrary keyword arguments.
|
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
forward_from (Optional[:class:`telegram.User`]):
|
2016-05-11 23:22:05 +02:00
|
|
|
forward_from_chat (:class:`telegram.Chat`):
|
2015-08-21 19:49:07 +02:00
|
|
|
forward_date (Optional[:class:`datetime.datetime`]):
|
|
|
|
reply_to_message (Optional[:class:`telegram.Message`]):
|
2016-05-24 01:28:36 +02:00
|
|
|
edit_date (Optional[:class:`datetime.datetime`]):
|
2015-08-21 19:49:07 +02:00
|
|
|
text (Optional[str]):
|
|
|
|
audio (Optional[:class:`telegram.Audio`]):
|
|
|
|
document (Optional[:class:`telegram.Document`]):
|
|
|
|
photo (Optional[List[:class:`telegram.PhotoSize`]]):
|
|
|
|
sticker (Optional[:class:`telegram.Sticker`]):
|
|
|
|
video (Optional[:class:`telegram.Video`]):
|
|
|
|
voice (Optional[:class:`telegram.Voice`]):
|
|
|
|
caption (Optional[str]):
|
|
|
|
contact (Optional[:class:`telegram.Contact`]):
|
|
|
|
location (Optional[:class:`telegram.Location`]):
|
2016-04-12 05:33:42 +02:00
|
|
|
new_chat_member (Optional[:class:`telegram.User`]):
|
|
|
|
left_chat_member (Optional[:class:`telegram.User`]):
|
2015-08-21 19:49:07 +02:00
|
|
|
new_chat_title (Optional[str]):
|
|
|
|
new_chat_photo (Optional[List[:class:`telegram.PhotoSize`]):
|
|
|
|
delete_chat_photo (Optional[bool]):
|
|
|
|
group_chat_created (Optional[bool]):
|
2016-03-14 09:54:25 +01:00
|
|
|
supergroup_chat_created (Optional[bool]):
|
|
|
|
migrate_to_chat_id (Optional[int]):
|
|
|
|
migrate_from_chat_id (Optional[int]):
|
|
|
|
channel_chat_created (Optional[bool]):
|
2015-08-21 19:49:07 +02:00
|
|
|
"""
|
|
|
|
|
2016-05-15 03:46:40 +02:00
|
|
|
def __init__(self, message_id, from_user, date, chat, **kwargs):
|
2015-08-22 04:15:29 +02:00
|
|
|
# Required
|
|
|
|
self.message_id = int(message_id)
|
2015-07-09 02:58:13 +02:00
|
|
|
self.from_user = from_user
|
|
|
|
self.date = date
|
|
|
|
self.chat = chat
|
2015-08-22 04:15:29 +02:00
|
|
|
# Optionals
|
2015-08-21 19:49:07 +02:00
|
|
|
self.forward_from = kwargs.get('forward_from')
|
2016-05-11 23:22:05 +02:00
|
|
|
self.forward_from_chat = kwargs.get('forward_from_chat')
|
2015-08-21 19:49:07 +02:00
|
|
|
self.forward_date = kwargs.get('forward_date')
|
|
|
|
self.reply_to_message = kwargs.get('reply_to_message')
|
2016-05-24 01:28:36 +02:00
|
|
|
self.edit_date = kwargs.get('edit_date')
|
2015-08-22 04:15:29 +02:00
|
|
|
self.text = kwargs.get('text', '')
|
2016-04-17 12:43:34 +02:00
|
|
|
self.entities = kwargs.get('entities', list())
|
2015-08-21 19:49:07 +02:00
|
|
|
self.audio = kwargs.get('audio')
|
|
|
|
self.document = kwargs.get('document')
|
|
|
|
self.photo = kwargs.get('photo')
|
|
|
|
self.sticker = kwargs.get('sticker')
|
|
|
|
self.video = kwargs.get('video')
|
|
|
|
self.voice = kwargs.get('voice')
|
2015-08-22 04:15:29 +02:00
|
|
|
self.caption = kwargs.get('caption', '')
|
2015-08-21 19:49:07 +02:00
|
|
|
self.contact = kwargs.get('contact')
|
|
|
|
self.location = kwargs.get('location')
|
2016-04-17 12:43:34 +02:00
|
|
|
self.venue = kwargs.get('venue')
|
2016-04-12 05:33:42 +02:00
|
|
|
self.new_chat_member = kwargs.get('new_chat_member')
|
|
|
|
self.left_chat_member = kwargs.get('left_chat_member')
|
2015-08-22 04:15:29 +02:00
|
|
|
self.new_chat_title = kwargs.get('new_chat_title', '')
|
2015-08-21 19:49:07 +02:00
|
|
|
self.new_chat_photo = kwargs.get('new_chat_photo')
|
2015-08-22 04:15:29 +02:00
|
|
|
self.delete_chat_photo = bool(kwargs.get('delete_chat_photo', False))
|
|
|
|
self.group_chat_created = bool(kwargs.get('group_chat_created', False))
|
2016-05-15 03:46:40 +02:00
|
|
|
self.supergroup_chat_created = bool(kwargs.get('supergroup_chat_created', False))
|
2015-12-16 15:31:02 +01:00
|
|
|
self.migrate_to_chat_id = int(kwargs.get('migrate_to_chat_id', 0))
|
|
|
|
self.migrate_from_chat_id = int(kwargs.get('migrate_from_chat_id', 0))
|
2016-05-15 03:46:40 +02:00
|
|
|
self.channel_chat_created = bool(kwargs.get('channel_chat_created', False))
|
2016-04-17 12:43:34 +02:00
|
|
|
self.pinned_message = kwargs.get('pinned_message')
|
2015-07-08 14:55:06 +02:00
|
|
|
|
2015-07-08 14:37:25 +02:00
|
|
|
@property
|
|
|
|
def chat_id(self):
|
2015-08-21 19:49:07 +02:00
|
|
|
"""int: Short for :attr:`Message.chat.id`"""
|
2015-07-08 14:37:25 +02:00
|
|
|
return self.chat.id
|
2015-07-07 21:50:36 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2015-07-09 02:15:46 +02:00
|
|
|
def de_json(data):
|
2015-08-21 19:49:07 +02:00
|
|
|
"""
|
|
|
|
Args:
|
2015-12-16 15:31:02 +01:00
|
|
|
data (dict):
|
2015-08-21 19:49:07 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
telegram.Message:
|
|
|
|
"""
|
2015-08-22 04:15:29 +02:00
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
2015-11-03 10:20:45 +01:00
|
|
|
data['from_user'] = User.de_json(data.get('from'))
|
2015-08-28 22:45:44 +02:00
|
|
|
data['date'] = datetime.fromtimestamp(data['date'])
|
2015-12-16 15:31:02 +01:00
|
|
|
data['chat'] = Chat.de_json(data.get('chat'))
|
2016-04-17 12:43:34 +02:00
|
|
|
data['entities'] = MessageEntity.de_list(data.get('entities'))
|
|
|
|
data['forward_from'] = User.de_json(data.get('forward_from'))
|
2016-05-11 23:37:46 +02:00
|
|
|
data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'))
|
2016-04-17 12:43:34 +02:00
|
|
|
data['forward_date'] = Message._fromtimestamp(data.get('forward_date'))
|
2016-05-15 04:26:56 +02:00
|
|
|
data['reply_to_message'] = Message.de_json(data.get('reply_to_message'))
|
2016-05-24 01:28:36 +02:00
|
|
|
data['edit_date'] = Message._fromtimestamp(data.get('edit_date'))
|
2016-04-17 12:43:34 +02:00
|
|
|
data['audio'] = Audio.de_json(data.get('audio'))
|
|
|
|
data['document'] = Document.de_json(data.get('document'))
|
|
|
|
data['photo'] = PhotoSize.de_list(data.get('photo'))
|
|
|
|
data['sticker'] = Sticker.de_json(data.get('sticker'))
|
|
|
|
data['video'] = Video.de_json(data.get('video'))
|
|
|
|
data['voice'] = Voice.de_json(data.get('voice'))
|
|
|
|
data['contact'] = Contact.de_json(data.get('contact'))
|
|
|
|
data['location'] = Location.de_json(data.get('location'))
|
|
|
|
data['venue'] = Venue.de_json(data.get('venue'))
|
|
|
|
data['new_chat_member'] = User.de_json(data.get('new_chat_member'))
|
|
|
|
data['left_chat_member'] = User.de_json(data.get('left_chat_member'))
|
|
|
|
data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'))
|
|
|
|
data['pinned_message'] = Message.de_json(data.get('pinned_message'))
|
2015-08-21 19:49:07 +02:00
|
|
|
|
2015-08-28 22:45:44 +02:00
|
|
|
return Message(**data)
|
2015-07-09 16:40:44 +02:00
|
|
|
|
2015-09-07 04:55:00 +02:00
|
|
|
def __getitem__(self, item):
|
|
|
|
if item in self.__dict__.keys():
|
|
|
|
return self.__dict__[item]
|
|
|
|
elif item == 'chat_id':
|
|
|
|
return self.chat.id
|
|
|
|
|
2015-07-20 04:25:44 +02:00
|
|
|
def to_dict(self):
|
2015-08-21 19:49:07 +02:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
dict:
|
|
|
|
"""
|
2015-08-28 22:45:44 +02:00
|
|
|
data = super(Message, self).to_dict()
|
2015-08-22 04:15:29 +02:00
|
|
|
|
|
|
|
# Required
|
2015-11-03 13:46:23 +01:00
|
|
|
data['from'] = data.pop('from_user', None)
|
2015-08-22 04:15:29 +02:00
|
|
|
data['date'] = self._totimestamp(self.date)
|
|
|
|
# Optionals
|
2015-08-21 19:49:07 +02:00
|
|
|
if self.forward_date:
|
|
|
|
data['forward_date'] = self._totimestamp(self.forward_date)
|
2016-05-24 01:28:36 +02:00
|
|
|
if self.edit_date:
|
|
|
|
data['edit_date'] = self._totimestamp(self.edit_date)
|
2015-07-09 16:40:44 +02:00
|
|
|
if self.photo:
|
2015-07-20 04:25:44 +02:00
|
|
|
data['photo'] = [p.to_dict() for p in self.photo]
|
2016-04-18 21:35:39 +02:00
|
|
|
if self.entities:
|
|
|
|
data['entities'] = [e.to_dict() for e in self.entities]
|
2015-07-09 16:40:44 +02:00
|
|
|
if self.new_chat_photo:
|
2015-08-19 20:41:09 +02:00
|
|
|
data['new_chat_photo'] = [p.to_dict() for p in self.new_chat_photo]
|
2015-08-21 19:49:07 +02:00
|
|
|
|
2015-07-20 04:06:04 +02:00
|
|
|
return data
|
2015-08-14 20:47:31 +02:00
|
|
|
|
2015-08-22 04:15:29 +02:00
|
|
|
@staticmethod
|
|
|
|
def _fromtimestamp(unixtime):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
unixtime (int):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
datetime.datetime:
|
|
|
|
"""
|
|
|
|
if not unixtime:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return datetime.fromtimestamp(unixtime)
|
|
|
|
|
2015-08-14 20:47:31 +02:00
|
|
|
@staticmethod
|
2015-08-21 19:49:07 +02:00
|
|
|
def _totimestamp(dt_obj):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
dt_obj (:class:`datetime.datetime`):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int:
|
|
|
|
"""
|
2015-08-22 04:15:29 +02:00
|
|
|
if not dt_obj:
|
|
|
|
return None
|
|
|
|
|
2015-08-21 19:49:07 +02:00
|
|
|
try:
|
|
|
|
# Python 3.3+
|
|
|
|
return int(dt_obj.timestamp())
|
|
|
|
except AttributeError:
|
|
|
|
# Python 3 (< 3.3) and Python 2
|
|
|
|
return int(mktime(dt_obj.timetuple()))
|