Spaces:
Configuration error
Configuration error
File size: 3,242 Bytes
4113431 | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import os
import requests
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
# --- ضع مفاتيحك السرية هنا مباشرة ---
# =================================================================
TELEGRAM_TOKEN = "4f3RezB0UeVmGoyTHptvsnhFAc"
OPENROUTER_API_KEY = "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"
# =================================================================
# يمكنك تغيير النموذج الافتراضي هنا في أي وقت
DEFAULT_MODEL = "google/gemma-7b-it:free"
# ذاكرة لحفظ سجل المحادثات لكل مستخدم على حدة
user_conversations = {}
# --- دوال البوت ---
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""يرسل رسالة ترحيبية."""
user_id = update.message.from_user.id
user_conversations[user_id] = []
await update.message.reply_text("مرحباً! أنا بوت الذكاء الاصطناعي الخاص بهاشم (نسخة نصية). أرسل لي أي سؤال وسأجيب عليه.")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""يعالج الرسائل النصية الواردة."""
user_id = update.message.from_user.id
user_message = update.message.text
if user_id not in user_conversations:
user_conversations[user_id] = []
user_conversations[user_id].append({"role": "user", "content": user_message})
thinking_message = await update.message.reply_text("أفكر...")
try:
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json"
},
json={
"model": DEFAULT_MODEL,
"messages": user_conversations[user_id]
}
)
response.raise_for_status()
data = response.json()
bot_response = data['choices'][0]['message']['content']
user_conversations[user_id].append({"role": "assistant", "content": bot_response})
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=bot_response)
except Exception as e:
error_message = f"عذراً، حدث خطأ: {e}"
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=error_message)
def main() -> None:
"""الدالة الرئيسية لتشغيل البوت."""
if not TELEGRAM_TOKEN or not OPENROUTER_API_KEY:
print("خطأ فادح: يرجى التأكد من وضع التوكن والمفتاح في بداية الملف.")
return
application = Application.builder().token(TELEGRAM_TOKEN).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
print("البوت النصي بدأ بالعمل...")
application.run_polling()
if __name__ == "__main__":
main() |