Edoruin commited on
Commit
88663a3
·
1 Parent(s): d7dcd2f

solving telegram bot issues

Browse files
Files changed (1) hide show
  1. app/main.py +16 -14
app/main.py CHANGED
@@ -523,8 +523,10 @@ if TG_TOKEN:
523
  else:
524
  print("[BOT] Usando conexión directa a Telegram")
525
 
526
- telebot.apihelper.CONNECT_TIMEOUT = 30
527
- telebot.apihelper.READ_TIMEOUT = 30
 
 
528
 
529
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
530
 
@@ -651,20 +653,20 @@ def start_bot_thread():
651
  print("[BOT] Otra instancia detectada. No se iniciará el bot en este hilo.")
652
  return
653
 
 
654
  time.sleep(5)
655
- try: bot.delete_webhook()
656
- except: pass
657
 
658
- print("[BOT] Iniciando polling...")
659
- while True:
660
- try:
661
- # Incrementar timeout para reducir peticiones al proxy
662
- # long_polling_timeout es el tiempo que espera el servidor de Telegram
663
- bot.infinity_polling(timeout=60, long_polling_timeout=30)
664
- except Exception as e:
665
- print(f"[BOT ERROR] {e}")
666
- # Espera más larga en caso de error para evitar saturar el proxy/cuotas
667
- time.sleep(60)
 
668
 
669
  if bot:
670
  threading.Thread(target=start_bot_thread, daemon=True).start()
 
523
  else:
524
  print("[BOT] Usando conexión directa a Telegram")
525
 
526
+ # Los timeouts de la librería DEBEN ser mayores que el tiempo de espera del long polling de Telegram
527
+ # para evitar "Ghost connections" que causan el error 409 Conflict.
528
+ telebot.apihelper.CONNECT_TIMEOUT = 90
529
+ telebot.apihelper.READ_TIMEOUT = 90
530
 
531
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
532
 
 
653
  print("[BOT] Otra instancia detectada. No se iniciará el bot en este hilo.")
654
  return
655
 
656
+ # Espera inicial para que los recursos se estabilicen
657
  time.sleep(5)
 
 
658
 
659
+ try:
660
+ bot.delete_webhook()
661
+ print("[BOT] Webhook eliminado.")
662
+ except Exception as e:
663
+ print(f"[BOT] Error eliminando webhook: {e}")
664
+
665
+ print("[BOT] Iniciando infinity_polling...")
666
+ # infinity_polling ya maneja reconexiones automáticas y errores internos.
667
+ # timeout: Cada cuánto tiempo la librería refresca la conexión.
668
+ # long_polling_timeout: Cuánto tiempo espera el servidor de Telegram antes de responder vacío.
669
+ bot.infinity_polling(timeout=90, long_polling_timeout=30)
670
 
671
  if bot:
672
  threading.Thread(target=start_bot_thread, daemon=True).start()