{
  "name": "whatsapp-welcome",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-welcome",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\n\n// Format phone: ensure it starts with country code\nlet phone = (data.phone || '').replace(/\\s+/g, '').replace(/^0+/, '');\nif (!phone.startsWith('+')) {\n  // Default to Egypt +20 if no country code\n  phone = phone.startsWith('20') ? `+${phone}` : `+20${phone}`;\n}\n\n// Build bilingual message\nconst name = data.name || '\u0639\u0632\u064a\u0632\u064a \u0627\u0644\u0639\u0645\u064a\u0644';\nconst country = data.country || '';\nconst budget = data.budget ? `$${Number(data.budget).toLocaleString()}` : '';\n\nconst message = `\u0645\u0631\u062d\u0628\u0627\u064b ${name}! \ud83c\udfe0\n\n\u0634\u0643\u0631\u0627\u064b \u0644\u0627\u0647\u062a\u0645\u0627\u0645\u0643 \u0628\u0639\u0642\u0627\u0631\u0627\u062a\u0646\u0627 \u0641\u064a \u0645\u0635\u0631.\n\u0633\u064a\u062a\u0648\u0627\u0635\u0644 \u0645\u0639\u0643 \u0641\u0631\u064a\u0642\u0646\u0627 \u0642\u0631\u064a\u0628\u0627\u064b \u0644\u0645\u0633\u0627\u0639\u062f\u062a\u0643 \u0641\u064a \u0625\u064a\u062c\u0627\u062f \u0627\u0644\u0639\u0642\u0627\u0631 \u0627\u0644\u0645\u0646\u0627\u0633\u0628.\n\n---\nHello ${name}! \ud83c\udfe0\n\nThank you for your interest in Egyptian real estate.\nOur team will contact you shortly to help find the perfect property${budget ? ` within your budget of ${budget}` : ''}.\n\nLeads Egypt | ${new Date().getFullYear()}`;\n\nreturn [{ json: { phone, message, name, leadId: data.leadId } }];"
      },
      "id": "format-message",
      "name": "Format Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "phoneNumberId": "={{ $credentials.phoneNumberId }}",
        "recipientPhoneNumber": "={{ $json.phone }}",
        "textBody": "={{ $json.message }}"
      },
      "id": "whatsapp-send",
      "name": "Send WhatsApp",
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, phone: $('Format Message').first().json.phone }) }}"
      },
      "id": "respond",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Format Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Message": {
      "main": [
        [
          {
            "node": "Send WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp": {
      "main": [
        [
          {
            "node": "Respond",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "leads-egypt"
    }
  ]
}