{
  "name": "Messenger",
  "nodes": [
    {
      "parameters": {
        "multipleMethods": true,
        "path": "webhook-path",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -448,
        0
      ],
      "name": "Webhook"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "leftValue": "={{ $json.query['hub.mode'] }}",
              "rightValue": "subscribe",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "leftValue": "={{ $json.query['hub.verify_token'] }}",
              "rightValue": "YOUR_VERIFY_TOKEN",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [
        -240,
        -96
      ],
      "name": "Verify Webhook"
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "={{ $json.query['hub.challenge'] }}"
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        -32,
        -96
      ],
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "leftValue": "={{ $json.body.entry[0].messaging[0].message }}",
              "operator": {
                "type": "object",
                "operation": "exists"
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [
        -240,
        96
      ],
      "name": "Check Message"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.body.entry[0].messaging[0].message.text }}",
        "options": {
          "systemMessage": "You are a professional lead-collection assistant. Collect Name, Phone, Email step-by-step with validation and confirmation before saving."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        -32,
        96
      ],
      "name": "AI Agent"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.body.entry[0].messaging[0].sender.id }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        -16,
        304
      ],
      "name": "Memory"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/vXX.X/{{ $('Webhook').item.json.body.entry[0].messaging[0].recipient.id }}/messages",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "access_token",
              "value": "YOUR_FACEBOOK_PAGE_ACCESS_TOKEN"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { \"recipient\": { \"id\": $('Webhook').item.json.body.entry[0].messaging[0].sender.id }, \"messaging_type\": \"RESPONSE\", \"message\": { \"text\": $json.output } } }}"
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        320,
        96
      ],
      "name": "Send Message"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": "YOUR_GOOGLE_SHEET_ID",
        "sheetName": "Sheet1",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Name": "={{ $fromAI('Name', '', 'string') }}",
            "Phone": "={{ $fromAI('Phone', '', 'string') }}",
            "Email": "={{ $fromAI('Email', '', 'string') }}"
          }
        }
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        128,
        304
      ],
      "name": "Save to Google Sheets"
    },
    {
      "parameters": {
        "model": "gpt-4.1-mini"
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        -176,
        304
      ],
      "name": "Chat Model"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Verify Webhook",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Check Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Webhook": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Message": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Save to Google Sheets": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  }
}