Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,74 @@
|
|
|
|
1 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
# Define the API URL
|
4 |
-
url = "https://api.puter.com/signup"
|
5 |
-
|
6 |
-
# Define the headers (including all the details you provided)
|
7 |
-
headers = {
|
8 |
-
"accept": "*/*",
|
9 |
-
"accept-encoding": "gzip, deflate, br, zstd",
|
10 |
-
"accept-language": "en-US,en;q=0.9",
|
11 |
-
"connection": "keep-alive",
|
12 |
-
"content-length": "130", # This will be automatically calculated by `requests`
|
13 |
-
"content-type": "application/json",
|
14 |
-
"host": "api.puter.com",
|
15 |
-
"origin": "https://dooratre-yjytjyjtyjtyjtjytj.static.hf.space",
|
16 |
-
"referer": "https://dooratre-yjytjyjtyjtyjtjytj.static.hf.space/",
|
17 |
-
"sec-ch-ua": '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
|
18 |
-
"sec-ch-ua-mobile": "?0",
|
19 |
-
"sec-ch-ua-platform": '"Windows"',
|
20 |
-
"sec-fetch-dest": "empty",
|
21 |
-
"sec-fetch-mode": "cors",
|
22 |
-
"sec-fetch-site": "cross-site",
|
23 |
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
|
24 |
-
"x-requested-with": "XMLHttpRequest"
|
25 |
-
}
|
26 |
-
|
27 |
-
# Define the payload (request body)
|
28 |
-
payload = {
|
29 |
-
"username": "tdpf6hhhhhtrgrtghhhhhhhQtGu9g",
|
30 |
-
"email": "[email protected]",
|
31 |
-
"password": "jH%K7RQz7t4@C",
|
32 |
-
"send_confirmation_code": True,
|
33 |
-
"p102xyzname": ""
|
34 |
-
}
|
35 |
-
|
36 |
-
# Send the POST request
|
37 |
-
response = requests.post(url, headers=headers, json=payload)
|
38 |
-
|
39 |
-
# Check if the request was successful (status code 200)
|
40 |
-
if response.status_code == 200:
|
41 |
try:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Replace with your Facebook Page Access Token
|
8 |
+
PAGE_ACCESS_TOKEN = "YOUR_PAGE_ACCESS_TOKEN"
|
9 |
+
|
10 |
+
# Your AI API function
|
11 |
+
def gpt4o_ai(user_input, chat_history):
|
12 |
+
url = "https://corvo-ai-alex-x.hf.space/chat" # URL of the chat endpoint
|
13 |
+
headers = {"Content-Type": "application/json"}
|
14 |
+
|
15 |
+
payload = {
|
16 |
+
"user_input": user_input,
|
17 |
+
"chat_history": chat_history
|
18 |
+
}
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
22 |
+
response.raise_for_status()
|
23 |
+
response_data = response.json()
|
24 |
+
assistant_response = response_data.get("assistant_response", "No response received.")
|
25 |
+
return assistant_response
|
26 |
+
except Exception as err:
|
27 |
+
return f"An error occurred: {err}"
|
28 |
+
|
29 |
+
# Endpoint to handle Facebook Messenger webhooks
|
30 |
+
@app.route("/webhook", methods=["GET"])
|
31 |
+
def verify_webhook():
|
32 |
+
# Facebook verification challenge
|
33 |
+
mode = request.args.get("hub.mode")
|
34 |
+
token = request.args.get("hub.verify_token")
|
35 |
+
challenge = request.args.get("hub.challenge")
|
36 |
+
|
37 |
+
if mode == "subscribe" and token == "YOUR_VERIFY_TOKEN":
|
38 |
+
return challenge
|
39 |
+
else:
|
40 |
+
return "Verification failed", 403
|
41 |
+
|
42 |
+
@app.route("/webhook", methods=["POST"])
|
43 |
+
def handle_message():
|
44 |
+
# Parse incoming message from Facebook
|
45 |
+
body = request.get_json()
|
46 |
+
print(body)
|
47 |
+
|
48 |
+
for entry in body["entry"]:
|
49 |
+
for messaging_event in entry["messaging"]:
|
50 |
+
if "message" in messaging_event:
|
51 |
+
sender_id = messaging_event["sender"]["id"]
|
52 |
+
message_text = messaging_event["message"].get("text", "")
|
53 |
+
|
54 |
+
# Call your AI API
|
55 |
+
chat_history = [] # Maintain chat history if needed
|
56 |
+
ai_response = gpt4o_ai(message_text, chat_history)
|
57 |
+
|
58 |
+
# Send the AI response back to Facebook Messenger
|
59 |
+
send_message(sender_id, ai_response)
|
60 |
+
|
61 |
+
return "OK", 200
|
62 |
+
|
63 |
+
# Function to send a message back to Facebook Messenger
|
64 |
+
def send_message(recipient_id, message_text):
|
65 |
+
url = f"https://graph.facebook.com/v16.0/me/messages?access_token={PAGE_ACCESS_TOKEN}"
|
66 |
+
payload = {
|
67 |
+
"recipient": {"id": recipient_id},
|
68 |
+
"message": {"text": message_text}
|
69 |
+
}
|
70 |
+
response = requests.post(url, json=payload)
|
71 |
+
print(response.json())
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
app.run(port=7680, debug=True)
|