solving telegram bot issues
Browse files- 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 |
-
|
| 527 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
|
|
|
| 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()
|