A
A
Anatoly Belousov2021-05-07 21:53:47
Python
Anatoly Belousov, 2021-05-07 21:53:47

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

3 answer(s)
S
soremix, 2021-05-07
@no_name_774

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, "число")

Well, in else you need to add a next step handler to the same function, or how it's all implemented there

F
Frayl, 2021-05-07
@Frayl

try has left the chat...

V
Vladislav Mukhin, 2021-05-07
@SladkayaDoza

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 question

Ask a Question

731 491 924 answers to any question