Tishka17 / aiogram_dialog

GUI framework on top of aiogram
https://aiogram-dialog.readthedocs.io
Apache License 2.0
683 stars 103 forks source link

Edit media VOICE #432

Open DarthRu opened 1 month ago

DarthRu commented 1 month ago

If you try to edit the Voice media in a message, the error line 337, in edit_media is displayed media = INPUT_MEDIA_TYPES[new_message.media.type]( KeyError: 'voice'. It may be necessary to delete the message and send a new one to 'change' it, in the same way as 'changing' a message without media to one with media.

The way I've solved it is this: File aiogram_dialog/manager/message_manager.py: Added Line 62: content_type=message_result.content_type, Edited To had_media function: line 100-112:

def had_media(self, old_message: OldMessage) -> bool:
    return old_message.media_id is not None and old_message.content_type != 'voice'

def had_voice(self, old_message: OldMessage) -> bool:
    return old_message.content_type == 'voice'

def need_voice(self, new_message: NewMessage) -> bool:
    try:
        return new_message.media.type == 'voice'
    except:
        return

def need_media(self, new_message: NewMessage) -> bool:
    return bool(new_message.media) and new_message.media.type != 'voice'

Edited to _can_edit function: line 148:

def _can_edit(self, new_message: NewMessage,
                  old_message: OldMessage) -> bool:
        # we cannot edit message if media appeared or removed
        if self.had_voice(old_message):
            return False
        return (
            self.had_media(old_message) == self.need_media(new_message) and
            not self.had_reply_keyboard(old_message) and
            not self.need_reply_keyboard(new_message)
        )

Edited to show_message function: line 190: if not self._can_edit(new_message, old_message) or new_message.media.type == 'voice':

File aiogram_dialog/api/entities/new_message.py: Added To OldMessage class: line 25: content_type: str

File aiogram_dialog/manager/manager.py: Added to the _get_message_from_callback function: line 400: content_type=current_message.content_type,

Added to the _get_last_message function: Line 437: content_type=event.content_type,

And to put it simply, I added a check if they want to change the message with content_type = 'voice'. If this condition is met, all deletion will follow the chain of deleting a message without media and sending it with media

chirizxc commented 1 month ago

version?

DarthRu commented 1 month ago

version?

Last release (2.2.0)