Spaces:
Runtime error
Runtime error
from flask import Flask, request | |
import requests | |
import config | |
from gemini_ai import get_gemini_response | |
app = Flask(__name__) | |
def verify(): | |
""" تحقق من Facebook Webhook """ | |
if request.args.get("hub.verify_token") == config.VERIFY_TOKEN: | |
return request.args.get("hub.challenge") | |
return "خطأ في التحقق", 403 | |
def webhook(): | |
""" استقبال الرسائل من Facebook Messenger """ | |
data = request.get_json() | |
if data.get("object") == "page": | |
for entry in data["entry"]: | |
for messaging_event in entry.get("messaging", []): | |
if messaging_event.get("message"): | |
sender_id = messaging_event["sender"]["id"] | |
message_text = messaging_event["message"].get("text", "") | |
if message_text: | |
response = get_gemini_response(message_text) | |
send_message(sender_id, response) | |
return "ok", 200 | |
def send_message(recipient_id, text): | |
""" إرسال رسالة إلى المستخدم عبر Facebook Messenger """ | |
url = f"https://graph.facebook.com/v13.0/me/messages?access_token={config.PAGE_ACCESS_TOKEN}" | |
headers = {"Content-Type": "application/json"} | |
data = {"recipient": {"id": recipient_id}, "message": {"text": text}} | |
requests.post(url, headers=headers, json=data) | |
if __name__ == "__main__": | |
app.run(port=7860, debug=True) |