I
I
igreklpofrss2021-12-08 16:37:00
Python
igreklpofrss, 2021-12-08 16:37:00

How to make several answers (YES-NO) to a question in Telegram?

I need to implement the code so that it works like this: the bot asks a question - the user answers YES, NO. If the user answers YES, then there are no more questions, but if NO, then the YES, NO question is asked again. The first question asked works, but the second decorator is ignored by the interpreter. And if I write yes-no again, then it continues to execute the first block. What am I doing wrong and how to do it right?

@bot.message_handler(func = lambda c: True, commands=['komanda'])
def 1_step(message):
    'тут код продолжается'
    for i in values:
        spisok.append(i)
    'тут код продолжается'
    bot.register_next_step_handler(msg, 2_step)

def 2_step(message):
     'тут код продолжается'
    bot.register_next_step_handler(msg, 3_step)

def 3_step(message):
    @bot.message_handler(func=lambda message: message.text == 'YES' or 'NO')
    def yes_no(message):
        if message.text == 'YES':
            'выполняется блок'
        elif message.text == 'NO':
            @bot.message_handler(func=lambda message: message.text == 'YES' or 'NO') #<- не работает
            def yes_no_2(message):
                if message.text == 'YES':
                    'выполняется код'
                elif message.text == 'NO':
                    'выполняется код'
                else:
                    bot.send_message(id, 'Пиши yes или no')
        else:
            bot.send_message(id, 'Пиши yes или no')
    bot.register_next_step_handler(msg, 4_step)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
link_vrb, 2021-12-08
@link_vrb

@bot.message_handler(func = lambda c: True, commands=['komanda'])
def one_step(message):
    'тут код продолжается'
    for i in values:
        spisok.append(i)
    'тут код продолжается'
    bot.register_next_step_handler(msg, two_step)

def two_step(message):
     'тут код продолжается'
    bot.register_next_step_handler(msg, three_step)

def three_step(message):
    msg_low=lower(message.text)
    if msg_low == 'yes':
        'выполняется блок'
    elif msg_low == 'no':
        while True:
            @bot.message_handler(commands=['yes', 'YES', 'Yes', 'NO', 'no', 'No'])
            def yes_no_2(message):
                message_low=lower(message.text)
                if message_low == 'yes':
                    'выполняется код'
                elif message_low == 'no':
                    'выполняется код'
                else:
                    bot.send_message(id, 'Пиши yes или no')
    else:
        bot.send_message(id, 'Пиши yes или no')
bot.register_next_step_handler(msg, four_step)

I changed:
1) I created variables with the value of the message in lowercase, so that there would be no errors if a person writes yes in the chat and the program compares yes and YES and does not go any further because they are not equal.
2) Changed the way functions are activated, since in the previous version the program could not work as it should because of this.
3) Changed the names of the functions, since it is impossible to make a function that starts with a number.
Note: If there are tab errors: replace all groups of 4 spaces : ' ' with Tab.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question