from fastapi import FastAPI, Request, HTTPException,APIRouter from pydantic import BaseModel webhook_router = APIRouter(tags=["Webhooks"]) WEBHOOK_SECRET = "your_webhook_secret" # Replace with your secret class WebhookPayload(BaseModel): event: str data: dict # Endpoint to receive webhook events @webhook_router.post("/webhook/sms_received/") async def handle_webhook(request: Request): # Parse the webhook payload payload = await request.json() event = payload.get("event") data = payload.get("data") # Handle different event types if event == "payment_success": # Process payment success print("Payment was successful:", data) elif event == "user_registered": # Process new user registration print("New user registered:", data) else: print("Received unknown event:", event) # Return a response return {"status": "received"} # # Optional: Verify the webhook signature # def verify_signature(request: Request, signature: str) -> bool: # if not signature: # return False # body = await request.body() # computed_signature = hmac.new( # WEBHOOK_SECRET.encode(), # body, # hashlib.sha256 # ).hexdigest() # return hmac.compare_digest(computed_signature, signature)