Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- app.py +42 -0
- config.py +3 -0
- gemini_ai.py +14 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
import requests
|
3 |
+
import config
|
4 |
+
from gemini_ai import get_gemini_response
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
@app.route("/webhook", methods=["GET"])
|
9 |
+
def verify():
|
10 |
+
""" تحقق من Facebook Webhook """
|
11 |
+
if request.args.get("hub.verify_token") == config.VERIFY_TOKEN:
|
12 |
+
return request.args.get("hub.challenge")
|
13 |
+
return "خطأ في التحقق", 403
|
14 |
+
|
15 |
+
@app.route("/webhook", methods=["POST"])
|
16 |
+
def webhook():
|
17 |
+
""" استقبال الرسائل من Facebook Messenger """
|
18 |
+
data = request.get_json()
|
19 |
+
|
20 |
+
if data.get("object") == "page":
|
21 |
+
for entry in data["entry"]:
|
22 |
+
for messaging_event in entry.get("messaging", []):
|
23 |
+
if messaging_event.get("message"):
|
24 |
+
sender_id = messaging_event["sender"]["id"]
|
25 |
+
message_text = messaging_event["message"].get("text", "")
|
26 |
+
|
27 |
+
if message_text:
|
28 |
+
response = get_gemini_response(message_text)
|
29 |
+
send_message(sender_id, response)
|
30 |
+
|
31 |
+
return "ok", 200
|
32 |
+
|
33 |
+
def send_message(recipient_id, text):
|
34 |
+
""" إرسال رسالة إلى المستخدم عبر Facebook Messenger """
|
35 |
+
url = f"https://graph.facebook.com/v13.0/me/messages?access_token={config.PAGE_ACCESS_TOKEN}"
|
36 |
+
headers = {"Content-Type": "application/json"}
|
37 |
+
data = {"recipient": {"id": recipient_id}, "message": {"text": text}}
|
38 |
+
|
39 |
+
requests.post(url, headers=headers, json=data)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
app.run(port=5000, debug=True)
|
config.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
PAGE_ACCESS_TOKEN = "EAARpz9gI6P4BO4chlCCUkMQPRmZC6FlFONV3qFHdKmNZCDc27HZA8EbqEaeWXc9M8kJFk0AjDEtNthJkGkRo6caMSZCYhXxzGtZCPCEJzuHCltaqwVtPtYbRpOXk9wCps38Ev99d49ZCVvu1uykVG0TiMgIxjyyzjwEU1qQTXrjJSVSIxg1uxZAuSh1HiwZCgBdg"
|
2 |
+
VERIFY_TOKEN = "page12345"
|
3 |
+
GEMINI_API_KEY = "AIzaSyBQUhaasVCO9BN0Z6p20IRAE-EQC8QLDTQ"
|
gemini_ai.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import config
|
3 |
+
|
4 |
+
def get_gemini_response(user_input):
|
5 |
+
url = "https://api.google.com/gemini" # ضع الرابط الصحيح هنا
|
6 |
+
headers = {"Authorization": f"Bearer {config.GEMINI_API_KEY}"}
|
7 |
+
payload = {"prompt": user_input}
|
8 |
+
|
9 |
+
response = requests.post(url, headers=headers, json=payload)
|
10 |
+
|
11 |
+
if response.status_code == 200:
|
12 |
+
return response.json().get("response", "لم أفهم ذلك، حاول مرة أخرى!")
|
13 |
+
else:
|
14 |
+
return "حدث خطأ أثناء الاتصال بـ Gemini AI."
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
requests
|