Search code examples
pythontelegram-botpython-telegram-bot

How To Edit / Delete Message While Using InlineKeyboard Python-Telegarm-Bot


I was developing a bot in python-telegram-bot framework and as on last stage i added some inline keyboard buttons for navigating from start message to other messages like help message or about message... And it worked ..... When someone use the inline keyboard it will sent the new message rather than editing the old message.... So I will get 2 messages 1 the orginal one from which the user tried to navigate and the new message which user wanted to navigate.... I want my bot to edit the 1st msg and sent the 2nd message..... I found found how can I do this if anyone can help it would be very much appreciated.....

How I added inline keyboard :


@run_async
def help(update, context):
    text = Translation.HELP_TEXT,
    update.effective_message.reply_text(text=text,
                          #   parse_mode=ParseMode.HTML,
                             reply_markup=InlineKeyboardMarkup(
                                 [[ InlineKeyboardButton('⚡Home⚡', callback_data='start_button'),
                                    InlineKeyboardButton('Donate💸', callback_data='upgrade_button')
                                  ],
                                  [ InlineKeyboardButton('About🛑', callback_data='about_button')
                                 ]]
                             )
                            )


@run_async
def about(update, context):
    text = Translation.ABOUT_TEXT,
    update.effective_message.reply_text(text=text,
                            # parse_mode=ParseMode.HTML,
                             reply_markup=InlineKeyboardMarkup(
                                 [[ InlineKeyboardButton('⚡Home⚡', callback_data='start_button'),
                                    InlineKeyboardButton('⚙ Help', callback_data='help_button')
                                  ],
                                  [ InlineKeyboardButton('Close🔐', callback_data='cancel_btn')
                                 ]]
                             )
                            )

How I added callback :

def main():
    fs_utils.start_cleanup()
   
    help_handler = CommandHandler(BotCommands.HelpCommand, help)
    
    about_handler = CommandHandler(BotCommands.AboutCommand, about)
    
    #Callback
    #
    #
    #
    #
    start_callback = CallbackQueryHandler(start, pattern='start_button')
    
    about_callback = CallbackQueryHandler(about, pattern='about_button')
    
    upgrade_callback = CallbackQueryHandler(upgrade, pattern='upgrade_button')
    
    help_callback = CallbackQueryHandler(help, pattern='help_button')
    #
    #Dispatcher
    #
    #
    #
    
    dispatcher.add_handler(about_handler)
    dispatcher.add_handler(help_handler)
    

    #callback
    #
    #
    #
    #
    dispatcher.add_handler(start_callback)
    dispatcher.add_handler(help_callback)
    dispatcher.add_handler(about_callback)
    dispatcher.add_handler(upgrade_callback)
    #
    #
    #
    #
    #
    updater.start_polling()
    LOGGER.info("Bot Started Sucessfully!")
    signal.signal(signal.SIGINT, fs_utils.exit_clean_up)

main()


Solution

  • Instead of update.effective_message.reply_text, use update.callback_query.edit_text

    edit_text method will edit the text of the sent message and the reply_markup while reply_text sends a new message.

    Example usage:

    update.callback_query.edit_text(text=text,
                                # parse_mode=ParseMode.HTML,
                                 reply_markup=InlineKeyboardMarkup(
                                     [[ InlineKeyboardButton('⚡Home⚡', callback_data='start_button'),
                                        InlineKeyboardButton('⚙ Help', callback_data='help_button')
                                      ],
                                      [ InlineKeyboardButton('Close🔐', callback_data='cancel_btn')
                                     ]]
                                 )
                                )
    

    Refer the docs here