|
27 | 27 | from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp |
28 | 28 |
|
29 | 29 |
|
| 30 | +_UNDEFINED = object() |
| 31 | + |
| 32 | + |
30 | 33 | class Message(TelegramObject): |
31 | 34 | """ |
32 | 35 | This object represents a message. |
@@ -170,6 +173,7 @@ class Message(TelegramObject): |
170 | 173 | successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service |
171 | 174 | message about a successful payment, information about the payment. |
172 | 175 | """ |
| 176 | + _effective_attachment = _UNDEFINED |
173 | 177 |
|
174 | 178 | def __init__(self, |
175 | 179 | message_id, |
@@ -301,6 +305,39 @@ def de_json(cls, data, bot): |
301 | 305 |
|
302 | 306 | return cls(bot=bot, **data) |
303 | 307 |
|
| 308 | + @property |
| 309 | + def effective_attachment(self): |
| 310 | + """ |
| 311 | + :class:`telegram.Audio` |
| 312 | + or :class:`telegram.Contact` |
| 313 | + or :class:`telegram.Document` |
| 314 | + or :class:`telegram.Game` |
| 315 | + or :class:`telegram.Invoice` |
| 316 | + or :class:`telegram.Location` |
| 317 | + or List[:class:`telegram.PhotoSize`] |
| 318 | + or :class:`telegram.Sticker` |
| 319 | + or :class:`telegram.SuccessfulPayment` |
| 320 | + or :class:`telegram.Venue` |
| 321 | + or :class:`telegram.Video` |
| 322 | + or :class:`telegram.VideoNote` |
| 323 | + or :class:`telegram.Voice`: The attachment that this message was sent with. May be |
| 324 | + ``None`` if no attachment was sent. |
| 325 | +
|
| 326 | + """ |
| 327 | + if self._effective_attachment is not _UNDEFINED: |
| 328 | + return self._effective_attachment |
| 329 | + |
| 330 | + for i in (self.audio, self.game, self.document, self.photo, self.sticker, |
| 331 | + self.video, self.voice, self.video_note, self.contact, self.location, |
| 332 | + self.venue, self.invoice, self.successful_payment): |
| 333 | + if i is not None: |
| 334 | + self._effective_attachment = i |
| 335 | + break |
| 336 | + else: |
| 337 | + self._effective_attachment = None |
| 338 | + |
| 339 | + return self._effective_attachment |
| 340 | + |
304 | 341 | def __getitem__(self, item): |
305 | 342 | if item in self.__dict__.keys(): |
306 | 343 | return self.__dict__[item] |
|
0 commit comments