{
  "name": "Lead Status Update Based on Email Response",
  "nodes": [
    {
      "parameters": {
        "authentication": "predefinedCredentialType",
        "mailbox": "INBOX",
        "options": {
          "onlyUnread": true,
          "download": true
        }
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json[\"subject\"].toLowerCase().includes(\"re:\") }}",
              "operation": "contains",
              "value2": true
            }
          ]
        }
      },
      "name": "Is Reply?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Extract email address from the From field\nconst fromEmail = $input.item.from.toLowerCase();\nconst emailRegex = /[\\w.-]+@[\\w.-]+\\.[\\w.-]+/;\nconst match = fromEmail.match(emailRegex);\n\nif (match) {\n  const email = match[0];\n  $input.item.extractedEmail = email;\n  return $input.item;\n} else {\n  $input.item.extractedEmail = null;\n  return $input.item;\n}"
      },
      "name": "Extract Email",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        200
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "GET",
        "url": "=https://your-crm-api.com/api/leads?search={{ $json.extractedEmail }}",
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.CRM_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Find Lead in CRM",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.length }}",
              "operation": ">",
              "value2": 0
            }
          ]
        }
      },
      "name": "Lead Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1050,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "// Get the first lead from the array\nreturn $input.item[0];"
      },
      "name": "Get Lead",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1250,
        100
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "PUT",
        "url": "=https://your-crm-api.com/api/leads/{{ $json.id }}",
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.CRM_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\n  \"status\": \"engaged\"\n}"
      },
      "name": "Update Lead Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1450,
        100
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "=https://your-crm-api.com/api/activities",
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.CRM_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\n  \"lead_id\": {{ $json.id }},\n  \"activity_type\": \"email\",\n  \"title\": \"Email reply received\",\n  \"description\": \"Lead replied to our outreach email. Subject: {{ $node[\"IMAP Email\"].json[\"subject\"] }}\",\n  \"completed_at\": \"{{ $now.toISOString() }}\"\n}"
      },
      "name": "Log Email Activity",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1650,
        100
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "=https://your-crm-api.com/api/activities",
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.CRM_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\n  \"lead_id\": {{ $json.id }},\n  \"activity_type\": \"call\",\n  \"title\": \"Follow-up call\",\n  \"description\": \"Schedule a follow-up call to discuss next steps\",\n  \"scheduled_at\": \"{{ $today.plus(1, 'days').toISOString() }}\"\n}"
      },
      "name": "Schedule Follow-up Call",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1850,
        100
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "message",
              "value": "=No lead found with email: {{ $json.extractedEmail }}"
            }
          ]
        },
        "options": {}
      },
      "name": "No Lead Found",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://hooks.slack.com/services/YOUR_SLACK_WEBHOOK",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": false,
        "queryParameters": {},
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text",
              "value": "=New email reply from unknown contact: {{ $json.extractedEmail }}. Subject: {{ $node[\"IMAP Email\"].json[\"subject\"] }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Send Notification",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1450,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "message",
              "value": "=Not a reply email: {{ $json[\"subject\"] }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Not a Reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        400
      ]
    }
  ],
  "connections": {
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Is Reply?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Reply?": {
      "main": [
        [
          {
            "node": "Extract Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not a Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email": {
      "main": [
        [
          {
            "node": "Find Lead in CRM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Lead in CRM": {
      "main": [
        [
          {
            "node": "Lead Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lead Found?": {
      "main": [
        [
          {
            "node": "Get Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Lead Found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Lead": {
      "main": [
        [
          {
            "node": "Update Lead Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead Status": {
      "main": [
        [
          {
            "node": "Log Email Activity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Email Activity": {
      "main": [
        [
          {
            "node": "Schedule Follow-up Call",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Lead Found": {
      "main": [
        [
          {
            "node": "Send Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8b4e6a2f-7a5d-4e6d-b1a2-c3d4e5f6a7b9",
  "id": "124",
  "tags": [
    {
      "name": "CRM"
    },
    {
      "name": "Lead Management"
    },
    {
      "name": "Email"
    }
  ]
}