Answer the question
In order to leave comments, you need to log in
How to make a telebot error handler?
I am writing a bot in telegram, and imagined the situation while the bot is parsing a huge database (seconds 2), the user can stop the bot, and an error pops up that the chat where the bot tried to send a message was not found
. In the aiogram library, all this can be prevented by the following code:
@dispatcher.errors_handler(exception=BotBlocked)
async def error_bot_blocked(update: types.Update, exception: BotBlocked):
print(f"Меня заблокировал пользователь!\nСообщение: {update.message.chat.id}\nОшибка: {exception}")
return True
Answer the question
In order to leave comments, you need to log in
Well, it's try-except straight out of the Python box.
try:
bot.send_message(message.chat.id, 'Поиск выполнен успешно')
except Exception:
print('пользователь заблокировал бота')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question