M
M
marselabdullin2020-09-13 19:44:19
Bots
marselabdullin, 2020-09-13 19:44:19

Why does the bot only reply to the second message?

The bot only responds to the second message on the button. What is the reason?

import telebot


bot = telebot.TeleBot(token='***')

button1 = telebot.types.KeyboardButton('Я ищу проект')
button2 = telebot.types.KeyboardButton('Я ищу команду')

main_markup = telebot.types.ReplyKeyboardMarkup().add(
    button1).add(button2)


@bot.message_handler(commands=['start', 'help'], content_types=['text'])
def process_start_command(message):
    bot.send_message(message.chat.id, "Привет! {0}".format(message.chat.first_name))
    bot.send_message(message.chat.id, "Чем я могу помочь тебе?",
                     reply_markup=main_markup)
    bot.register_next_step_handler(message, name)


@bot.message_handler(content_types=['text'])
def process_start_command(message):
    bot.register_next_step_handler(message, name)

def name(msg):
    if msg.text == 'Я ищу команду':
        answer = 'Все проекты'
    if msg.text == 'Я ищу проект':
        answer = 'Все участники'

    bot.send_message(msg.chat.id, answer)
                     

if __name__ == '__main__':
   bot.polling(none_stop=True, interval=0, timeout=0)


5f5e4c33698d3732345913.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
afterters, 2020-09-13
@marselabdullin

In the name function, you do not set next_step_handler again , respectively, the next time the standard handler is called, which does not react in any way, but sets next_step_handler . The second message will already be processed correctly.
Correct code name

def name(msg):
    answer = 0
    
    if (msg.text == "Я ищу команду"):
        answer = "Все проекты"

    if (msg.text == "Я ищу проект"):
        answer = "Все участники"

    bot.register_next_step_handler (msg, name)
    if (answer):    # без этого, если ни один из if не отработает, бот упадёт с ошибкой
        bot.send_message(msg.chat.id, answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question