Telegram-бот. Pu sh пользователи будут использовать кнопки, если пользователь использует тестовое сообщение - PullRequest
0 голосов
/ 07 августа 2020

Что мне использовать, если пользователь не выбрал какую-либо кнопку, но написал сообщение, и я хочу сказать ему, что он должен нажать одну из кнопок и показать их снова (ВАЖНО: из предыдущего шага, потому что таких шагов может быть много.)

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()

1 Ответ

0 голосов
/ 23 августа 2020

Это то, что вам нужно. Используйте 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()
...