{
  "name": "ChatRail — WhatsApp images to Google Drive",
  "nodes": [
    {
      "parameters": {
        "content": "## Setup\n\n1. **WhatsApp Webhook** — copy the Production URL and register it in ChatRail:\n   `POST /v1/webhook-endpoints` with `events: [\"message.received\"]`.\n2. **Download Image** — create an HTTP Bearer credential holding your ChatRail API key\n   (`messages:read`).\n3. **Google Drive nodes** — connect a Google Drive OAuth2 credential.\n\nOnly image replies continue; a text reply is filtered out before any download.",
        "height": 300,
        "width": 360
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-360, -120],
      "id": "note-setup",
      "name": "Setup notes"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-images",
        "options": {}
      },
      "id": "node-webhook",
      "name": "WhatsApp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [0, 0],
      "webhookId": "chatrail-whatsapp-images"
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "typeValidation": "strict", "version": 2 },
          "conditions": [
            {
              "id": "only-image",
              "leftValue": "={{ $json.body.data.kind }}",
              "rightValue": "image",
              "operator": { "type": "string", "operation": "equals" }
            },
            {
              "id": "has-url",
              "leftValue": "={{ $json.body.data.media.download_url }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "exists", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-only-images",
      "name": "Only Images",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [224, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "folderName",
              "type": "string",
              "value": "=WhatsApp images {{ $now.toFormat('yyyy-MM-dd') }}"
            },
            {
              "id": "a2",
              "name": "fileName",
              "type": "string",
              "value": "=whatsapp-{{ $now.toFormat('yyyyMMdd-HHmmss') }}.jpg"
            }
          ]
        },
        "options": {}
      },
      "id": "node-prepare",
      "name": "Prepare",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [448, 0]
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $json.folderName }}",
        "limit": 1,
        "filter": {
          "driveId": { "__rl": true, "mode": "list", "value": "My Drive", "cachedResultName": "My Drive" },
          "folderId": { "__rl": true, "mode": "list", "value": "root", "cachedResultName": "/ (Root folder)" },
          "whatToSearch": "folders",
          "includeTrashed": false
        },
        "options": { "fields": ["id", "name"] }
      },
      "id": "node-find-folder",
      "name": "Find Daily Folder",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [672, 0],
      "alwaysOutputData": true,
      "credentials": { "googleDriveOAuth2Api": { "name": "Google Drive account" } }
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "typeValidation": "strict", "version": 2 },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "exists", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-folder-exists",
      "name": "Folder Exists?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [896, 0]
    },
    {
      "parameters": {
        "resource": "folder",
        "name": "={{ $('Prepare').item.json.folderName }}",
        "driveId": { "__rl": true, "mode": "list", "value": "My Drive", "cachedResultName": "My Drive" },
        "folderId": { "__rl": true, "mode": "list", "value": "root", "cachedResultName": "/ (Root folder)" },
        "options": { "simplifyOutput": true }
      },
      "id": "node-create-folder",
      "name": "Create Daily Folder",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [1120, 176],
      "credentials": { "googleDriveOAuth2Api": { "name": "Google Drive account" } }
    },
    {
      "parameters": {},
      "id": "node-folder-ready",
      "name": "Folder Ready",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [1344, 0]
    },
    {
      "parameters": {
        "url": "={{ \"https://www.chatrail.dev\" + $(\"WhatsApp Webhook\").first().json.body.data.media.download_url }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "options": { "response": { "response": { "responseFormat": "file" } } }
      },
      "id": "node-download",
      "name": "Download Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [1568, 0],
      "credentials": { "httpBearerAuth": { "name": "ChatRail API key" } }
    },
    {
      "parameters": {
        "name": "={{ $('Prepare').item.json.fileName }}",
        "driveId": { "__rl": true, "mode": "list", "value": "My Drive", "cachedResultName": "My Drive" },
        "folderId": { "__rl": true, "mode": "id", "value": "={{ $('Folder Ready').item.json.id }}" },
        "options": {}
      },
      "id": "node-upload",
      "name": "Upload to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [1792, 0],
      "credentials": { "googleDriveOAuth2Api": { "name": "Google Drive account" } }
    }
  ],
  "connections": {
    "WhatsApp Webhook": { "main": [[{ "node": "Only Images", "type": "main", "index": 0 }]] },
    "Only Images": { "main": [[{ "node": "Prepare", "type": "main", "index": 0 }], []] },
    "Prepare": { "main": [[{ "node": "Find Daily Folder", "type": "main", "index": 0 }]] },
    "Find Daily Folder": { "main": [[{ "node": "Folder Exists?", "type": "main", "index": 0 }]] },
    "Folder Exists?": {
      "main": [
        [{ "node": "Folder Ready", "type": "main", "index": 0 }],
        [{ "node": "Create Daily Folder", "type": "main", "index": 0 }]
      ]
    },
    "Create Daily Folder": { "main": [[{ "node": "Folder Ready", "type": "main", "index": 1 }]] },
    "Folder Ready": { "main": [[{ "node": "Download Image", "type": "main", "index": 0 }]] },
    "Download Image": { "main": [[{ "node": "Upload to Drive", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1", "binaryMode": "separate" },
  "tags": [{ "name": "ChatRail" }]
}
