Edoruin commited on
Commit
b80e277
·
1 Parent(s): 8a4f554

antigravity

Browse files
Files changed (1) hide show
  1. app/main.py +18 -12
app/main.py CHANGED
@@ -73,6 +73,14 @@ def main(page: ft.Page):
73
  # Inicializar bot
74
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
75
 
 
 
 
 
 
 
 
 
76
  def mostrar_notificacion(texto, color="blue"):
77
  snack = ft.SnackBar(ft.Text(texto), bgcolor=color)
78
  page.overlay.append(snack)
@@ -81,6 +89,7 @@ def main(page: ft.Page):
81
 
82
  def enviar_telegram_con_botones(loan_id, mensaje):
83
  if not bot or not TG_CHAT_ID:
 
84
  return "ERR_CONFIG"
85
 
86
  markup = types.InlineKeyboardMarkup()
@@ -89,14 +98,14 @@ def main(page: ft.Page):
89
  markup.add(btn_aceptar, btn_declinar)
90
 
91
  try:
 
92
  bot.send_message(TG_CHAT_ID, mensaje, reply_markup=markup, parse_mode="Markdown")
93
  return "OK"
94
  except Exception as e:
95
  error_msg = str(e)
96
- print(f"Error detallado: {error_msg}")
97
- # Si el error contiene 'Max retries', es un problema de red real
98
  if "Max retries" in error_msg:
99
- return f"ERR_DNS: {error_msg[:50]}"
100
  return f"ERR_CONN: {error_msg[:100]}"
101
 
102
  # --- BOT CALLBACK HANDLER ---
@@ -108,13 +117,16 @@ def main(page: ft.Page):
108
  loan_id = call.data.replace("accept_", "")
109
  if loan_mgr.update_status(loan_id, "ACCEPTED"):
110
  bot.answer_callback_query(call.id, "Préstamo Aceptado")
111
- bot.edit_message_text(f"✅ *ACEPTADO*\n{call.message.text}", TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
 
 
112
  mostrar_notificacion(f"Préstamo {loan_id[:8]} ACEPTADO", ft.Colors.GREEN)
113
  elif call.data.startswith("decline_"):
114
  loan_id = call.data.replace("decline_", "")
115
  if loan_mgr.update_status(loan_id, "DECLINED"):
116
  bot.answer_callback_query(call.id, "Préstamo Declinado")
117
- bot.edit_message_text(f"❌ *DECLINADO*\n{call.message.text}", TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
 
118
  mostrar_notificacion(f"Préstamo {loan_id[:8]} DECLINADO", ft.Colors.RED)
119
  page.update()
120
  except Exception as e:
@@ -240,14 +252,8 @@ def main(page: ft.Page):
240
  if not nombre.value or not item.value:
241
  st_txt.value = "⚠️ Completa los datos"; st_txt.color="red"; page.update(); return
242
 
243
- # Escapar caracteres que rompen el Markdown
244
- def escape_md(text):
245
- for char in ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']:
246
- text = text.replace(char, f"\\{char}")
247
- return text
248
-
249
  loan_id = str(uuid.uuid4())
250
- # Solo ponemos en negrita los encabezados fijos
251
  mensaje_tg = (
252
  "🛠 *SOLICITUD DE PRÉSTAMO*\n"
253
  f"👤 {escape_md(nombre.value)}\n"
 
73
  # Inicializar bot
74
  bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
75
 
76
+ # --- AYUDANTE PARA ESCAPAR MARKDOWN (V1) ---
77
+ def escape_md(text):
78
+ if not text: return ""
79
+ # En Markdown V1 solo necesitamos escapar estos caracteres si no son formato
80
+ for char in ['_', '*', '[', '`']:
81
+ text = text.replace(char, f"\\{char}")
82
+ return text
83
+
84
  def mostrar_notificacion(texto, color="blue"):
85
  snack = ft.SnackBar(ft.Text(texto), bgcolor=color)
86
  page.overlay.append(snack)
 
89
 
90
  def enviar_telegram_con_botones(loan_id, mensaje):
91
  if not bot or not TG_CHAT_ID:
92
+ print("Error: Bot o Chat ID no configurados")
93
  return "ERR_CONFIG"
94
 
95
  markup = types.InlineKeyboardMarkup()
 
98
  markup.add(btn_aceptar, btn_declinar)
99
 
100
  try:
101
+ print(f"DEBUG: Enviando mensaje a Telegram (Chat: {TG_CHAT_ID}). Len: {len(mensaje)}")
102
  bot.send_message(TG_CHAT_ID, mensaje, reply_markup=markup, parse_mode="Markdown")
103
  return "OK"
104
  except Exception as e:
105
  error_msg = str(e)
106
+ print(f"DEBUG Error Telegram: {error_msg}")
 
107
  if "Max retries" in error_msg:
108
+ return f"ERR_DNS: {error_msg[:40]}"
109
  return f"ERR_CONN: {error_msg[:100]}"
110
 
111
  # --- BOT CALLBACK HANDLER ---
 
117
  loan_id = call.data.replace("accept_", "")
118
  if loan_mgr.update_status(loan_id, "ACCEPTED"):
119
  bot.answer_callback_query(call.id, "Préstamo Aceptado")
120
+ # Escapar el texto anterior para evitar que caracteres especiales rompan el nuevo mensaje
121
+ nuevo_texto = f"✅ *ACEPTADO*\n{escape_md(call.message.text)}"
122
+ bot.edit_message_text(nuevo_texto, TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
123
  mostrar_notificacion(f"Préstamo {loan_id[:8]} ACEPTADO", ft.Colors.GREEN)
124
  elif call.data.startswith("decline_"):
125
  loan_id = call.data.replace("decline_", "")
126
  if loan_mgr.update_status(loan_id, "DECLINED"):
127
  bot.answer_callback_query(call.id, "Préstamo Declinado")
128
+ nuevo_texto = f"❌ *DECLINADO*\n{escape_md(call.message.text)}"
129
+ bot.edit_message_text(nuevo_texto, TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
130
  mostrar_notificacion(f"Préstamo {loan_id[:8]} DECLINADO", ft.Colors.RED)
131
  page.update()
132
  except Exception as e:
 
252
  if not nombre.value or not item.value:
253
  st_txt.value = "⚠️ Completa los datos"; st_txt.color="red"; page.update(); return
254
 
 
 
 
 
 
 
255
  loan_id = str(uuid.uuid4())
256
+ # Construir mensaje usando el ayudante global
257
  mensaje_tg = (
258
  "🛠 *SOLICITUD DE PRÉSTAMO*\n"
259
  f"👤 {escape_md(nombre.value)}\n"