Telegram bot API 使用 python 编辑 InlineKeyboard-telegram-bot 不工作
Telegram bot API edit InlineKeyboard with python-telegram-bot not working
我正在尝试创建一个用户可以在其中导航的菜单。这是我的代码:
MENU, HELP = range(2)
def start(bot, update):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
# Create initial message:
message = 'Welcome.'
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
def help(bot, update):
keyboard = [
[InlineKeyboardButton('Leave', callback_data='cancel')]
]
update.callback_query.edit_message_reply_markup('Help ... help..', reply_markup=InlineKeyboardMarkup(keyboard))
def cancel(bot, update):
update.message.reply_text('Bye.', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
# Create the EventHandler and pass it your bot's token.
updater = Updater(token=config.TELEGRAM_API_TOKEN)
# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(CallbackQueryHandler(cancel, pattern='cancel'))
updater.start_polling()
updater.idle()
正如预期的那样,在 /start 用户获得菜单 'Help'。当用户点击它时,函数 help() 也会按预期触发。
根据我对 python-telegram-bot 文档的理解,应该填充 update.callback_query.inline_message_id,但其值为 None.
我需要 update.callback_query.inline_message_id 来更新我的 InlineKeyboard 菜单,对吗?为什么 inline_message_id 为空 (None)?
Python 3.6.7
python-telegram-bot==11.1.0
此致。
克莱森里奥斯.
我认为您的代码中存在 2 个问题。
第一。在您的 help
函数中,您试图同时更改消息的 text 和 markup。但是 edit_message_reply_markup
方法 仅更改标记 。所以而不是
update.callback_query.edit_message_reply_markup(
'Help ... help..',
reply_markup=InlineKeyboardMarkup(keyboard)
)
这样做:
bot.edit_message_text(
text='Help ... help..',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard)
)
bot.answer_callback_query(update.callback_query.id, text='')
注意变化:
- 我用
bot
替换了 update.callback_query
。
- 重要:我用
edit_message_text
替换了edit_message_reply_markup
;因为第一个 只更改标记 ,但第二个可以两者都做。
- 我添加了
chat_id
和 message_id
参数;因为that's what it says in the documents.
- 重要:我添加了一个新方法(
bot.answer_callback_query
);因为每次你处理一个回调查询你需要回答它(使用这个方法)。但是,您可以将 text
参数 留空 ,这样它就不会显示任何内容。
第二。如果我错了请纠正我,但我相信当用户按下 cancel
按钮时,您希望消息的文本 changed 到 "Bye." 并且移除键盘。如果是这种情况,您做错的是您在尝试移除键盘 (reply_markup=ReplyKeyboardRemove()
时发送 new 消息 (reply_text
) ).你可以简单地这样做:
bot.edit_message_text(
text='Bye',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
bot.answer_callback_query(update.callback_query.id, text='')
这里的想法是,当您编辑消息的 text 并且不使用标记键盘时,previous键盘会自动 移除 ,因此您无需使用 ReplyKeyboardRemove()
.
这是一个有效的 GIF(带有硬 G)!
我正在尝试创建一个用户可以在其中导航的菜单。这是我的代码:
MENU, HELP = range(2)
def start(bot, update):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
# Create initial message:
message = 'Welcome.'
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
def help(bot, update):
keyboard = [
[InlineKeyboardButton('Leave', callback_data='cancel')]
]
update.callback_query.edit_message_reply_markup('Help ... help..', reply_markup=InlineKeyboardMarkup(keyboard))
def cancel(bot, update):
update.message.reply_text('Bye.', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
# Create the EventHandler and pass it your bot's token.
updater = Updater(token=config.TELEGRAM_API_TOKEN)
# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(CallbackQueryHandler(cancel, pattern='cancel'))
updater.start_polling()
updater.idle()
正如预期的那样,在 /start 用户获得菜单 'Help'。当用户点击它时,函数 help() 也会按预期触发。
根据我对 python-telegram-bot 文档的理解,应该填充 update.callback_query.inline_message_id,但其值为 None.
我需要 update.callback_query.inline_message_id 来更新我的 InlineKeyboard 菜单,对吗?为什么 inline_message_id 为空 (None)?
Python 3.6.7
python-telegram-bot==11.1.0
此致。 克莱森里奥斯.
我认为您的代码中存在 2 个问题。
第一。在您的 help
函数中,您试图同时更改消息的 text 和 markup。但是 edit_message_reply_markup
方法 仅更改标记 。所以而不是
update.callback_query.edit_message_reply_markup(
'Help ... help..',
reply_markup=InlineKeyboardMarkup(keyboard)
)
这样做:
bot.edit_message_text(
text='Help ... help..',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard)
)
bot.answer_callback_query(update.callback_query.id, text='')
注意变化:
- 我用
bot
替换了update.callback_query
。 - 重要:我用
edit_message_text
替换了edit_message_reply_markup
;因为第一个 只更改标记 ,但第二个可以两者都做。 - 我添加了
chat_id
和message_id
参数;因为that's what it says in the documents. - 重要:我添加了一个新方法(
bot.answer_callback_query
);因为每次你处理一个回调查询你需要回答它(使用这个方法)。但是,您可以将text
参数 留空 ,这样它就不会显示任何内容。
第二。如果我错了请纠正我,但我相信当用户按下 cancel
按钮时,您希望消息的文本 changed 到 "Bye." 并且移除键盘。如果是这种情况,您做错的是您在尝试移除键盘 (reply_markup=ReplyKeyboardRemove()
时发送 new 消息 (reply_text
) ).你可以简单地这样做:
bot.edit_message_text(
text='Bye',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
bot.answer_callback_query(update.callback_query.id, text='')
这里的想法是,当您编辑消息的 text 并且不使用标记键盘时,previous键盘会自动 移除 ,因此您无需使用 ReplyKeyboardRemove()
.
这是一个有效的 GIF(带有硬 G)!