solving telegram bot issues
Browse files- app/main.py +71 -0
app/main.py
CHANGED
|
@@ -267,6 +267,17 @@ class UserManager(HFDatasetManager):
|
|
| 267 |
return User(user_data['id'], username) # Changed from User(username, username) to User(user_data['id'], username)
|
| 268 |
return None
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
# --- GESTOR DE AULAS (Persistencia con HF Datasets) ---
|
| 271 |
class ClassroomManager(HFDatasetManager):
|
| 272 |
"""Clase para manejar aulas y estudiantes con persistencia."""
|
|
@@ -834,6 +845,66 @@ def prestamos():
|
|
| 834 |
loans = loan_mgr.get_all()
|
| 835 |
return render_template('prestamos.html', title="Préstamos", loans=loans)
|
| 836 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
# --- CLASSROOM MAKER ---
|
| 838 |
|
| 839 |
@app.route('/miembros')
|
|
|
|
| 267 |
return User(user_data['id'], username) # Changed from User(username, username) to User(user_data['id'], username)
|
| 268 |
return None
|
| 269 |
|
| 270 |
+
def update_password(self, user_id, new_password):
|
| 271 |
+
"""Actualiza la contraseña de un usuario."""
|
| 272 |
+
for user in self.users:
|
| 273 |
+
if user["id"] == user_id:
|
| 274 |
+
user["password"] = generate_password_hash(new_password)
|
| 275 |
+
user["reset_token"] = None
|
| 276 |
+
user["reset_expiry"] = None
|
| 277 |
+
self.save()
|
| 278 |
+
return True
|
| 279 |
+
return False
|
| 280 |
+
|
| 281 |
# --- GESTOR DE AULAS (Persistencia con HF Datasets) ---
|
| 282 |
class ClassroomManager(HFDatasetManager):
|
| 283 |
"""Clase para manejar aulas y estudiantes con persistencia."""
|
|
|
|
| 845 |
loans = loan_mgr.get_all()
|
| 846 |
return render_template('prestamos.html', title="Préstamos", loans=loans)
|
| 847 |
|
| 848 |
+
@app.route('/api/prestamo', methods=['POST'])
|
| 849 |
+
def api_prestamo():
|
| 850 |
+
try:
|
| 851 |
+
data = request.json
|
| 852 |
+
if not data:
|
| 853 |
+
return jsonify({"status": "error", "message": "No data received"}), 400
|
| 854 |
+
|
| 855 |
+
solicitante = data.get('solicitante')
|
| 856 |
+
hora_salida = data.get('hora_salida')
|
| 857 |
+
hora_retorno = data.get('hora_retorno')
|
| 858 |
+
items_list = data.get('items', [])
|
| 859 |
+
|
| 860 |
+
if not solicitante:
|
| 861 |
+
return jsonify({"status": "error", "message": "Solicitante es requerido"}), 400
|
| 862 |
+
|
| 863 |
+
# Formatear items para el campo 'item'
|
| 864 |
+
items_desc = []
|
| 865 |
+
for it in items_list:
|
| 866 |
+
items_desc.append(f"• {it['descripcion']} ({it['cantidad']}) [{it['categoria']}]")
|
| 867 |
+
|
| 868 |
+
full_items_string = "\n".join(items_desc)
|
| 869 |
+
loan_id = str(uuid.uuid4())
|
| 870 |
+
|
| 871 |
+
new_loan = {
|
| 872 |
+
"id": loan_id,
|
| 873 |
+
"Solicitante": solicitante,
|
| 874 |
+
"hora": hora_salida,
|
| 875 |
+
"devolucion": hora_retorno,
|
| 876 |
+
"item": full_items_string,
|
| 877 |
+
"status_loan": "PENDING",
|
| 878 |
+
"timestamp": datetime.datetime.now().isoformat()
|
| 879 |
+
}
|
| 880 |
+
|
| 881 |
+
loan_mgr.add_loan(new_loan)
|
| 882 |
+
|
| 883 |
+
# Notificar a Telegram
|
| 884 |
+
if bot and TG_CHAT_ID:
|
| 885 |
+
try:
|
| 886 |
+
msg = f"📦 *NUEVA SOLICITUD DE PRÉSTAMO*\n\n"
|
| 887 |
+
msg += f"👤 *Solicitante:* {escape_md(solicitante)}\n"
|
| 888 |
+
msg += f"🕒 *Horario:* {hora_salida} - {hora_retorno}\n"
|
| 889 |
+
msg += f"🛠 *Herramientas:*\n{escape_md(full_items_string)}"
|
| 890 |
+
|
| 891 |
+
markup = types.InlineKeyboardMarkup()
|
| 892 |
+
markup.add(
|
| 893 |
+
types.InlineKeyboardButton("✅ ACEPTAR", callback_data=f"accept_{loan_id}"),
|
| 894 |
+
types.InlineKeyboardButton("❌ DECLINAR", callback_data=f"decline_{loan_id}")
|
| 895 |
+
)
|
| 896 |
+
bot.send_message(TG_CHAT_ID, msg, reply_markup=markup, parse_mode="Markdown")
|
| 897 |
+
except Exception as tg_e:
|
| 898 |
+
print(f"Error TG Loan: {tg_e}")
|
| 899 |
+
|
| 900 |
+
# Emitir notificación por socket
|
| 901 |
+
socketio.emit('notification', {"text": f"Nueva solicitud de {solicitante}", "color": "orange"})
|
| 902 |
+
|
| 903 |
+
return jsonify({"status": "success", "loan_id": loan_id})
|
| 904 |
+
except Exception as e:
|
| 905 |
+
print(f"API Prestamo Error: {e}")
|
| 906 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
| 907 |
+
|
| 908 |
# --- CLASSROOM MAKER ---
|
| 909 |
|
| 910 |
@app.route('/miembros')
|