在我的机器人中,用户应该主要通过Inlinekeyboard
消息进行交互。
因此,如果用户写了一些“常规”消息或发送了一些内容,我必须以某种方式更改最后一个Inlinekeyboard
消息以继续该过程或给他一些消息。
见下图:
用户写了一些消息,但我不得不用新消息创建一个新的Inlinekeyboard
按钮,因为我找不到一种方法来获取前一个“欢迎”按钮的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()
最好的祝福。 Kleyson Rios。
如果您想编辑机器人发送的先前消息,您需要存储他们的message_id
(您已经知道)。
所以你可以这样做:
sent_message = update.message.reply_text(
message,
reply_markup=InlineKeyboardMarkup(keyboard)
)
然后你可以像sent_message.message_id
一样访问它
现在,对于变量sent_message
,您可以将其设置为全局或创建单独的文件来处理这些变量,或者在更大规模上,您可以将其存储在数据库中。