skip to Main Content

I am trying to do an optional input, and have this code

 bot.send_chat_action(message.from_user.id, 'typing')
 markup = types.InlineKeyboardMarkup()
 markup.add(types.InlineKeyboardButton("Лечу только в одну сторону", callback_data="one_way"))
 msg = bot.send_message(message.from_user.id, '✅Хорошо. Теперь введите дату возвращения:', reply_markup=markup)
 bot.register_next_step_handler(msg, get_return_date)

This code sends user a message with button to skip this step, and registers function get_return_date(), that waits for a date value. Message
And if user clicks the button, query handler register another function get_adults(), that waits for numeric value:

@bot.callback_query_handler(func=lambda call: call.data == "one_way")
def is_one_way(call):
    msg = bot.send_message(call.from_user.id,
                           '✅Хорошо. Сколько взрослых (пассажиров старше 12-ти лет на момент полёта) полетят 🤵👩‍💼?')
    bot.register_next_step_handler(msg, get_adults)
    return

And, trouble is that – if user clicks the button to skip, both get_return_date() and get_adults() are waiting for a value and work at one time:
Problem

Any ideas what should i do?

2

Answers


  1. You can do this:

    bot.send_chat_action(message.from_user.id, 'typing')
     msg = bot.send_message(message.from_user.id, '✅Хорошо. Теперь введите дату возвращения: (you can skip this passage with /skip)')
     bot.register_next_step_handler(msg, get_return_date)
    
    def get_retourn_date(message):
        if message.text == '/skip':
            get_adults()
            return
        # write here the actual function code
    
    Login or Signup to reply.
  2. i use query callbacks and i put this func at the begining of the callback:

    bot.clear_step_handler_by_chat_id(call.message.chat.id)
    

    u can use it like this

    def gen_markup():
      markup = InlineKeyboardMarkup()
      markup.row_width = 1
      markup.add(InlineKeyboardButton("Button 1", callback_data="one"),
                 InlineKeyboardButton("Button 2", callback_data="two"))
    
    
    def ButtonOneFunc(message):
       #do whatever u want here
    
    def ButtonTwoFunc(message):
       #do whatever u want here
    
    
    @bot.callback_query_handler(func=lambda call: True)
    def callback_query(call):
      bot.clear_step_handler_by_chat_id(call.message.chat.id)
      if call.data == "one":
        sent= bot.reply_to(call.message, "This is Button 1")  
        bot.register_next_step_handler(sent, ButtonOneFunc) 
      elif call.data == "two":
        sent= bot.reply_to(call.message, "This is Button 2")
        bot.register_next_step_handler(sent, ButtonTwoFunc)
    

    what will happen here is, whenever the user will click a button, it will clear all next_register_handler that were open
    and then enter the new call.data and set the handler which the user clicked on

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search