Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,52 +70,51 @@ conversation_start_times: Dict[str, str] = {}
|
|
| 70 |
# 🤖 FONCTION D'ENVOI TÉLÉGRAM
|
| 71 |
# ======================================================================
|
| 72 |
|
| 73 |
-
def
|
| 74 |
"""
|
| 75 |
-
|
| 76 |
-
C'est la fonction principale pour l'archivage.
|
| 77 |
"""
|
| 78 |
if not TELEGRAM_NOTIFICATIONS_ENABLED:
|
| 79 |
return
|
| 80 |
|
| 81 |
-
# Construction du message formaté
|
| 82 |
-
MESSAGE = f"""
|
| 83 |
-
*🔔 Nouvelle Interaction LLM 🔔*
|
| 84 |
-
*Session ID:* `{session_id}`
|
| 85 |
-
*Heure:* {time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 86 |
-
|
| 87 |
-
*Question (Utilisateur):*
|
| 88 |
-
{question}
|
| 89 |
-
|
| 90 |
-
*Réponse (LLM - Début):*
|
| 91 |
-
{reponse_llm[:200]}... (Voir le log pour la réponse complète)
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
# Construction de l'URL pour la méthode 'sendMessage'
|
| 95 |
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
| 96 |
-
|
| 97 |
-
# Paramètres de la requête
|
| 98 |
params = {
|
| 99 |
"chat_id": chat_id,
|
| 100 |
-
"text":
|
| 101 |
"parse_mode": "Markdown"
|
| 102 |
}
|
| 103 |
|
| 104 |
try:
|
| 105 |
-
# Envoi de la requête POST (avec un timeout pour ne pas ralentir l'API principale)
|
| 106 |
response = requests.post(url, params=params, timeout=5)
|
| 107 |
response.raise_for_status()
|
| 108 |
|
| 109 |
if response.json().get("ok"):
|
| 110 |
-
|
| 111 |
-
print(f"✅ Notification Telegram envoyée pour la session {session_id}.")
|
| 112 |
else:
|
| 113 |
print(f"❌ Échec envoi Telegram: {response.json().get('description')}")
|
| 114 |
|
| 115 |
except requests.exceptions.RequestException as e:
|
| 116 |
-
print(f"❌ Erreur connexion Telegram (Vérifiez le TOKEN/Réseau): {e}")
|
| 117 |
except Exception as e:
|
| 118 |
-
print(f"❌ Erreur inattendue Telegram: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
# ======================================================================
|
|
@@ -408,7 +407,6 @@ def api_get_answer():
|
|
| 408 |
try:
|
| 409 |
data = request.get_json()
|
| 410 |
query_text = data.get('query_text')
|
| 411 |
-
# Utiliser un ID par défaut si non fourni
|
| 412 |
session_id = data.get('session_id', 'archive')
|
| 413 |
|
| 414 |
if not query_text:
|
|
@@ -466,6 +464,11 @@ def api_clear_history():
|
|
| 466 |
if __name__ == '__main__':
|
| 467 |
print("start app.py")
|
| 468 |
if initialize_global_resources():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
try:
|
| 471 |
import socket
|
|
|
|
| 70 |
# 🤖 FONCTION D'ENVOI TÉLÉGRAM
|
| 71 |
# ======================================================================
|
| 72 |
|
| 73 |
+
def send_telegram_message(message: str, token: str, chat_id: str):
|
| 74 |
"""
|
| 75 |
+
Fonction générique pour envoyer un message à Telegram.
|
|
|
|
| 76 |
"""
|
| 77 |
if not TELEGRAM_NOTIFICATIONS_ENABLED:
|
| 78 |
return
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
|
|
|
|
|
|
| 81 |
params = {
|
| 82 |
"chat_id": chat_id,
|
| 83 |
+
"text": message,
|
| 84 |
"parse_mode": "Markdown"
|
| 85 |
}
|
| 86 |
|
| 87 |
try:
|
|
|
|
| 88 |
response = requests.post(url, params=params, timeout=5)
|
| 89 |
response.raise_for_status()
|
| 90 |
|
| 91 |
if response.json().get("ok"):
|
| 92 |
+
print(f"✅ Message Telegram envoyé.")
|
|
|
|
| 93 |
else:
|
| 94 |
print(f"❌ Échec envoi Telegram: {response.json().get('description')}")
|
| 95 |
|
| 96 |
except requests.exceptions.RequestException as e:
|
| 97 |
+
print(f"❌ Erreur connexion Telegram (Vérifiez le TOKEN/Réseau) : {e}")
|
| 98 |
except Exception as e:
|
| 99 |
+
print(f"❌ Erreur inattendue Telegram : {e}")
|
| 100 |
+
|
| 101 |
+
def send_llm_interaction_to_telegram(question: str, reponse_llm: str, session_id: str, token: str, chat_id: str):
|
| 102 |
+
"""
|
| 103 |
+
Construit le message d'interaction Q/R et l'envoie via Telegram.
|
| 104 |
+
"""
|
| 105 |
+
# Construction du message formaté
|
| 106 |
+
MESSAGE = f"""
|
| 107 |
+
*🔔 Nouvelle Interaction LLM 🔔*
|
| 108 |
+
*Session ID:* `{session_id}`
|
| 109 |
+
*Heure:* {time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 110 |
+
|
| 111 |
+
*Question (Utilisateur):*
|
| 112 |
+
{question}
|
| 113 |
+
|
| 114 |
+
*Réponse (LLM - Début):*
|
| 115 |
+
{reponse_llm[:200]}... (Voir le log pour la réponse complète)
|
| 116 |
+
"""
|
| 117 |
+
send_telegram_message(MESSAGE, token, chat_id)
|
| 118 |
|
| 119 |
|
| 120 |
# ======================================================================
|
|
|
|
| 407 |
try:
|
| 408 |
data = request.get_json()
|
| 409 |
query_text = data.get('query_text')
|
|
|
|
| 410 |
session_id = data.get('session_id', 'archive')
|
| 411 |
|
| 412 |
if not query_text:
|
|
|
|
| 464 |
if __name__ == '__main__':
|
| 465 |
print("start app.py")
|
| 466 |
if initialize_global_resources():
|
| 467 |
+
|
| 468 |
+
# ➡️ ENVOI DE LA NOTIFICATION TÉLÉGRAM AU DÉMARRAGE
|
| 469 |
+
startup_message = f"🚀 **Application RAG Démarrée**\nHeure: {time.strftime('%Y-%m-%d %H:%M:%S')}\nToutes les ressources sont chargées."
|
| 470 |
+
send_telegram_message(startup_message, TELEGRAM_TOKEN, TELEGRAM_CHAT_ID)
|
| 471 |
+
# ------------------------------------------------------
|
| 472 |
|
| 473 |
try:
|
| 474 |
import socket
|