如何在使用InlineKeyboard Python-Telearm-Bot时编辑/删除消息 [英] How To Edit / Delete Message While Using InlineKeyboard Python-Telegarm-Bot

查看:38
本文介绍了如何在使用InlineKeyboard Python-Telearm-Bot时编辑/删除消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于python-Telegram-bot框架的机器人,在最后一个阶段,我添加了一些内联键盘按钮,用于从开始消息导航到其他消息,如帮助消息或关于消息...它起作用了.当有人使用内联键盘时,它将发送新消息,而不是编辑旧消息...因此,我将收到2条消息1用户试图导航的原始消息和用户想要导航的新消息……我希望我的机器人编辑第一条消息并发送第二条消息.....我发现我该怎么做,如果有人能帮忙的话我会非常感激的.

我如何添加串联键盘:


@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')
                                 ]]
                             )
                            )

我如何添加回调:

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

推荐答案

而不是update.effective_message.reply_text,使用update.callback_query.edit_text

edit_text方法将编辑已发送消息的文本和回复标记 而reply_text发送新消息。

用法示例:

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')
                                 ]]
                             )
                            )

参考文档here

这篇关于如何在使用InlineKeyboard Python-Telearm-Bot时编辑/删除消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆