Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, jsonify | |
| import requests | |
| app = Flask(__name__) | |
| # Configuration - Your n8n webhook URL | |
| N8N_WEBHOOK_URL = "https://n53dnorth.app.n8n.cloud/webhook/lead-intake" | |
| def home(): | |
| return render_template('index.html') | |
| def submit_form(): | |
| """ | |
| Receives form data and forwards it to n8n workflow | |
| Maps form fields to the exact structure expected by n8n | |
| """ | |
| try: | |
| # 1. Get data from the web form | |
| data = request.json | |
| # 2. Prepare payload matching EXACTLY what n8n workflow expects | |
| # Based on the sample: { first_name, last_name, email, city, source, budget } | |
| payload = { | |
| "first_name": data.get('first_name', '').strip(), | |
| "last_name": data.get('last_name', '').strip(), | |
| "email": data.get('email', '').strip(), | |
| "city": data.get('city', '').strip(), | |
| "source": data.get('source', 'Web Form').strip(), | |
| "budget": int(data.get('budget', 5000)), # Convert to integer | |
| # Optional fields (can be included for reference) | |
| "company": data.get('company', '').strip() if data.get('company') else None, | |
| "position": data.get('position', '').strip() if data.get('position') else None, | |
| "phone": data.get('phone', '').strip() if data.get('phone') else None, | |
| "address": data.get('address', '').strip() if data.get('address') else None, | |
| "zip_code": data.get('zip_code', '').strip() if data.get('zip_code') else None, | |
| } | |
| # Remove None values to keep payload clean | |
| payload = {k: v for k, v in payload.items() if v is not None} | |
| print(f"Sending to n8n: {payload}") # Debug log | |
| # 3. Send to n8n webhook | |
| response = requests.post( | |
| N8N_WEBHOOK_URL, | |
| json=payload, | |
| timeout=10 # 10 second timeout | |
| ) | |
| print(f"n8n Response Status: {response.status_code}") # Debug log | |
| print(f"n8n Response Body: {response.text}") # Debug log | |
| # 4. Handle n8n's response | |
| if response.status_code == 200: | |
| # Success response from n8n | |
| result = response.json() | |
| return jsonify({ | |
| "status": "success", | |
| "message": result.get("message", "Lead submitted successfully!"), | |
| "data": result.get("data"), | |
| "timestamp": result.get("timestamp") | |
| }), 200 | |
| elif response.status_code == 400: | |
| # Validation error from n8n | |
| result = response.json() | |
| return jsonify({ | |
| "status": "error", | |
| "message": result.get("message", "Validation failed"), | |
| "error": result.get("error") | |
| }), 400 | |
| else: | |
| # Unexpected response | |
| return jsonify({ | |
| "status": "error", | |
| "message": f"Unexpected response from server (Status: {response.status_code})" | |
| }), 500 | |
| except requests.exceptions.Timeout: | |
| return jsonify({ | |
| "status": "error", | |
| "message": "Request timed out. Please try again." | |
| }), 504 | |
| except requests.exceptions.ConnectionError: | |
| return jsonify({ | |
| "status": "error", | |
| "message": "Could not connect to the registration service. Please check your connection." | |
| }), 503 | |
| except ValueError as e: | |
| # JSON parsing error | |
| return jsonify({ | |
| "status": "error", | |
| "message": f"Invalid data format: {str(e)}" | |
| }), 400 | |
| except Exception as e: | |
| # Catch-all for any other errors | |
| print(f"Unexpected error: {str(e)}") # Debug log | |
| return jsonify({ | |
| "status": "error", | |
| "message": "An unexpected error occurred. Please try again." | |
| }), 500 | |
| def health_check(): | |
| """Health check endpoint""" | |
| return jsonify({"status": "healthy", "service": "Lead Registration API"}), 200 | |
| if __name__ == '__main__': | |
| app.run(debug=True, host='0.0.0.0', port=7860) |