如何在使用内联键盘时编辑/删除消息 Python-Telegram-Bot
How To Edit / Delete Message While Using InlineKeyboard Python-Telegarm-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_markup
而 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
我在 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_markup
而 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