Spaces:
Configuration error
Configuration error
Upload 2 files
Browse files- app.py +75 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from telegram import Update
|
| 4 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 5 |
+
|
| 6 |
+
# --- ضع مفاتيحك السرية هنا مباشرة ---
|
| 7 |
+
# =================================================================
|
| 8 |
+
TELEGRAM_TOKEN = "4f3RezB0UeVmGoyTHptvsnhFAc"
|
| 9 |
+
OPENROUTER_API_KEY = "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"
|
| 10 |
+
# =================================================================
|
| 11 |
+
|
| 12 |
+
# يمكنك تغيير النموذج الافتراضي هنا في أي وقت
|
| 13 |
+
DEFAULT_MODEL = "google/gemma-7b-it:free"
|
| 14 |
+
|
| 15 |
+
# ذاكرة لحفظ سجل المحادثات لكل مستخدم على حدة
|
| 16 |
+
user_conversations = {}
|
| 17 |
+
|
| 18 |
+
# --- دوال البوت ---
|
| 19 |
+
|
| 20 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
| 21 |
+
"""يرسل رسالة ترحيبية."""
|
| 22 |
+
user_id = update.message.from_user.id
|
| 23 |
+
user_conversations[user_id] = []
|
| 24 |
+
await update.message.reply_text("مرحباً! أنا بوت الذكاء الاصطناعي الخاص بهاشم (نسخة نصية). أرسل لي أي سؤال وسأجيب عليه.")
|
| 25 |
+
|
| 26 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
| 27 |
+
"""يعالج الرسائل النصية الواردة."""
|
| 28 |
+
user_id = update.message.from_user.id
|
| 29 |
+
user_message = update.message.text
|
| 30 |
+
|
| 31 |
+
if user_id not in user_conversations:
|
| 32 |
+
user_conversations[user_id] = []
|
| 33 |
+
|
| 34 |
+
user_conversations[user_id].append({"role": "user", "content": user_message})
|
| 35 |
+
|
| 36 |
+
thinking_message = await update.message.reply_text("أفكر...")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = requests.post(
|
| 40 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 41 |
+
headers={
|
| 42 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 43 |
+
"Content-Type": "application/json"
|
| 44 |
+
},
|
| 45 |
+
json={
|
| 46 |
+
"model": DEFAULT_MODEL,
|
| 47 |
+
"messages": user_conversations[user_id]
|
| 48 |
+
}
|
| 49 |
+
)
|
| 50 |
+
response.raise_for_status()
|
| 51 |
+
data = response.json()
|
| 52 |
+
bot_response = data['choices'][0]['message']['content']
|
| 53 |
+
|
| 54 |
+
user_conversations[user_id].append({"role": "assistant", "content": bot_response})
|
| 55 |
+
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=bot_response)
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
error_message = f"عذراً، حدث خطأ: {e}"
|
| 59 |
+
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=error_message)
|
| 60 |
+
|
| 61 |
+
def main() -> None:
|
| 62 |
+
"""الدالة الرئيسية لتشغيل البوت."""
|
| 63 |
+
if not TELEGRAM_TOKEN or not OPENROUTER_API_KEY:
|
| 64 |
+
print("خطأ فادح: يرجى التأكد من وضع التوكن والمفتاح في بداية الملف.")
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
application = Application.builder().token(TELEGRAM_TOKEN).build()
|
| 68 |
+
application.add_handler(CommandHandler("start", start))
|
| 69 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 70 |
+
|
| 71 |
+
print("البوت النصي بدأ بالعمل...")
|
| 72 |
+
application.run_polling()
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-telegram-bot
|
| 2 |
+
requests
|
| 3 |
+
python-dotenv
|