raghavNCI
commited on
Commit
·
89615e3
1
Parent(s):
de0e189
wa addressing name
Browse files- routes/wa_gateway.py +9 -5
routes/wa_gateway.py
CHANGED
@@ -14,7 +14,7 @@ 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 |
-
def send_preferences_flow(to):
|
18 |
url = f"https://graph.facebook.com/v22.0/714483311743364/messages"
|
19 |
headers = {
|
20 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
@@ -27,7 +27,7 @@ def send_preferences_flow(to):
|
|
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",
|
@@ -71,15 +71,19 @@ async def receive_whatsapp_event(request: Request):
|
|
71 |
changes = entry["changes"][0]
|
72 |
value = changes["value"]
|
73 |
messages = value.get("messages")
|
|
|
74 |
|
75 |
-
if messages:
|
76 |
msg = messages[0]
|
|
|
|
|
77 |
from_number = msg["from"]
|
|
|
78 |
text = msg["text"]["body"]
|
79 |
|
80 |
-
print(f"[INFO] Message from {from_number}: {text}")
|
81 |
|
82 |
-
response = send_preferences_flow(from_number)
|
83 |
|
84 |
if response["status"] == "failed":
|
85 |
print("[ERROR] Failed to send interactive message:", response["error"])
|
|
|
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, name):
|
18 |
url = f"https://graph.facebook.com/v22.0/714483311743364/messages"
|
19 |
headers = {
|
20 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
|
|
27 |
"interactive": {
|
28 |
"type": "list",
|
29 |
"header": { "type": "text", "text": "Select Your Interests" },
|
30 |
+
"body": { "text": f"Hi {name}! Please pick your preferred news topics." },
|
31 |
"footer": { "text": "Powered by nuse" },
|
32 |
"action": {
|
33 |
"button": "Choose Topics",
|
|
|
71 |
changes = entry["changes"][0]
|
72 |
value = changes["value"]
|
73 |
messages = value.get("messages")
|
74 |
+
contacts = value.get("contacts")
|
75 |
|
76 |
+
if messages and contacts:
|
77 |
msg = messages[0]
|
78 |
+
contact = contacts[0]
|
79 |
+
|
80 |
from_number = msg["from"]
|
81 |
+
sender_name = contact["profile"]["name"] # <- 👈 This is the name
|
82 |
text = msg["text"]["body"]
|
83 |
|
84 |
+
print(f"[INFO] Message from {sender_name} ({from_number}): {text}")
|
85 |
|
86 |
+
response = send_preferences_flow(from_number, sender_name)
|
87 |
|
88 |
if response["status"] == "failed":
|
89 |
print("[ERROR] Failed to send interactive message:", response["error"])
|