mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-16 20:29:55 +01:00
parent
56f7d18853
commit
915cd64140
2 changed files with 49 additions and 0 deletions
|
@ -27,6 +27,9 @@ from telegram.utils.deprecate import warn_deprecate_obj
|
|||
from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp
|
||||
|
||||
|
||||
_UNDEFINED = object()
|
||||
|
||||
|
||||
class Message(TelegramObject):
|
||||
"""
|
||||
This object represents a message.
|
||||
|
@ -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,
|
||||
|
@ -301,6 +305,39 @@ class Message(TelegramObject):
|
|||
|
||||
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]
|
||||
|
|
|
@ -205,6 +205,18 @@ class MessageTest(BaseTest, unittest.TestCase):
|
|||
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()
|
||||
|
|
Loading…
Add table
Reference in a new issue