Telegram机器人.如果用户使用测试消息,则推送用户使用按钮



如果用户没有选择任何按钮,但他写了一条消息,我想告诉他必须单击其中一个按钮并再次显示它们,我应该使用什么?(重要提示:从上一步开始,因为这样的步骤可能很多。(

import config
import telebot

permissions = config.permissions
bot = telebot.TeleBot(config.API_BOT_TEST_TOKEN)

@bot.message_handler(commands=['start'])
def test_message(message):
chat_id = message.chat.id
markup = telebot.types.InlineKeyboardMarkup()
markup.add(telebot.types.InlineKeyboardButton(text='Алматы', callback_data='city_Алматы'))
markup.add(telebot.types.InlineKeyboardButton(text='Нур-Султан', callback_data='city_Нур-Султан'))
bot.send_message(chat_id, 'Choose your city', reply_markup=markup)
@bot.callback_query_handler(func=lambda call: 'city' in call.data)
def query(call):
bot.send_message(call.from_user.id, 'Good choice!')
bot.edit_message_reply_markup(call.from_user.id, message_id=call.message.message_id, reply_markup='')
##### what should I use if the user didn't choose any button but he wrote a message, and I want to tell him that he has to click one of the buttons and show them again (IMPORTANT: from the previous step)?

bot.polling()

这正是您所需要的。使用register_next_step_handler

@bot.message_handler(commands=['start'])
def test_message(message):
chat_id = message.chat.id
markup = telebot.types.InlineKeyboardMarkup()
markup.add(telebot.types.InlineKeyboardButton(text='Алматы', callback_data='city_Алматы'))
markup.add(telebot.types.InlineKeyboardButton(text='Нур-Султан', callback_data='city_Нур-Султан'))
msg = bot.send_message(chat_id, 'Choose your city', reply_markup=markup)
bot.register_next_step_handler(msg, process_city_step)
def process_city_step(message):
if not message.text.startswith('/'):
bot.send_message(message.chat.id, 'You has to click one of the buttons!')
test_message(message)
@bot.callback_query_handler(func=lambda call: 'city' in call.data)
def query(call):
bot.send_message(call.from_user.id, 'Good choice!')
bot.edit_message_reply_markup(call.from_user.id, message_id=call.message.message_id, reply_markup='')
##### what should I use if the user didn't choose any button but he wrote a message, and I want to tell him that he has to click one of the buttons and show them again (IMPORTANT: from the previous step)?

bot.polling()

最新更新