- Notifications
You must be signed in to change notification settings - Fork5.7k
How to handle timeouts#2876
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Unfortunately, the Wiki doesn't provide any code samples on how to properly handle timeouts. Usingechobot.py as an example, connections will sometimes be closed on the Telegram end and a It's unclear how a retry can be achieved, alas. This is what I tried, thus far. To no avail: ...deferror_handler(update:Update,context:CallbackContext)->None:print("Error raised")update.message.chat.bot.get_updates()time.sleep(10)dispatcher.add_error_handler(error_handler)... |
BetaWas this translation helpful?Give feedback.
All reactions
This is the solution I ended up settling on.
defretry_on_error(func,wait=0.1,retry=0,*args,**kwargs):i=0whileTrue:try:returnfunc(*args,**kwargs)breakexcepttelegram.error.NetworkError:logging.exception(f"Network Error. Retrying...{i}")i+=1time.sleep(wait)ifretry!=0andi==retry:break
Which can then be used as follows:
retry_on_error(update.message.reply_photo,photo=url)
EDIT: Should bereturn func...
Replies: 3 comments 12 replies
-
There's an wiki article forHandling network errors |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ok. So if I understand this correctly, the error handler is the wrong place to implement a retry logic, correct? I was hoping for a more general solution. Something akin to Telebot's |
BetaWas this translation helpful?Give feedback.
All reactions
-
yes
Note that this would retry the whole callback, which may be too much. e.g. if your callback sends 2 messages and only the second one fails, then a
You're mixing things up here: |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yep. You're right. I thought about that right after I posted it. Lol. Hm. I guess unless I'm overlooking something obvious, the best solution would be to wrap the most recurring API calls in my own functions. I'd rather not clutter my entire codebase with try-except logic.
Oh. Ok. I wasn't sure. I thought infinity_polling was basically a general solution that the creator of Telebot built out of PTB. |
BetaWas this translation helpful?Give feedback.
All reactions
-
not sure if I'm understanding you correctly, but pyTelegramBotAPI is not built on top of PTB - it's an independent library |
BetaWas this translation helpful?Give feedback.
All reactions
-
My bad. I could've sworn I saw references to PTB. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Note that |
BetaWas this translation helpful?Give feedback.
All reactions
-
That's what I figured. I just found it in another Telegram bot library that has an |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
This is the solution I ended up settling on. defretry_on_error(func,wait=0.1,retry=0,*args,**kwargs):i=0whileTrue:try:returnfunc(*args,**kwargs)breakexcepttelegram.error.NetworkError:logging.exception(f"Network Error. Retrying...{i}")i+=1time.sleep(wait)ifretry!=0andi==retry:break Which can then be used as follows: retry_on_error(update.message.reply_photo,photo=url) EDIT: Should be |
BetaWas this translation helpful?Give feedback.
All reactions
-
time.sleep() stops the whole thread, which might be good and maybe not depending on your code, and I would have probably used a for loop, but very neat solution! Thanks for sharing. |
BetaWas this translation helpful?Give feedback.
All reactions
👀 1
-
@Poolitzer can you share your final code please |
BetaWas this translation helpful?Give feedback.