Answer the question
In order to leave comments, you need to log in
How to make the telegram bot not stop working after an error?
The bot should ask for a number, but if the user does not enter a number, the bot crashes, climbed a lot of sites and nothing helps. We need the bot to re-request the number. On an example of this code it is desirable to explain.
def func_1(message):
i = int(message.text)
b = i * i
bot.send_message(message.chat.id, str(i)+' + '+str(i)+' = '+str(b))
Answer the question
In order to leave comments, you need to log in
def func_1(message):
if message.text.isdigit():
i = int(message.text)
b = i * i
bot.send_message(message.chat.id, str(i)+' + '+str(i)+' = '+str(b))
else:
bot.send_message(message.chat.id, "число")
Use - try:
This is an attempt to execute the code, if there is an error during the attempt, it will not stop the whole code, but simply skip it, but if an error occurs, you can execute another code, via except:
Or print the Error to the console - except Exception as a: print(a)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question