Edoruin commited on
Commit
2898c04
·
1 Parent(s): 3601b92

improving Telegram resquest, and solve model issues in classroom endpoint

Browse files
Files changed (1) hide show
  1. app/main.py +20 -7
app/main.py CHANGED
@@ -1,6 +1,11 @@
1
  import eventlet
2
  eventlet.monkey_patch() # Parchea librerías estándar para compatibilidad con eventlet (necesario para Socket.IO)
3
 
 
 
 
 
 
4
  import os
5
  import fcntl
6
  import json
@@ -360,11 +365,16 @@ except:
360
  # URL del proxy de Google Script
361
  GOOGLE_PROXY_URL = os.getenv("GOOGLE_PROXY_URL") or "https://script.google.com/macros/s/AKfycbz7z1Jb0vsur42GmmqrL3PVXeRkN2WxSojFDIleEDoLOg6MnrmJjb_uuPcQ15CTwyzD/exec"
362
 
363
- if TG_TOKEN and GOOGLE_PROXY_URL:
364
- base_url = GOOGLE_PROXY_URL.split('?')[0]
365
- telebot.apihelper.API_URL = base_url + "?path={1}&token={0}"
366
- telebot.apihelper.CONNECT_TIMEOUT = 60
367
- telebot.apihelper.READ_TIMEOUT = 60
 
 
 
 
 
368
 
369
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
370
 
@@ -498,10 +508,13 @@ def start_bot_thread():
498
  print("[BOT] Iniciando polling...")
499
  while True:
500
  try:
501
- bot.infinity_polling(timeout=20, long_polling_timeout=10)
 
 
502
  except Exception as e:
503
  print(f"[BOT ERROR] {e}")
504
- time.sleep(30)
 
505
 
506
  if bot:
507
  threading.Thread(target=start_bot_thread, daemon=True).start()
 
1
  import eventlet
2
  eventlet.monkey_patch() # Parchea librerías estándar para compatibilidad con eventlet (necesario para Socket.IO)
3
 
4
+ import logging
5
+ # Silenciar logs de error de socket.io que son ruido (Bad file descriptor)
6
+ logging.getLogger('engineio.server').setLevel(logging.CRITICAL)
7
+ logging.getLogger('socketio.server').setLevel(logging.CRITICAL)
8
+
9
  import os
10
  import fcntl
11
  import json
 
365
  # URL del proxy de Google Script
366
  GOOGLE_PROXY_URL = os.getenv("GOOGLE_PROXY_URL") or "https://script.google.com/macros/s/AKfycbz7z1Jb0vsur42GmmqrL3PVXeRkN2WxSojFDIleEDoLOg6MnrmJjb_uuPcQ15CTwyzD/exec"
367
 
368
+ if TG_TOKEN:
369
+ if GOOGLE_PROXY_URL:
370
+ print("[BOT] Usando Google Proxy URL")
371
+ base_url = GOOGLE_PROXY_URL.split('?')[0]
372
+ telebot.apihelper.API_URL = base_url + "?path={1}&token={0}"
373
+ else:
374
+ print("[BOT] Usando conexión directa a Telegram")
375
+
376
+ telebot.apihelper.CONNECT_TIMEOUT = 30
377
+ telebot.apihelper.READ_TIMEOUT = 30
378
 
379
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
380
 
 
508
  print("[BOT] Iniciando polling...")
509
  while True:
510
  try:
511
+ # Incrementar timeout para reducir peticiones al proxy
512
+ # long_polling_timeout es el tiempo que espera el servidor de Telegram
513
+ bot.infinity_polling(timeout=60, long_polling_timeout=30)
514
  except Exception as e:
515
  print(f"[BOT ERROR] {e}")
516
+ # Espera más larga en caso de error para evitar saturar el proxy/cuotas
517
+ time.sleep(60)
518
 
519
  if bot:
520
  threading.Thread(target=start_bot_thread, daemon=True).start()