Spaces:
Runtime error
Runtime error
| import os | |
| import logging | |
| from telegram import Update | |
| from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters | |
| from groq import Groq | |
| # --- الربط المباشر مع أسماء المتغيرات في صورتك الأخيرة (Koyeb) --- | |
| TOKEN = os.environ.get("TELEGRAM_TOKEN") | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| # التأكد من أن الكود قرأ المفتاح بنجاح | |
| if not GROQ_API_KEY: | |
| print("⚠️ تنبيه: لم يتم العثور على GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) | |
| async def generate_script(url): | |
| prompt = ( | |
| "أنت خبير محتوى فصيح. حول الرابط لسكريبت 30 ثانية بتنسيق: " | |
| "خُطّاف جذاب، صلب الموضوع، دعوة للتفاعل. اكتب نص الكلام فقط." | |
| ) | |
| completion = client.chat.completions.create( | |
| messages=[{"role": "system", "content": prompt}, {"role": "user", "content": url}], | |
| model="llama-3.3-70b-versatile", | |
| ) | |
| return completion.choices[0].message.content | |
| async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| await update.message.reply_text("🚀 البوت انطلق بنجاح من Koyeb! أرسل الرابط الآن.") | |
| async def handle_msg(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| if "http" in update.message.text.lower(): | |
| status = await update.message.reply_text("⏳ جاري التحليل...") | |
| try: | |
| res = await generate_script(update.message.text) | |
| await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=status.message_id, text=res) | |
| except Exception as e: | |
| await update.message.reply_text(f"❌ خطأ: {str(e)}") | |
| if __name__ == '__main__': | |
| app = ApplicationBuilder().token(TOKEN).build() | |
| app.add_handler(CommandHandler('start', start)) | |
| app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_msg)) | |
| print("✅ البوت يعمل...") | |
| app.run_polling() |