'"Message is not modified" when I press the InlineKeyboardButton multiple times and too fast

I have an InlineKeyboardButton that changes the text of a sent message. The time it takes to edit the message is about one second and if I wait this amount before pressing the button again, everything works fine. But if I press the button repeatedly before the message is edited, an exception is thrown with this message:

Message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message

The user won't notice anything, because it is not expected for the bot to edit the message when this button is pressed repeatedly before the message is already modified. And I can catch the exception and just ignore it, nothing bad happens. But I want to avoid the exception from being thrown altogether.

The part where I edit the message is this:

if text and keyboard:
    try:
        query.edit_message_text(
            text,
            parse_mode=ParseMode.HTML,
            disable_web_page_preview=True,
            reply_markup=InlineKeyboardMarkup(keyboard)
        )
    except error.BadRequest as err:
        logging.error(err)

When I press the button calmly one time, the message is editted. But when I press it really fast and multiple times, only for the fist time the message is edited, and until the message is done being modified, the requests all throw error.BadRequest.

Since the error message says Message is not modified, I figured if I check for this modification, I can avoid the error. So I changed my code to this:

if text and keyboard and text != query.message.text:
    try:
        query.edit_message_text(
            text,
            parse_mode=ParseMode.HTML,
            disable_web_page_preview=True,
            reply_markup=InlineKeyboardMarkup(keyboard)
        )
    except error.BadRequest as err:
        logging.error(err)

The part I added is text != query.message.text, so that it should only try to edit the message if the text is different. But this doesn't work either. Because when I press the button for the second (and fast) the message has not yet being modified and thus the test is passed and it tries to edit the message, but when it gets to edit the message, it's already modified and the exception is thrown. How can I fix this?



Solution 1:[1]

Try to save state of the last used text in context, and check for this context before you execute edit_message_text():

if context.user_data['query_message_text'] != text:
   context.user_data['query_message_text'] = text
   query.edit_message_text(text, reply_markup=InlineKeyboardMarkup(keyboard))

No need of try/except. Of course at some point you should assign something to context.user_data['query_message_text'], on /start for example. Otherwise you'll have a KeyError.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew K