N
N
Nazar Ivakhiv2021-03-25 19:21:01
Python
Nazar Ivakhiv, 2021-03-25 19:21:01

How to make the bot not react to other text, but only to the one in the button?

how to make the bot not react to other text but only to the one in the button?

@bot.message_handler(commands=['Підтримка'])
def send_about(message):
    bot.send_message(message.chat.id, '<b>Вітаємо!{0.first_name},ви в розділі "‍ Служба підтримки"</b>'"\n\nЯкщо ви помітили несправність бота зафіксуйте це(скріншот/запис екрану),сповістіть про це розробника   @ivakhiv_off\n\n  З питань реклами та комерції   @ivakhiv_off ".format(message.from_user, bot.get_me()),
        parse_mode='html')

@bot.message_handler(commands=["Оформити_доставку"])
def user_reg(message):
       markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
       itembtn1 = types.KeyboardButton('Новояворівськ')
       itembtn2 = types.KeyboardButton('Шкло (р-н школи)')
       itembtn3 = types.KeyboardButton('Шкло (р-н Санаторія)')
       itembtn4 = types.KeyboardButton('Старичі')
       itembtn5 = types.KeyboardButton('Старичі (Яворівський полігон КПП №1)')
       itembtn6 = types.KeyboardButton('Когути')
       markup.add(itembtn1, itembtn2, itembtn3, itembtn4, itembtn5, itembtn6)

       msg = bot.send_message(message.chat.id, '<b>Виберіть на "кнопках" населенний пункт\n\nДоставка тільки в перечислених населених пунктах!!!</b>\n\nпо Новояворівську - 20 грн\nНовояворівськ - Шкло (р-н школи) - 60 грн\nНовояворівськ - Шкло (р-н Санаторія) - 80 грн\nНовояворівськ - Старичі (в межах села) - 80 грн\nНовояворівськ - Старичі (Яворівський полігон КПП №1) - 120 грн\nНовояворівськ - Когути - 50 грн', reply_markup=markup,parse_mode='html')
       bot.register_next_step_handler(msg, process_city_step)

def process_city_step(message):
    try:
        if message.text == ("Новояворівськ" "Шкло (р-н школи)" "Шкло (р-н Санаторія)" "Старичі" "Старичі (Яворівський полігон КПП №1)" "Когути"):
            pass
            chat_id = message.chat.id
            user_dict[chat_id] = User(message.text)

            # сховати клавіатуру
            markup = types.ReplyKeyboardRemove(selective=False)
        
            markup = types.ReplyKeyboardRemove(selective=False)
        
            msg = bot.send_message(chat_id, "Прізвище ім'я | ПІБ:", reply_markup=markup)
            bot.register_next_step_handler(msg, process_fullname_step)
        else:
            bot.send_message(chat_id, "Новояворівськ" "Шкло (р-н школи)" "Шкло (р-н Санаторія)" "Старичі" "Старичі (Яворівський полігон КПП №1)" "Когути", reply_markup=markup)

    except Exception as e:
        bot.reply_to(message, 'Ми працюємо тільки по вказаним населеним пунктам')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-03-25
@twistfire92

Reply buttons simulate sending a message. Those. if you click on such a button, it will be equivalent to sending the text manually.
Therefore, if you have a certain number of buttons that are responsible for some specific phrases, then just at the very beginning of the process_city_step function, check message.text for a match with the desired values. If it doesn't match any of them, delete the message, either inform the user about the error and call bot.register_next_step_handler(msg, process_fullname_step) again to give the user a second try to click on the desired button, or enter the desired message.
But if I were you, I would consider an option with an Inline keyboard. Much more features and does not clutter up the interface. And there will be no more problems with checking for available values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question