Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGES/1841.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix invalid `content_type` property identification for a live photo message
240 changes: 126 additions & 114 deletions aiogram/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
SendDocument,
SendGame,
SendInvoice,
SendLivePhoto,
SendLocation,
SendMediaGroup,
SendMessage,
Expand Down Expand Up @@ -712,6 +713,8 @@ def content_type(self) -> str:
return ContentType.DOCUMENT
if self.game:
return ContentType.GAME
if self.live_photo:
return ContentType.LIVE_PHOTO
if self.photo:
return ContentType.PHOTO
if self.sticker:
Expand Down Expand Up @@ -850,8 +853,6 @@ def content_type(self) -> str:
return ContentType.POLL_OPTION_ADDED
if self.poll_option_deleted:
return ContentType.POLL_OPTION_DELETED
if self.live_photo:
return ContentType.LIVE_PHOTO
if self.rich_message:
return ContentType.RICH_MESSAGE
if self.community_chat_added:
Expand Down Expand Up @@ -3930,6 +3931,7 @@ def send_copy( # noqa: C901
| SendDocument
| SendLocation
| SendMessage
| SendLivePhoto
| SendPhoto
| SendPoll
| SendDice
Expand Down Expand Up @@ -3974,6 +3976,7 @@ def send_copy( # noqa: C901
SendDocument,
SendLocation,
SendMessage,
SendLivePhoto,
SendPhoto,
SendPoll,
SendSticker,
Expand All @@ -3998,118 +4001,127 @@ def send_copy( # noqa: C901
"message_effect_id": message_effect_id or self.effect_id,
}

if self.text:
return SendMessage(
text=self.text,
entities=self.entities,
link_preview_options=link_preview_options or self.link_preview_options,
**kwargs,
).as_(self._bot)
if self.audio:
return SendAudio(
audio=self.audio.file_id,
caption=self.caption,
title=self.audio.title,
performer=self.audio.performer,
duration=self.audio.duration,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
if self.animation:
return SendAnimation(
animation=self.animation.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
if self.document:
return SendDocument(
document=self.document.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
if self.photo:
return SendPhoto(
photo=self.photo[-1].file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
if self.sticker:
return SendSticker(
sticker=self.sticker.file_id,
**kwargs,
).as_(self._bot)
if self.video:
return SendVideo(
video=self.video.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
if self.video_note:
return SendVideoNote(
video_note=self.video_note.file_id,
**kwargs,
).as_(self._bot)
if self.voice:
return SendVoice(
voice=self.voice.file_id,
**kwargs,
).as_(self._bot)
if self.contact:
return SendContact(
phone_number=self.contact.phone_number,
first_name=self.contact.first_name,
last_name=self.contact.last_name,
vcard=self.contact.vcard,
**kwargs,
).as_(self._bot)
if self.venue:
return SendVenue(
latitude=self.venue.location.latitude,
longitude=self.venue.location.longitude,
title=self.venue.title,
address=self.venue.address,
foursquare_id=self.venue.foursquare_id,
foursquare_type=self.venue.foursquare_type,
**kwargs,
).as_(self._bot)
if self.location:
return SendLocation(
latitude=self.location.latitude,
longitude=self.location.longitude,
**kwargs,
).as_(self._bot)
if self.poll:
from .input_poll_option import InputPollOption

return SendPoll(
question=self.poll.question,
options=[
InputPollOption(
text=option.text,
voter_count=option.voter_count,
text_entities=option.text_entities,
text_parse_mode=None,
)
for option in self.poll.options
],
**kwargs,
).as_(self._bot)
if self.dice: # Dice value can't be controlled
return SendDice(
**kwargs,
).as_(self._bot)
if self.story:
return ForwardMessage(
from_chat_id=self.chat.id,
message_id=self.message_id,
**kwargs,
).as_(self._bot)

raise TypeError("This type of message can't be copied.")
match (self.content_type):
case ContentType.TEXT:
return SendMessage(
text=self.text,
entities=self.entities,
link_preview_options=link_preview_options or self.link_preview_options,
**kwargs,
).as_(self._bot)
case ContentType.AUDIO:
return SendAudio(
audio=self.audio.file_id,
caption=self.caption,
title=self.audio.title,
performer=self.audio.performer,
duration=self.audio.duration,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.ANIMATION:
return SendAnimation(
animation=self.animation.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.DOCUMENT:
return SendDocument(
document=self.document.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.PHOTO:
return SendPhoto(
photo=self.photo[-1].file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.LIVE_PHOTO:
return SendLivePhoto(
photo=self.photo[-1].file_id,
live_photo=self.live_photo.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.STICKER:
return SendSticker(
sticker=self.sticker.file_id,
**kwargs,
).as_(self._bot)
case ContentType.VIDEO:
return SendVideo(
video=self.video.file_id,
caption=self.caption,
caption_entities=self.caption_entities,
**kwargs,
).as_(self._bot)
case ContentType.VIDEO_NOTE:
return SendVideoNote(
video_note=self.video_note.file_id,
**kwargs,
).as_(self._bot)
case ContentType.VOICE:
return SendVoice(
voice=self.voice.file_id,
**kwargs,
).as_(self._bot)
case ContentType.CONTACT:
return SendContact(
phone_number=self.contact.phone_number,
first_name=self.contact.first_name,
last_name=self.contact.last_name,
vcard=self.contact.vcard,
**kwargs,
).as_(self._bot)
case ContentType.VENUE:
return SendVenue(
latitude=self.venue.location.latitude,
longitude=self.venue.location.longitude,
title=self.venue.title,
address=self.venue.address,
foursquare_id=self.venue.foursquare_id,
foursquare_type=self.venue.foursquare_type,
**kwargs,
).as_(self._bot)
case ContentType.LOCATION:
return SendLocation(
latitude=self.location.latitude,
longitude=self.location.longitude,
**kwargs,
).as_(self._bot)
case ContentType.POLL:
from .input_poll_option import InputPollOption

return SendPoll(
question=self.poll.question,
options=[
InputPollOption(
text=option.text,
voter_count=option.voter_count,
text_entities=option.text_entities,
text_parse_mode=None,
)
for option in self.poll.options
],
**kwargs,
).as_(self._bot)
case ContentType.DICE: # Dice value can't be controlled
return SendDice(
**kwargs,
).as_(self._bot)
case ContentType.STORY:
return ForwardMessage(
from_chat_id=self.chat.id,
message_id=self.message_id,
**kwargs,
).as_(self._bot)
case _:
raise TypeError("This type of message can't be copied.")

def copy_to(
self,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_api/test_types/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SendDocument,
SendGame,
SendInvoice,
SendLivePhoto,
SendLocation,
SendMediaGroup,
SendMessage,
Expand Down Expand Up @@ -911,6 +912,7 @@
TEST_MESSAGE_LIVE_PHOTO = Message(
message_id=42,
date=datetime.datetime.now(),
photo=[PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42)],
live_photo=LivePhoto(
file_id="file id",
file_unique_id="file unique id",
Expand Down Expand Up @@ -1122,7 +1124,7 @@
[TEST_MESSAGE_MANAGED_BOT_CREATED, None],
[TEST_MESSAGE_POLL_OPTION_ADDED, None],
[TEST_MESSAGE_POLL_OPTION_DELETED, None],
[TEST_MESSAGE_LIVE_PHOTO, None],
[TEST_MESSAGE_LIVE_PHOTO, SendLivePhoto],
[TEST_MESSAGE_RICH_MESSAGE, None],
[TEST_MESSAGE_COMMUNITY_CHAT_ADDED, None],
[TEST_MESSAGE_COMMUNITY_CHAT_REMOVED, None],
Expand Down
Loading