{
  "nodes": [
    {
      "id": "5a9086c7-c006-4797-9cf6-808e496eb683",
      "name": "On form submission",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -3220,
        1360
      ],
      "parameters": {
        "options": {},
        "formTitle": "Real Estate Inquiry",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Phone"
            }
          ]
        },
        "formDescription": "Please enter your phone number with country code (example +1)"
      },
      "typeVersion": 2.2
    },
    {
      "id": "879f41da-4a3d-492a-8b09-f5f8e5bc9a68",
      "name": "Postgres Chat Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "position": [
        -2720,
        1900
      ],
      "parameters": {
        "sessionKey": "{{ $('Twilio Trigger').item.json.data.from }}",
        "sessionIdType": "customKey"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": false,
      "typeVersion": 1
    },
    {
      "id": "ab202e50-c344-47ca-a279-0850eb444a21",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1540,
        1840
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4dffd5d2-f53d-4b19-b386-efb1eb3dd8df",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        -3120,
        1740
      ],
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "58c0ea8f-d5c2-48fb-9137-4a79db9cbe3c",
      "name": "OpenAI Chat Model2",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -2920,
        1880
      ],
      "parameters": {
        "model": "gpt-4o",
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7c2d3782-03d8-4f92-8b3f-9ad4bb080594",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        -1340,
        1840
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\t\"transcript\": \"transcript\",\n\t\"summary\": \"summary\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "1d36cb2c-b6cd-4eda-b052-38950603a25c",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3400,
        1200
      ],
      "parameters": {
        "width": 920,
        "height": 320,
        "content": "## Initial Text Message after From Submission\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6e55dc34-0d89-42dd-853c-56dcb8722d3b",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3400,
        1560
      ],
      "parameters": {
        "width": 920,
        "height": 680,
        "content": "## Wait for text response & Reply\n"
      },
      "typeVersion": 1
    },
    {
      "id": "53ecc0f7-43a7-4570-b1ce-3b50ad365828",
      "name": "Real Estate Qualifier",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -2880,
        1680
      ],
      "parameters": {
        "text": "={{ $json.data.body }}",
        "options": {
          "systemMessage": "=You are a friendly real estate assistant helping qualify new leads for a realtor. Ask only three brief questions, then end the conversation politely. Keep the tone warm and professional. Your goal is to gather basic information to help the realtor follow up later.\n\n1. Ask for their name.\n2. Ask what type of property they\u2019re looking for (e.g., house, condo, rental).\n3. Ask what city or neighborhood they\u2019re interested in.\n\nOnce all three answers are collected, thank them and let them know a realtor will follow up soon. also add *** to the response\n"
        },
        "promptType": "define"
      },
      "typeVersion": 1.6
    },
    {
      "id": "88e9c3eb-dc7c-40e6-b483-2ca6009210bd",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2420,
        1380
      ],
      "parameters": {
        "width": 1720,
        "height": 860,
        "content": "## Once the conversation is over, summarize and text lead to owner\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6891b278-3cdd-4c10-93dd-d9d12b137a39",
      "name": "Wait 5 Seconds",
      "type": "n8n-nodes-base.wait",
      "position": [
        -3000,
        1360
      ],
      "parameters": {
        "amount": 10
      },
      "typeVersion": 1.1
    },
    {
      "id": "6162dad2-af40-4bcb-a08f-cb4b1b33b275",
      "name": "Initial Text Message",
      "type": "n8n-nodes-base.twilio",
      "position": [
        -2760,
        1340
      ],
      "parameters": {
        "to": "={{ $json.Phone }}",
        "from": "+1234567890",
        "message": "Thanks for submitting the form on our site realestate.com. Do you have a few minutes to answer a few questions?",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e189af10-316c-402b-ae09-c5a4940d8cb9",
      "name": "Wait for Text Response",
      "type": "n8n-nodes-base.twilioTrigger",
      "position": [
        -3340,
        1700
      ],
      "parameters": {
        "updates": [
          "com.twilio.messaging.inbound-message.received"
        ]
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2aa67f37-6e9f-4a30-91ce-ae9338413a1b",
      "name": "Check if Conversation Completed",
      "type": "n8n-nodes-base.if",
      "position": [
        -2340,
        1600
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "717941a7-5fbd-4256-9983-e766ef34b8bb",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.output }}",
              "rightValue": "***"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c1d78c94-e4ac-44be-ac8f-957b338b89cb",
      "name": "Thank Your Text",
      "type": "n8n-nodes-base.twilio",
      "position": [
        -2020,
        1460
      ],
      "parameters": {
        "to": "={{ $('Wait for Text Response').item.json.data.from }}",
        "from": "+1234567890",
        "message": "=Thanks, I'll give you a call soon.",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b01e4aec-ce25-465d-97f0-e1b35e076249",
      "name": "Response Text from Agent",
      "type": "n8n-nodes-base.twilio",
      "position": [
        -2120,
        1900
      ],
      "parameters": {
        "to": "={{ $('Wait for Text Response').item.json.data.from }}",
        "from": "+1234567890",
        "message": "={{ $json.output }}",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b232d078-8780-490a-9389-e30eca969ec8",
      "name": "Query Supabase for conversation history",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -1880,
        1760
      ],
      "parameters": {
        "query": "SELECT *\nFROM n8n_chat_histories\nWHERE session_id = '{{ $('Twilio Trigger').item.json.data.from }}';\n",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.5
    },
    {
      "id": "c18d8a47-f4df-419c-a904-c86b5418925c",
      "name": "Combine Rows",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        -1720,
        1500
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "8cbed657-22f6-4c4a-bc62-c7e2bfe383de",
      "name": "Summarize Transcript",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1520,
        1560
      ],
      "parameters": {
        "text": "={{ $json.data }}",
        "options": {
          "systemMessage": "=output two fields a clean transcript of the conversation and a summary of the conversation"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.6
    },
    {
      "id": "ecf7fdc7-9d63-40af-8b22-37cf988da9bf",
      "name": "Store results in google ",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -1080,
        1480
      ],
      "parameters": {
        "columns": {
          "value": {
            "Phone": "={{ $('Wait for Text Response').item.json.data.from }}",
            "Summary": "={{ $json.output.summary }}",
            "Transcript": "={{ $json.output.transcript }}"
          },
          "schema": [
            {
              "id": "Phone",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Phone",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Transcript",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Transcript",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Summary",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jSlYXSBA9BJvSuMiqFUJo3omztNJtbejNYEopfbGWN8/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1jSlYXSBA9BJvSuMiqFUJo3omztNJtbejNYEopfbGWN8",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jSlYXSBA9BJvSuMiqFUJo3omztNJtbejNYEopfbGWN8/edit?usp=drivesdk",
          "cachedResultName": "Real Estate Calls"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "50dbd5e4-3b38-4448-aa58-2e44974d9875",
      "name": "Send Lead to owner",
      "type": "n8n-nodes-base.twilio",
      "position": [
        -920,
        1760
      ],
      "parameters": {
        "to": "={{ $('Wait for Text Response').item.json.data.from }}",
        "from": "+1234567890",
        "message": "=*** You just got a new lead. Here's a transcript of the interaction with our AI Bot. ***{{ $json.Summary }}***",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b2849c48-337a-4a4c-b104-6842e525e25a",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2420,
        1200
      ],
      "parameters": {
        "color": 5,
        "width": 1720,
        "height": 120,
        "content": "## AI-Powered Real Estate Lead Responder & Qualifier (n8n Workflow)\n\n** Feel free to contact me if you need help implementing (rbreen@ynteractive.com) **"
      },
      "typeVersion": 1
    },
    {
      "id": "54444b6d-415d-4078-91c9-019af6aad790",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -4140,
        1200
      ],
      "parameters": {
        "color": 5,
        "width": 700,
        "height": 1040,
        "content": "## How to Implement\nFollow these steps to get your workflow fully running:\n\n1. Sign Up for Twilio\n\ud83d\udc49 https://login.twilio.com/\n\nPurchase a phone number with SMS support.\n\nIn n8n, set up Twilio credentials and connect them to the Twilio nodes in the workflow.\n\n2. Add Your OpenAI API Key\n\ud83d\udc49 https://platform.openai.com/\n\nGet your secret key.\n\nAdd your key to the AI Agent and Chat Model nodes using OpenAI credentials.\n\n3. Set Up Google Sheets\n\ud83d\udc49 Copy this sheet - https://docs.google.com/spreadsheets/d/1jSlYXSBA9BJvSuMiqFUJo3omztNJtbejNYEopfbGWN8/edit?usp=sharing\n\nIn n8n, connect your Google Sheets OAuth2 credentials and update the sheet ID in the Google Sheets nodes.\n\n4. Set Up Supabase for Chat Memory\n\ud83d\udc49 https://supabase.com/\n\nCreate a free Supabase project.\n\nGo to Database > Connection Info, and copy your PostgreSQL credentials.\n\nIn n8n, go to Credentials > PostgreSQL and create a new connection using your Supabase info.\n\nLink this credential in the PostgreSQL Chat Memory field of your AI Agent node to persist and recall chat history per phone number.\n\n5. Customize Buyer Questions (Optional)\nEdit the AI Agent node that asks pre-qualifying questions.\n\nYou can change the prompt to ask any questions relevant to your lead funnel (e.g., loan preapproval, desired move-in date, etc.).\n\n\u2705 Ready to Launch!\nOnce configured:\n\nThe workflow listens for a form submission.\n\nSends an SMS to the buyer to start a chat.\n\nUses AI to ask key buyer questions.\n\nStores chat memory in Supabase (so follow-ups stay contextual).\n\nSummarizes and logs the lead to Google Sheets for your agent."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Wait": {
      "main": [
        [
          {
            "node": "Real Estate Qualifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Rows": {
      "main": [
        [
          {
            "node": "Summarize Transcript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 5 Seconds": {
      "main": [
        [
          {
            "node": "Initial Text Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On form submission": {
      "main": [
        [
          {
            "node": "Wait 5 Seconds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Summarize Transcript",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "Real Estate Qualifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "Real Estate Qualifier",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Summarize Transcript": {
      "main": [
        [
          {
            "node": "Store results in google ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Real Estate Qualifier": {
      "main": [
        [
          {
            "node": "Check if Conversation Completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Text Response": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store results in google ": {
      "main": [
        [
          {
            "node": "Send Lead to owner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "Summarize Transcript",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Check if Conversation Completed": {
      "main": [
        [
          {
            "node": "Query Supabase for conversation history",
            "type": "main",
            "index": 0
          },
          {
            "node": "Thank Your Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response Text from Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query Supabase for conversation history": {
      "main": [
        [
          {
            "node": "Combine Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}