raghavNCI
commited on
Commit
·
4a6e3e3
1
Parent(s):
047bda7
wa preference flow
Browse files- routes/wa_gateway.py +40 -0
routes/wa_gateway.py
CHANGED
@@ -14,6 +14,44 @@ ACCESS_TOKEN = os.getenv("WHATSAPP_ACCESS_TOKEN")
|
|
14 |
PHONE_NUMBER_ID = os.getenv("WHATSAPP_PHONE_NUMBER_ID")
|
15 |
RECIPIENT_NUMBER = "+353 89 949 5777" # or pass dynamically
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@wa_router.get("/webhook")
|
18 |
async def verify_webhook(request: Request):
|
19 |
params = request.query_params
|
@@ -41,6 +79,8 @@ async def receive_whatsapp_event(request: Request):
|
|
41 |
|
42 |
print(f"[INFO] Message from {from_number}: {text}")
|
43 |
|
|
|
|
|
44 |
# Trigger your response logic here — for example, reply with interactive message
|
45 |
|
46 |
except Exception as e:
|
|
|
14 |
PHONE_NUMBER_ID = os.getenv("WHATSAPP_PHONE_NUMBER_ID")
|
15 |
RECIPIENT_NUMBER = "+353 89 949 5777" # or pass dynamically
|
16 |
|
17 |
+
def send_preferences_flow(to):
|
18 |
+
url = f"https://graph.facebook.com/v17.0/{PHONE_NUMBER_ID}/messages"
|
19 |
+
headers = {
|
20 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
21 |
+
"Content-Type": "application/json"
|
22 |
+
}
|
23 |
+
payload = {
|
24 |
+
"messaging_product": "whatsapp",
|
25 |
+
"to": to,
|
26 |
+
"type": "interactive",
|
27 |
+
"interactive": {
|
28 |
+
"type": "list",
|
29 |
+
"header": { "type": "text", "text": "Select Your Interests" },
|
30 |
+
"body": { "text": "Please pick your preferred news topics." },
|
31 |
+
"footer": { "text": "Powered by nuse" },
|
32 |
+
"action": {
|
33 |
+
"button": "Choose Topics",
|
34 |
+
"sections": [{
|
35 |
+
"title": "Categories",
|
36 |
+
"rows": [
|
37 |
+
{ "id": "world", "title": "World" },
|
38 |
+
{ "id": "india", "title": "India" },
|
39 |
+
{ "id": "finance", "title": "Finance" },
|
40 |
+
{ "id": "sports", "title": "Sports" },
|
41 |
+
{ "id": "entertainment", "title": "Entertainment" }
|
42 |
+
]
|
43 |
+
}]
|
44 |
+
}
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
response = requests.post(url, headers=headers, json=payload)
|
49 |
+
try:
|
50 |
+
response.raise_for_status()
|
51 |
+
return {"status": "sent", "response": response.json()}
|
52 |
+
except Exception as e:
|
53 |
+
return {"status": "failed", "error": str(e)}
|
54 |
+
|
55 |
@wa_router.get("/webhook")
|
56 |
async def verify_webhook(request: Request):
|
57 |
params = request.query_params
|
|
|
79 |
|
80 |
print(f"[INFO] Message from {from_number}: {text}")
|
81 |
|
82 |
+
send_preferences_flow(from_number)
|
83 |
+
|
84 |
# Trigger your response logic here — for example, reply with interactive message
|
85 |
|
86 |
except Exception as e:
|