[英]How to check if user press the reply keyboard button? telegram bot python
Easy task, but I can't figure out how to do it.简单的任务,但我不知道该怎么做。
I'd like to check if user press the button 'Add variants' to add next messages from user to list variants
.我想检查用户是否按下“添加变体”按钮将用户的下一条消息添加到列表variants
。 I tried to do it using if message.text == 'Add variants'
, but it just adds to the list text 'Add variants' one time and that's it.我尝试使用if message.text == 'Add variants'
来做到这一点,但它只是一次将“添加变体”添加到列表文本中,仅此而已。 Is there any way to check if user click the button and after that add next messages from user to list?有什么方法可以检查用户是否单击按钮,然后将用户的下一条消息添加到列表中?
variants = []
@bot.message_handler(commands=['start', 'welcome'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item_1 = types.KeyboardButton('Random🎲')
item_2 = types.KeyboardButton('TradingView💸')
item_3 = types.KeyboardButton('Weather🌤')
item_4 = types.KeyboardButton('Other')
item_5 = types.KeyboardButton('Back🔙')
markup.add(item_1, item_2, item_3, item_4, item_5)
bot.send_message(message.chat.id, 'Choose', reply_markup = markup)
@bot.message_handler(content_types=['text'])
def bot_message(message):
if message.chat.type == 'private':
if message.text == 'Random🎲':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item_1 = types.KeyboardButton('Add variants')
item_2 = types.KeyboardButton('Back🔙')
markup.add(item_1, item_2)
bot.send_message(message.chat.id, 'Choose', reply_markup = markup)
In this part of code you dont add anything to 'variants'.在这部分代码中,您不会向“变体”添加任何内容。 But as I understood: after user clicks 'Add variants' button you want to handle new message from user and add this message to a 'variants'.但据我了解:用户单击“添加变体”按钮后,您要处理来自用户的新消息并将此消息添加到“变体”。 If so you need to register next_step_handler after you handeled 'Add variants' message.如果是这样,您需要在处理“添加变体”消息后注册 next_step_handler。
Like that:像那样:
variants = []
@bot.message_handler(content_types=['text'])
def bot_message(message):
if message.chat.type == 'private':
if message.text == 'Add variants':
msg = bot.send_message(message.chat.id, "Send me variant")
bot.register_next_step_handler(msg, handling_variant)
def handling_variant(message):
variants.append(message.text)
here is example of using next_step_handler 这是使用 next_step_handler 的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.