I am writing a bot in Telegram in Python . I work with the pyTelegramBotAPI library.

Given: quest bot. For each task you can get a hint from it by pressing either the / hint command or the "Hint" button.

Task: answer with different hints for different tasks.

What wrote:

@bot.message_handler(commands=['start']) def handle_text(message): user_markup = telebot.types.ReplyKeyboardMarkup(1) user_markup.row("A", "B") bot.send_message(message.from_user.id, constants.start, reply_markup=user_markup) @bot.message_handler(content_types=['text']) def handle_text(message): if message.text == "A": bot.send_message(message.from_user.id, "Task 1, /hint") elif message.text == "/hint": bot.send_message(message.from_user.id, "Hint 1") @bot.message_handler(content_types=['text']) def handle_text(message): if message.text == "B": bot.send_message(message.from_user.id, "Task 2, /hint") elif message.text == "/hint": bot.send_message(message.from_user.id, "Hint 2") 

Of course, this code does not go, set it as an example, so that it is clear what I’m trying to achieve.

The bot does not respond to the second command:

The bot does not respond to the second command

  • one
    And what is your problem? - mkkik
  • Add error output if you have them, of course. - approximatenumber
  • @aapproximatenumber added bot behavior. He does not respond to the second team - Nikita Avdeev
  • The handle_text function must be one. In it is a search of possible messages: `if message == 'A': ... elif message == 'B': ...`. - Lebedev Ilya

1 answer 1

Here is my option

 import telebot from telebot import types import shelve bot = telebot.TeleBot('token') @bot.message_handler(commands=['start']) def start(mess): chat_id = mess.chat.id db = shelve.open('shelve') db[str(chat_id)] = 'init' db.close() markup = types.ReplyKeyboardMarkup() btn_a = types.KeyboardButton('A') btn_b = types.KeyboardButton('B') markup.add(btn_a, btn_b) bot.send_message(chat_id, 'ΠŸΡ€ΠΈΠ²Π΅Ρ‚', reply_markup = markup) @bot.message_handler(func=lambda mess: mess.text=='B' and mess.content_type=='text') def b(mess): chat_id = mess.chat.id db = shelve.open('shelve') state = db[str(chat_id)] if state != 'init': start(mess)#ΠΈΠ»ΠΈ какая-Π½ΠΈΠ±ΡƒΠ΄ΡŒ функция else: db[str(chat_id)] = 'B' db.close() markup = types.ReplyKeyboardMarkup() btn_hint = types.KeyboardButton('Подсказка') markup.add(btn_hint) bot.send_message(chat_id, 'B', reply_markup = markup) @bot.message_handler(func=lambda mess: mess.text=='A' and mess.content_type=='text') def a(mess): chat_id = mess.chat.id db = shelve.open('shelve') state = db[str(chat_id)] if state != 'init': start(mess)#ΠΈΠ»ΠΈ какая-Π½ΠΈΠ±ΡƒΠ΄ΡŒ функция else: db[str(chat_id)] = 'A' db.close() markup = types.ReplyKeyboardMarkup() btn_hint = types.KeyboardButton('Подсказка') markup.add(btn_hint) bot.send_message(chat_id, 'A', reply_markup = markup) @bot.message_handler(func=lambda mess: mess.text == 'Подсказка' and mess.content_type == 'text') def hint(mess): chat_id = mess.chat.id db = 'shelve'.open('shelve') state = db[str(chat_id)] if state == 'init': pass elif state == 'B': bot.send_message(chat_id, 'hint B') elif state == 'A': bot.send_message(chat_id, 'hint A') if __name__ == '__main__': bot.polling(True)