FbChatbot / app.py
imseldrith's picture
Update app.py
5041c3f verified
raw
history blame
1.54 kB
from flask import Flask, request
import requests
import config
from gemini_ai import get_gemini_response
app = Flask(__name__)
@app.route("/webhook", methods=["GET"])
def verify():
""" تحقق من Facebook Webhook """
if request.args.get("hub.verify_token") == config.VERIFY_TOKEN:
return request.args.get("hub.challenge")
return "خطأ في التحقق", 403
@app.route("/webhook", methods=["POST"])
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)