{
  "name": "Proof of Delivery AI Verification",
  "nodes": [
    {
      "parameters": {
        "url": "={{ $json.body.MediaUrl0 }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "pod_image"
            }
          }
        }
      },
      "id": "22500509-17ea-4d19-a955-5adb7ff4f778",
      "name": "Download POD Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        -304,
        224
      ],
      "credentials": {
        "twilioApi": {
          "id": "o4Y4iih7jN5Tzrmv",
          "name": "Twilio account"
        },
        "httpBasicAuth": {
          "id": "qpWWvk9Vj3Vlzanm",
          "name": "Http Basic Auth account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are given a POD image URL.\n\nAnalyze this delivery POD image and extract:\n\n- customer_name\n- delivery_date\n- invoice_number\n- signature_present (true/false)\n- stamp_present (true/false)\n- is_valid_pod (true/false)\n\nReturn ONLY valid JSON.\nDo NOT return any text.",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "=You are a delivery verification assistant. Analyze POD (Proof of Delivery) images and extract key information accurately."
        }
      },
      "id": "b7ecfbb6-47df-4fb2-84fc-1e3c3aafa5f7",
      "name": "Analyze POD Image",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        368,
        224
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o"
        },
        "builtInTools": {},
        "options": {}
      },
      "id": "7fd08354-33df-46c1-a2b6-e1daf5b3217f",
      "name": "GPT-4 Vision Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        352,
        448
      ],
      "credentials": {
        "openAiApi": {
          "id": "SsnwT4koLkkkT1th",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"customer_name\": { \"type\": \"string\" },\n    \"delivery_date\": { \"type\": [\"string\", \"null\"] },\n    \"invoice_number\": { \"type\": [\"string\", \"null\"] },\n    \"signature_present\": { \"type\": \"boolean\" },\n    \"stamp_present\": { \"type\": \"boolean\" },\n    \"is_valid_pod\": { \"type\": \"boolean\" }\n  }\n}"
      },
      "id": "1866b420-e80c-4c2e-b3f7-a50cdca9da4e",
      "name": "POD Data Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        512,
        448
      ]
    },
    {
      "parameters": {
        "inputDataFieldName": "pod_image",
        "name": "={{ 'POD_' + $('POD WhatsApp Webhook Trigger').item.json.body.WaId + '_' + $now.toFormat('yyyyLLdd') + '.jpg' }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "1tFdTx-tE7OzKMmolGvE9ZxK9nTJGVbD5",
          "mode": "list",
          "cachedResultName": "E-POD n8n Demo"
        },
        "options": {}
      },
      "id": "93ddad3f-b03a-4d2c-b643-5bcbb32c3196",
      "name": "Save POD to Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -80,
        160
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "JLUPaAhAkOsVMOBF",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "mode": "chooseBranch",
        "useDataOfInput": 2
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        144,
        224
      ],
      "id": "49da9783-ca81-4bae-a1ff-fee1b53fc20c",
      "name": "Merge POD Data"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "id-1",
              "leftValue": "={{ $json.output.signature_present }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            },
            {
              "id": "id-2",
              "leftValue": "={{ $json.output.is_valid_pod }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f01936c4-8163-4ae4-9019-d0a5a98c23a4",
      "name": "Validate POD Conditions",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        720,
        224
      ]
    },
    {
      "parameters": {
        "from": "={{ $('POD WhatsApp Webhook Trigger').item.json.body.To }}",
        "to": "={{ $('POD WhatsApp Webhook Trigger').item.json.body.From }}",
        "message": "=⚠️ POD Issue Detected\n\nThe image you sent is unclear or missing a signature.\n\nPlease resend a clear POD image with signature to proceed.\n\nThank you!",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        944,
        320
      ],
      "id": "e1ed47bb-136f-4fb9-9cf2-6785474655d7",
      "name": "Send message to Request Clear POD",
      "credentials": {
        "twilioApi": {
          "id": "x3JY36aoL50A71ad",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "from": "={{ $('POD WhatsApp Webhook Trigger').item.json.body.To }}",
        "to": "={{ $('POD WhatsApp Webhook Trigger').item.json.body.From }}",
        "message": "=✅ Delivery Completed!\n\nInvoice #{{ $json.output.invoice_number }} has been successfully delivered.\nBilling has been initiated.\n\nThank you!",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1168,
        128
      ],
      "id": "575400ee-6e8c-4fc8-9ac5-0b14069c37f3",
      "name": "Send POD Confirmation",
      "credentials": {
        "twilioApi": {
          "id": "x3JY36aoL50A71ad",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1ivjZzcSOkGtojuqWW72r1yzAIGi6COkzL7QzcksWLrI",
          "mode": "list",
          "cachedResultName": "POD Tracking"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "customer_name": "={{ $json.output.customer_name }}",
            "invoice_number": "={{ $json.output.invoice_number ?? null }}",
            "signature_present": "={{ $json.output.signature_present }}",
            "stamp_present": "={{ $json.output.stamp_present }}",
            "is_valid_pod": "={{ $json.output.is_valid_pod }}",
            "created_at": "={{ $now }}",
            "status": "Delivered",
            "phone": "={{ $('POD WhatsApp Webhook Trigger').item.json.body.From.replace('whatsapp:+', '') }}",
            "pod_image_url": "={{ $node[\"Save POD to Google Drive\"].json.webViewLink }}",
            "invoice_date": "={{ $json.output.invoice_date }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_name",
              "displayName": "customer_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "invoice_number",
              "displayName": "invoice_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "invoice_date",
              "displayName": "invoice_date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "signature_present",
              "displayName": "signature_present",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "stamp_present",
              "displayName": "stamp_present",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "is_valid_pod",
              "displayName": "is_valid_pod",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "pod_image_url",
              "displayName": "pod_image_url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "c2f3ea9c-80e6-4999-a4f5-56f566c37b32",
      "name": "Save POD Record",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        944,
        128
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Pkc8jJJZleMSmXoy",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Google Sheet\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **POD Tracking → Sheet1**: `phone`, `customer_name`, `invoice_number`, `invoice_date`, `signature_present`, `stamp_present`, `is_valid_pod`, `status`, `pod_image_url`, `created_at`"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -864,
        224
      ],
      "id": "b0c10eec-0eb9-4101-9add-3f82ddb9153a",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "content": "## Google Drive \nSelect your own Google Drive folder in the Drive nodes."
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -864,
        416
      ],
      "id": "b25fa6a9-6a4d-431c-9580-f0a323e1c21a",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "e-pod-message",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -528,
        224
      ],
      "id": "68aae57f-938e-4c8d-81d3-43c2f5ace759",
      "name": "POD WhatsApp Webhook Trigger",
      "webhookId": "ea3e2cb8-9ee4-475d-9da2-2b13584eff40"
    }
  ],
  "pinData": {},
  "connections": {
    "Download POD Image": {
      "main": [
        [
          {
            "node": "Save POD to Google Drive",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge POD Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Analyze POD Image": {
      "main": [
        [
          {
            "node": "Validate POD Conditions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT-4 Vision Model": {
      "ai_languageModel": [
        [
          {
            "node": "Analyze POD Image",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "POD Data Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Analyze POD Image",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Save POD to Google Drive": {
      "main": [
        [
          {
            "node": "Merge POD Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge POD Data": {
      "main": [
        [
          {
            "node": "Analyze POD Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate POD Conditions": {
      "main": [
        [
          {
            "node": "Save POD Record",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send message to Request Clear POD",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send POD Confirmation": {
      "main": [
        []
      ]
    },
    "Save POD Record": {
      "main": [
        [
          {
            "node": "Send POD Confirmation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "POD WhatsApp Webhook Trigger": {
      "main": [
        [
          {
            "node": "Download POD Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "22b59a36-4c30-497a-a4f6-8b0634082bef",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "T7SqafNdEpxocd10",
  "tags": []
}