Edoruin commited on
Commit
b1bc7d6
·
1 Parent(s): 857443a

solution telegram connection issues

Browse files
Files changed (1) hide show
  1. app/main.py +12 -21
app/main.py CHANGED
@@ -61,12 +61,13 @@ def main(page: ft.Page):
61
  GIT_TOKEN = os.getenv("GITLAB_TOKEN")
62
  GIT_GROUP = os.getenv("GITLAB_GROUP_ID")
63
 
64
- # Intentar usar un Mirror para el Bot Polling (evita el bloqueo de api.telegram.org)
65
  if TG_TOKEN:
66
- # Usamos un mirror común que suele estar desbloqueado
67
- telebot.apihelper.API_URL = "https://tpa.coronawarn.app/bot{0}/{1}"
68
-
69
- # Inicializar bot (esto no fallará aquí, solo crea la instancia)
 
70
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
71
 
72
  def mostrar_notificacion(texto, color="blue"):
@@ -76,30 +77,20 @@ def main(page: ft.Page):
76
  page.update()
77
 
78
  def enviar_telegram_con_botones(loan_id, mensaje):
79
- chat_id = str(os.getenv("TELEGRAM_CHAT_ID", "")).strip()
80
- if not TG_TOKEN or not chat_id:
81
- return "ERR_TOKEN"
82
 
83
  markup = types.InlineKeyboardMarkup()
84
  btn_aceptar = types.InlineKeyboardButton("✅ Aceptar", callback_data=f"accept_{loan_id}")
85
  btn_declinar = types.InlineKeyboardButton("❌ Declinar", callback_data=f"decline_{loan_id}")
86
  markup.add(btn_aceptar, btn_declinar)
87
 
88
- # Enviar vía Proxy de Google para evitar bloqueos de red
89
- payload = {
90
- "chat_id": chat_id,
91
- "text": mensaje,
92
- "reply_markup": json.loads(markup.to_json()),
93
- "parse_mode": "Markdown"
94
- }
95
-
96
  try:
97
- response = requests.post(GOOGLE_PROXY_URL, json=payload, timeout=15)
98
- if response.status_code == 200:
99
- return "OK"
100
- return f"ERR_PROXY_{response.status_code}"
101
  except Exception as e:
102
- print(f"Error Proxy: {e}")
103
  return "ERR_CONN"
104
 
105
  # --- BOT CALLBACK HANDLER ---
 
61
  GIT_TOKEN = os.getenv("GITLAB_TOKEN")
62
  GIT_GROUP = os.getenv("GITLAB_GROUP_ID")
63
 
64
+ # --- CONFIGURACIÓN DE PROXY PARA TODO EL BOT ---
65
  if TG_TOKEN:
66
+ # Redirigimos todas las llamadas de la librería al Proxy de Google
67
+ # Importante: El script de Google debe ser capaz de procesar el PATH o el TOKEN
68
+ telebot.apihelper.API_URL = GOOGLE_PROXY_URL + "?path={1}&token={0}"
69
+
70
+ # Inicializar bot
71
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
72
 
73
  def mostrar_notificacion(texto, color="blue"):
 
77
  page.update()
78
 
79
  def enviar_telegram_con_botones(loan_id, mensaje):
80
+ if not bot or not TG_CHAT_ID:
81
+ return "ERR_CONFIG"
 
82
 
83
  markup = types.InlineKeyboardMarkup()
84
  btn_aceptar = types.InlineKeyboardButton("✅ Aceptar", callback_data=f"accept_{loan_id}")
85
  btn_declinar = types.InlineKeyboardButton("❌ Declinar", callback_data=f"decline_{loan_id}")
86
  markup.add(btn_aceptar, btn_declinar)
87
 
 
 
 
 
 
 
 
 
88
  try:
89
+ # Ahora bot.send_message usará automáticamente el Proxy de Google
90
+ bot.send_message(TG_CHAT_ID, mensaje, reply_markup=markup, parse_mode="Markdown")
91
+ return "OK"
 
92
  except Exception as e:
93
+ print(f"Error enviando via Bot: {e}")
94
  return "ERR_CONN"
95
 
96
  # --- BOT CALLBACK HANDLER ---