Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

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()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...