Spaces:
Runtime error
Runtime error
| import os | |
| import asyncio | |
| import logging | |
| from telegram import Update | |
| from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters | |
| from groq import Groq | |
| logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) | |
| TOKEN = os.environ.get("TELEGRAM_TOKEN") | |
| GROQ_KEY = os.environ.get("GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_KEY) | |
| async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| user_text = update.message.text | |
| print(f"Mensaje recibido: {user_text}") # Esto aparecerá en los logs | |
| try: | |
| completion = client.chat.completions.create( | |
| model="llama3-8b-8192", | |
| messages=[ | |
| {"role": "system", "content": "Eres un asistente de cocreación para el proyecto Argosa."}, | |
| {"role": "user", "content": user_text} | |
| ] | |
| ) | |
| respuesta = completion.choices[0].message.content | |
| await update.message.reply_text(respuesta) | |
| except Exception as e: | |
| error_msg = f"Error al conectar con la IA: {e}" | |
| print(error_msg) | |
| await update.message.reply_text(f"Hola Ricardo, recibí tu mensaje pero tengo este error: {e}") | |
| if __name__ == '__main__': | |
| if not TOKEN or not GROQ_KEY: | |
| print("ERROR: Faltan las llaves en Settings (Secrets)") | |
| else: | |
| app = ApplicationBuilder().token(TOKEN).build() | |
| app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message)) | |
| print("¡Bot despertando!") | |
| app.run_polling() | |