如何从普通消息更改以前的 Inlinekeyboard 消息

How to change a previous Inlinekeyboard message from a normal message

在我的机器人中,用户应该主要通过 Inlinekeyboard 消息进行交互。

因此,如果用户写了一些 'regular' 消息或发送了一些东西,我必须以某种方式更改最后的 Inlinekeyboard 消息以继续该过程或给他一些消息。

见下图:

用户写了一些消息,但我不得不用新消息创建一个新的 Inlinekeyboard 按钮,因为我找不到获取前一个 'Welcome' 的 message_id 的方法] 按钮并更改它。

我的代码:

HELP = range(1)

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('Help', callback_data='help')]
               ]

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

def unknown(bot, update):

    message = 'Please press the Help button for more instructions.'

    keyboard = [
                 [InlineKeyboardButton('Help', callback_data='help')]
               ]

    update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))



# 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(MessageHandler(Filters.all, unknown))

updater.start_polling()

updater.idle()    

此致。 克莱森里奥斯.

如果您想编辑机器人发送的先前消息,您需要存储它们的message_id(您已经知道)。

所以你可以简单地这样做:

sent_message = update.message.reply_text(
    message,
    reply_markup=InlineKeyboardMarkup(keyboard)
)

然后你可以像sent_message.message_id

一样访问它

现在对于变量 sent_message,您可以将其设为 global 或者创建一个 单独的文件 来处理这些变量,或者在更大的范围内,您可以将其存储在 数据库 .