Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import requests
|
4 |
+
from fastapi import FastAPI, Request
|
5 |
+
from fastapi.responses import PlainTextResponse
|
6 |
+
|
7 |
+
# Initialize FastAPI (used by Gradio Spaces under the hood)
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load credentials from environment variables
|
11 |
+
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
12 |
+
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
13 |
+
TWILIO_SID = os.environ["TWILIO_SID"]
|
14 |
+
TWILIO_AUTH_TOKEN = os.environ["TWILIO_AUTH_TOKEN"]
|
15 |
+
TWILIO_NUMBER = "whatsapp:+14155238886" # Twilio sandbox number
|
16 |
+
|
17 |
+
@app.post("/webhook")
|
18 |
+
async def receive_message(request: Request):
|
19 |
+
form_data = await request.form()
|
20 |
+
user_input = form_data.get("Body")
|
21 |
+
user_number = form_data.get("From")
|
22 |
+
|
23 |
+
# 1. Create a new assistant thread
|
24 |
+
thread = requests.post(
|
25 |
+
"https://api.openai.com/v1/threads",
|
26 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
27 |
+
).json()
|
28 |
+
thread_id = thread["id"]
|
29 |
+
|
30 |
+
# 2. Send user message
|
31 |
+
requests.post(
|
32 |
+
f"https://api.openai.com/v1/threads/{thread_id}/messages",
|
33 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"},
|
34 |
+
json={"role": "user", "content": user_input}
|
35 |
+
)
|
36 |
+
|
37 |
+
# 3. Run assistant
|
38 |
+
run = requests.post(
|
39 |
+
f"https://api.openai.com/v1/threads/{thread_id}/runs",
|
40 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"},
|
41 |
+
json={"assistant_id": ASSISTANT_ID}
|
42 |
+
).json()
|
43 |
+
|
44 |
+
# 4. Wait for run to complete
|
45 |
+
while True:
|
46 |
+
run_status = requests.get(
|
47 |
+
f"https://api.openai.com/v1/threads/{thread_id}/runs/{run['id']}",
|
48 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
49 |
+
).json()
|
50 |
+
if run_status["status"] == "completed":
|
51 |
+
break
|
52 |
+
time.sleep(2)
|
53 |
+
|
54 |
+
# 5. Fetch response
|
55 |
+
messages = requests.get(
|
56 |
+
f"https://api.openai.com/v1/threads/{thread_id}/messages",
|
57 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
58 |
+
).json()
|
59 |
+
reply = messages["data"][0]["content"][0]["text"]["value"]
|
60 |
+
|
61 |
+
# 6. Send reply via Twilio back to WhatsApp
|
62 |
+
requests.post(
|
63 |
+
f"https://api.twilio.com/2010-04-01/Accounts/{TWILIO_SID}/Messages.json",
|
64 |
+
auth=(TWILIO_SID, TWILIO_AUTH_TOKEN),
|
65 |
+
data={
|
66 |
+
"From": TWILIO_NUMBER,
|
67 |
+
"To": user_number,
|
68 |
+
"Body": reply
|
69 |
+
}
|
70 |
+
)
|
71 |
+
|
72 |
+
return PlainTextResponse("OK")
|