{
  "name": "Legal AI Agent for WhatsApp",
  "nodes": [
    {
      "parameters": {
        "path": "legal-intake",
        "httpMethod": "POST",
        "responseMode": "onReceived",
        "responseCode": 200
      },
      "id": "3c5e5c1e-1f1a-4c3e-9b7a-1234567890ab",
      "name": "Incoming WhatsApp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "phone",
              "value": "={{ $json[\"From\"].replace('whatsapp:', '') }}"
            },
            {
              "name": "message",
              "value": "={{ $json[\"Body\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "4d6f6d2f-2g2b-5d4f-ac8b-2345678901bc",
      "name": "Extract Clean Phone",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_OPENROUTER_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyContentType": "json",
        "jsonBody": "{\n  \"model\": \"openai/gpt-3.5-turbo\",\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"Detect the language and tone of this message: '{{ $json[\\\"message\\\"] }}'. Respond only with 'Roman' or 'English'.\"\n    }\n  ]\n}"
      },
      "id": "5e7g7e3g-3h3c-6e5g-bd9c-3456789012cd",
      "name": "Detect Language",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_SID/Messages.json",
        "authentication": "basicAuth",
        "basicAuth": {
          "user": "YOUR_TWILIO_SID",
          "password": "YOUR_TWILIO_AUTH_TOKEN"
        },
        "sendBody": true,
        "bodyContentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "={{ $json[\"phone\"] }}"
            },
            {
              "name": "From",
              "value": "whatsapp:+YOUR_TWILIO_WHATSAPP_NUMBER"
            },
            {
              "name": "Body",
              "value": "={{ $json[\"choices\"][0][\"message\"][\"content\"] === 'Roman' ? 'Salam! Aap ka naam? Maslay ki type? Kitni zarurat hai? Masla zara explain karein.' : 'Hello! Please tell me: 1. Your name 2. Type of legal issue 3. Urgency (e.g., very urgent) 4. Explain the issue briefly.' }}"
            }
          ]
        }
      },
      "id": "6f8h8f4h-4i4d-7f6h-ce0d-4567890123de",
      "name": "Send Intake Questions",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "path": "legal-reply",
        "httpMethod": "POST",
        "responseMode": "onReceived"
      },
      "id": "7g9i9g5i-5j5e-8g7i-df1e-5678901234ef",
      "name": "Wait for User Reply",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_OPENROUTER_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyContentType": "json",
        "jsonBody": "{\n  \"model\": \"openai/gpt-3.5-turbo\",\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"You are a legal assistant AI. A client said: '{{ $json[\\\"Body\\\"] }}'. Extract: name, issue type, urgency, explanation. Respond in JSON format with keys: name, issue, urgency, explanation.\"\n    }\n  ]\n}"
      },
      "id": "8h0j0h6j-6k6f-9h8j-eg2f-6789012345fg",
      "name": "AI Extract Case Info",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "extracted_data"
            }
          ]
        },
        "options": {}
      },
      "id": "9i1k1i7k-7l7g-0i9k-fh3g-7890123456gh",
      "name": "Parse AI Response",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "serviceAccount",
        "serviceAccount": {
          "email": "YOUR_SERVICE_ACCOUNT_EMAIL",
          "privateKey": "YOUR_PRIVATE_KEY"
        },
        "operation": "append",
        "documentId": "YOUR_SPREADSHEET_ID",
        "sheetName": "Cases",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Name": "={{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).name }}",
            "Issue": "={{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).issue }}",
            "Urgency": "={{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).urgency }}",
            "Explanation": "={{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).explanation }}",
            "Phone": "={{ $('Wait for User Reply').item.json[\"From\"].replace('whatsapp:', '') }}",
            "Timestamp": "={{ $now }}"
          }
        }
      },
      "id": "0j2l2j8l-8m8h-1j0l-gi4h-8901234567hi",
      "name": "Save to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1780,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).urgency.toLowerCase() }}",
              "operation": "contains",
              "value2": "urgent"
            }
          ]
        }
      },
      "id": "1k3m3k9m-9n9i-2k1m-hj5i-9012345678ij",
      "name": "Urgency Check",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        2000,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_SID/Messages.json",
        "authentication": "basicAuth",
        "basicAuth": {
          "user": "YOUR_TWILIO_SID",
          "password": "YOUR_TWILIO_AUTH_TOKEN"
        },
        "sendBody": true,
        "bodyContentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "whatsapp:+YOUR_LAWYER_PHONE"
            },
            {
              "name": "From",
              "value": "whatsapp:+YOUR_TWILIO_WHATSAPP_NUMBER"
            },
            {
              "name": "Body",
              "value": "\ud83d\udea8 URGENT Case Received: {{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).name }} needs immediate help. Issue: {{ JSON.parse($json[\"choices\"][0][\"message\"][\"content\"]).issue }}. Phone: {{ $('Wait for User Reply').item.json[\"From\"].replace('whatsapp:', '') }}"
            }
          ]
        }
      },
      "id": "2l4n4l0n-0o0j-3l2n-ik6j-0123456789jk",
      "name": "Notify Lawyer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        2220,
        200
      ]
    },
    {
      "parameters": {
        "amount": 5,
        "unit": "hours"
      },
      "id": "3m5o5m1o-1p1k-4m3o-jl7k-123456789abc",
      "name": "Wait 5 Hours",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        2220,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_SID/Messages.json",
        "authentication": "basicAuth",
        "basicAuth": {
          "user": "YOUR_TWILIO_SID",
          "password": "YOUR_TWILIO_AUTH_TOKEN"
        },
        "sendBody": true,
        "bodyContentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "={{ $('Wait for User Reply').item.json[\"From\"] }}"
            },
            {
              "name": "From",
              "value": "whatsapp:+YOUR_TWILIO_WHATSAPP_NUMBER"
            },
            {
              "name": "Body",
              "value": "Just checking in! If you still need legal assistance, we're here to help. \ud83e\uddd1\u200d\u2696\ufe0f"
            }
          ]
        }
      },
      "id": "4n6p6n2p-2q2l-5n4p-km8l-23456789abcd",
      "name": "Send Follow-up Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        2440,
        400
      ]
    }
  ],
  "connections": {
    "Incoming WhatsApp Webhook": {
      "main": [
        [
          {
            "node": "Extract Clean Phone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Clean Phone": {
      "main": [
        [
          {
            "node": "Detect Language",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Detect Language": {
      "main": [
        [
          {
            "node": "Send Intake Questions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Intake Questions": {
      "main": [
        [
          {
            "node": "Wait for User Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for User Reply": {
      "main": [
        [
          {
            "node": "AI Extract Case Info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Extract Case Info": {
      "main": [
        [
          {
            "node": "Parse AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Response": {
      "main": [
        [
          {
            "node": "Save to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Google Sheets": {
      "main": [
        [
          {
            "node": "Urgency Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Urgency Check": {
      "main": [
        [
          {
            "node": "Notify Lawyer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait 5 Hours",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 5 Hours": {
      "main": [
        [
          {
            "node": "Send Follow-up Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "versionId": "c4f39d6e-8b1a-4f3e-9a2b-1c3d4e5f6789",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "123",
  "tags": []
}