Spaces:
Paused
Paused
File size: 2,375 Bytes
d31519b 710163b d31519b 710163b d31519b 204e70e d31519b 710163b d31519b 710163b d31519b 710163b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # app.py
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from flask import Flask
from threading import Thread
import os
import aiohttp
# === Partie Web pour Hugging Face ===
web_app = Flask(__name__)
@web_app.route('/')
def home():
return """
<h2>🤖 Bot Telegram RAG actif sur Hugging Face</h2>
<p>Chatbot link : <a href="https://t.me/Emfaye01Bot" target="_blank">https://t.me/Emfaye01Bot</a></p>
"""
def keep_alive():
Thread(target=lambda: web_app.run(host="0.0.0.0", port=7860)).start()
# === Partie Bot Telegram ===
API_URL = os.getenv("RAG_API_URL", "https://makhtar7186-telecom-api.hf.space/ask")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"🤖 Bonjour ! Posez-moi une question technique et je vous répondrai grâce à mon moteur RAG IA."
)
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
question = update.message.text
if question.strip():
await update.message.reply_text("🔍 Traitement de votre question...")
try:
async with aiohttp.ClientSession() as session:
async with session.post(API_URL, json={"question": question}) as resp:
if resp.status == 200:
data = await resp.json()
await update.message.reply_text(data["answer"])
else:
await update.message.reply_text("❌ Erreur lors de la requête à l'API.")
except Exception as e:
await update.message.reply_text(f"❌ Une erreur est survenue : {str(e)}")
else:
await update.message.reply_text("⚠️ Veuillez poser une question valide.")
def main():
keep_alive() # ← Démarre un petit serveur web pour Hugging Face
token = os.getenv("TELEGRAM_BOT_TOKEN")
if not token:
print("❌ TELEGRAM_BOT_TOKEN non défini dans les variables d'environnement.")
return
application = Application.builder().token(token).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
print("🤖 Bot Telegram lancé")
application.run_polling()
if __name__ == "__main__":
main()
|