raghavNCI
commited on
Commit
·
ecbc095
1
Parent(s):
493ea8d
wa gateway
Browse files- app.py +2 -0
- routes/wa_gateway.py +71 -0
app.py
CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
2 |
from routes.category import router # routes.py must be in same folder
|
3 |
from routes.question import askMe
|
4 |
from routes.headlines import headlines
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
from cache_init import fetch_and_cache_articles
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
@@ -13,6 +14,7 @@ app = FastAPI()
|
|
13 |
app.include_router(router)
|
14 |
app.include_router(askMe)
|
15 |
app.include_router(headlines)
|
|
|
16 |
|
17 |
app.add_middleware(
|
18 |
CORSMiddleware,
|
|
|
2 |
from routes.category import router # routes.py must be in same folder
|
3 |
from routes.question import askMe
|
4 |
from routes.headlines import headlines
|
5 |
+
from routes.wa_gateway import wa_router
|
6 |
from dotenv import load_dotenv
|
7 |
from cache_init import fetch_and_cache_articles
|
8 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
14 |
app.include_router(router)
|
15 |
app.include_router(askMe)
|
16 |
app.include_router(headlines)
|
17 |
+
app.include_router(wa_router)
|
18 |
|
19 |
app.add_middleware(
|
20 |
CORSMiddleware,
|
routes/wa_gateway.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app/routes/wa_gateway.py
|
2 |
+
from fastapi import APIRouter
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from fastapi.responses import JSONResponse
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
wa_router = APIRouter()
|
11 |
+
|
12 |
+
ACCESS_TOKEN = os.getenv("WHATSAPP_ACCESS_TOKEN")
|
13 |
+
PHONE_NUMBER_ID = os.getenv("WHATSAPP_PHONE_NUMBER_ID")
|
14 |
+
RECIPIENT_NUMBER = "+353 89 949 5777" # or pass dynamically
|
15 |
+
|
16 |
+
@wa_router.get("/webhook")
|
17 |
+
async def verify_webhook(request: Request):
|
18 |
+
params = request.query_params
|
19 |
+
if params.get("hub.mode") == "subscribe" and params.get("hub.verify_token") == ACCESS_TOKEN:
|
20 |
+
return JSONResponse(content=params.get("hub.challenge"))
|
21 |
+
return JSONResponse(status_code=403, content={"error": "Verification failed"})
|
22 |
+
|
23 |
+
@wa_router.post("/webhook")
|
24 |
+
async def receive_whatsapp_event(request: Request):
|
25 |
+
body = await request.json()
|
26 |
+
print("[WEBHOOK] Incoming message:", body)
|
27 |
+
|
28 |
+
# Detect message and extract user number + message
|
29 |
+
try:
|
30 |
+
entry = body["entry"][0]
|
31 |
+
changes = entry["changes"][0]
|
32 |
+
value = changes["value"]
|
33 |
+
messages = value.get("messages")
|
34 |
+
|
35 |
+
if messages:
|
36 |
+
msg = messages[0]
|
37 |
+
from_number = msg["from"]
|
38 |
+
text = msg["text"]["body"]
|
39 |
+
|
40 |
+
print(f"[INFO] Message from {from_number}: {text}")
|
41 |
+
|
42 |
+
# Trigger your response logic here — for example, reply with interactive message
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
print("[ERROR] Webhook processing failed:", str(e))
|
46 |
+
|
47 |
+
return JSONResponse(content={"status": "received"})
|
48 |
+
|
49 |
+
@wa_router.post("/whatsapp/send")
|
50 |
+
def send_whatsapp_message():
|
51 |
+
url = f"https://graph.facebook.com/v17.0/{PHONE_NUMBER_ID}/messages"
|
52 |
+
headers = {
|
53 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
54 |
+
"Content-Type": "application/json"
|
55 |
+
}
|
56 |
+
payload = {
|
57 |
+
"messaging_product": "whatsapp",
|
58 |
+
"to": RECIPIENT_NUMBER,
|
59 |
+
"type": "template",
|
60 |
+
"template": {
|
61 |
+
"name": "hello_world",
|
62 |
+
"language": { "code": "en_US" }
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
response = requests.post(url, headers=headers, json=payload)
|
67 |
+
try:
|
68 |
+
response.raise_for_status()
|
69 |
+
return {"status": "sent", "response": response.json()}
|
70 |
+
except Exception as e:
|
71 |
+
return {"status": "failed", "error": str(e)}
|