L
L
Lassa2020-12-03 16:45:39
Python
Lassa, 2020-12-03 16:45:39

How to make telegram bot accept only integer messages?

The bot should ask "How old are you?" and then show the result, but
how to make the bot accept only numerical messages (integer), and if you enter text, then let it start all over again or give an error.
I am using pyTelegramBotAPI

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
STARoD00M, 2020-12-03
@Lassa

@bot.message_handler(content_types=['text'])
def reg_age(message):
  global age
  age = message.text
  if age == int:
    bot.send_message(message.chat.id, "Хорошо тебе! " + age + "лет")
    bot.register_next_step_handler(message, next_step) # указывай какая функция будет обрабатывать следующее сообщение пользователя
  else:
    bot.send_message (message.chat.id, "Возраст это целое число! ")
    bot.register_next_step_handler(message, reg_age) # Следущее сообщение пользователя считает функция reg_age

Decorator bot.message_handler for functions from register_next_step_handler do not prescribe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question