Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp


_UNDEFINED = object()


class Message(TelegramObject):
"""
This object represents a message.
Expand Down Expand Up @@ -170,6 +173,7 @@ class Message(TelegramObject):
successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service
message about a successful payment, information about the payment.
"""
_effective_attachment = _UNDEFINED

def __init__(self,
message_id,
Expand Down Expand Up @@ -301,6 +305,39 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

@property
def effective_attachment(self):
"""
:class:`telegram.Audio`
or :class:`telegram.Contact`
or :class:`telegram.Document`
or :class:`telegram.Game`
or :class:`telegram.Invoice`
or :class:`telegram.Location`
or List[:class:`telegram.PhotoSize`]
or :class:`telegram.Sticker`
or :class:`telegram.SuccessfulPayment`
or :class:`telegram.Venue`
or :class:`telegram.Video`
or :class:`telegram.VideoNote`
or :class:`telegram.Voice`: The attachment that this message was sent with. May be
``None`` if no attachment was sent.

"""
if self._effective_attachment is not _UNDEFINED:
return self._effective_attachment

for i in (self.audio, self.game, self.document, self.photo, self.sticker,
self.video, self.voice, self.video_note, self.contact, self.location,
self.venue, self.invoice, self.successful_payment):
if i is not None:
self._effective_attachment = i
break
else:
self._effective_attachment = None

return self._effective_attachment

def __getitem__(self, item):
if item in self.__dict__.keys():
return self.__dict__[item]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ def test_equality(self):
self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))

def test_effective_attachment(self):
for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note',
'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'):
dummy = object()
kwargs = {i: dummy}
msg = telegram.Message(1, telegram.User(1, ""), None, None, **kwargs)
self.assertIs(msg.effective_attachment, dummy,
'invalid {} effective attachment'.format(i))

msg = telegram.Message(1, telegram.User(1, ""), None, None)
self.assertIsNone(msg.effective_attachment)


if __name__ == '__main__':
unittest.main()