1010 User ,
1111)
1212from telegram .constants import MessageLimit
13- from telegram .error import BadRequest
13+ from telegram .error import BadRequest , TimedOut , NetworkError , TelegramError
1414from telegram .ext import (
1515 CallbackQueryHandler ,
1616 CommandHandler ,
@@ -322,9 +322,14 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
322322 )
323323 except BadRequest as excp :
324324 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
325- await bot .send_sticker (chat .id , filt .reply , reply_markup = keyboard )
325+ try :
326+ await bot .send_sticker (chat .id , filt .reply , reply_markup = keyboard )
327+ except (TimedOut , NetworkError ):
328+ LOGGER .warning ("Timeout/Network error sending sticker filter in chat %s" , str (chat .id ))
326329 else :
327330 raise
331+ except (TimedOut , NetworkError ):
332+ LOGGER .warning ("Timeout/Network error replying with sticker filter in chat %s" , str (chat .id ))
328333 elif filt .is_document :
329334 try :
330335 await reply_msg .reply_document (
@@ -336,15 +341,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
336341 )
337342 except BadRequest as excp :
338343 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
339- await bot .send_document (
340- chat .id ,
341- filt .reply ,
342- caption = media_caption ,
343- parse_mode = "Markdown" ,
344- reply_markup = keyboard ,
345- )
344+ try :
345+ await bot .send_document (
346+ chat .id ,
347+ filt .reply ,
348+ caption = media_caption ,
349+ parse_mode = "Markdown" ,
350+ reply_markup = keyboard ,
351+ )
352+ except (TimedOut , NetworkError ):
353+ LOGGER .warning ("Timeout/Network error sending document filter in chat %s" , str (chat .id ))
346354 else :
347355 raise
356+ except (TimedOut , NetworkError ):
357+ LOGGER .warning ("Timeout/Network error replying with document filter in chat %s" , str (chat .id ))
348358 elif filt .is_image :
349359 try :
350360 await reply_msg .reply_photo (
@@ -356,15 +366,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
356366 )
357367 except BadRequest as excp :
358368 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
359- await bot .send_photo (
360- chat .id ,
361- filt .reply ,
362- caption = media_caption ,
363- parse_mode = "Markdown" ,
364- reply_markup = keyboard ,
365- )
369+ try :
370+ await bot .send_photo (
371+ chat .id ,
372+ filt .reply ,
373+ caption = media_caption ,
374+ parse_mode = "Markdown" ,
375+ reply_markup = keyboard ,
376+ )
377+ except (TimedOut , NetworkError ):
378+ LOGGER .warning ("Timeout/Network error sending photo filter in chat %s" , str (chat .id ))
366379 else :
367380 raise
381+ except (TimedOut , NetworkError ):
382+ LOGGER .warning ("Timeout/Network error replying with photo filter in chat %s" , str (chat .id ))
368383 elif filt .is_audio :
369384 try :
370385 await reply_msg .reply_audio (
@@ -376,15 +391,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
376391 )
377392 except BadRequest as excp :
378393 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
379- await bot .send_audio (
380- chat .id ,
381- filt .reply ,
382- caption = media_caption ,
383- parse_mode = "Markdown" ,
384- reply_markup = keyboard ,
385- )
394+ try :
395+ await bot .send_audio (
396+ chat .id ,
397+ filt .reply ,
398+ caption = media_caption ,
399+ parse_mode = "Markdown" ,
400+ reply_markup = keyboard ,
401+ )
402+ except (TimedOut , NetworkError ):
403+ LOGGER .warning ("Timeout/Network error sending audio filter in chat %s" , str (chat .id ))
386404 else :
387405 raise
406+ except (TimedOut , NetworkError ):
407+ LOGGER .warning ("Timeout/Network error replying with audio filter in chat %s" , str (chat .id ))
388408 elif filt .is_voice :
389409 try :
390410 await reply_msg .reply_voice (
@@ -396,15 +416,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
396416 )
397417 except BadRequest as excp :
398418 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
399- await bot .send_voice (
400- chat .id ,
401- filt .reply ,
402- caption = media_caption ,
403- parse_mode = "Markdown" ,
404- reply_markup = keyboard ,
405- )
419+ try :
420+ await bot .send_voice (
421+ chat .id ,
422+ filt .reply ,
423+ caption = media_caption ,
424+ parse_mode = "Markdown" ,
425+ reply_markup = keyboard ,
426+ )
427+ except (TimedOut , NetworkError ):
428+ LOGGER .warning ("Timeout/Network error sending voice filter in chat %s" , str (chat .id ))
406429 else :
407430 raise
431+ except (TimedOut , NetworkError ):
432+ LOGGER .warning ("Timeout/Network error replying with voice filter in chat %s" , str (chat .id ))
408433 elif filt .is_video :
409434 try :
410435 await reply_msg .reply_video (
@@ -416,15 +441,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
416441 )
417442 except BadRequest as excp :
418443 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
419- await bot .send_video (
420- chat .id ,
421- filt .reply ,
422- caption = media_caption ,
423- parse_mode = "Markdown" ,
424- reply_markup = keyboard ,
425- )
444+ try :
445+ await bot .send_video (
446+ chat .id ,
447+ filt .reply ,
448+ caption = media_caption ,
449+ parse_mode = "Markdown" ,
450+ reply_markup = keyboard ,
451+ )
452+ except (TimedOut , NetworkError ):
453+ LOGGER .warning ("Timeout/Network error sending video filter in chat %s" , str (chat .id ))
426454 else :
427455 raise
456+ except (TimedOut , NetworkError ):
457+ LOGGER .warning ("Timeout/Network error replying with video filter in chat %s" , str (chat .id ))
428458
429459 elif filt .has_markdown :
430460 keyb = build_keyboard (buttons )
@@ -449,13 +479,16 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
449479 "again, or ask in @ELSupport for help."
450480 )
451481 elif excp .message in ("Message to be replied not found" , "Replied message not found" ):
452- await bot .send_message (
453- chat .id ,
454- filt .reply ,
455- parse_mode = "Markdown" ,
456- link_preview_options = {"is_disabled" : should_preview_disabled },
457- reply_markup = keyboard ,
458- )
482+ try :
483+ await bot .send_message (
484+ chat .id ,
485+ filt .reply ,
486+ parse_mode = "Markdown" ,
487+ link_preview_options = {"is_disabled" : should_preview_disabled },
488+ reply_markup = keyboard ,
489+ )
490+ except (TimedOut , NetworkError ):
491+ LOGGER .warning ("Timeout/Network error sending markdown filter in chat %s" , str (chat .id ))
459492 else :
460493 await reply_msg .reply_text (
461494 "This note could not be sent, as it is incorrectly formatted. Ask in "
@@ -469,15 +502,22 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
469502 str (filt .keyword ),
470503 str (chat .id ),
471504 )
505+ except (TimedOut , NetworkError ):
506+ LOGGER .warning ("Timeout/Network error replying with markdown filter in chat %s" , str (chat .id ))
472507
473508 else :
474509 try :
475510 await reply_msg .reply_text (filt .reply )
476511 except BadRequest as excp :
477512 if excp .message in ("Message to be replied not found" , "Replied message not found" ):
478- await bot .send_message (chat .id , filt .reply )
513+ try :
514+ await bot .send_message (chat .id , filt .reply )
515+ except (TimedOut , NetworkError ):
516+ LOGGER .warning ("Timeout/Network error sending text filter in chat %s" , str (chat .id ))
479517 else :
480518 raise
519+ except (TimedOut , NetworkError ):
520+ LOGGER .warning ("Timeout/Network error replying with text filter in chat %s" , str (chat .id ))
481521 break
482522
483523
0 commit comments