{
  "name": "Messenger AI agent",
  "nodes": [
    {
      "parameters": {
        "multipleMethods": true,
        "httpMethod": [
          "GET",
          "POST"
        ],
        "path": "messenger",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "e31fe0bb-8a56-5917-9658-36b4e89a2405",
      "name": "Messenger webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "237d3b07-c124-529b-bc6e-20adb808384f",
              "leftValue": "={{ $json.query['hub.mode'] }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "rightValue": "subscribe"
            },
            {
              "id": "6971e554-41f6-52eb-b5a6-7b370e856d5c",
              "leftValue": "={{ $json.query['hub.verify_token'] }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "rightValue": "YOUR_VERIFY_TOKEN"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f6e3fa05-f012-5518-b7e8-6d4b420f9a8c",
      "name": "Is it Meta's check?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        260,
        140
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "={{ $json.query['hub.challenge'] }}",
        "options": {
          "responseCode": 200,
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "text/plain"
              }
            ]
          }
        }
      },
      "id": "3d94b2d6-7b03-5976-bc41-85554a74e87e",
      "name": "Send the challenge back",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        540,
        40
      ]
    },
    {
      "parameters": {
        "respondWith": "noData",
        "options": {
          "responseCode": 403
        }
      },
      "id": "ff184586-fa84-53c0-9846-367e0a183666",
      "name": "Not ours",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        540,
        220
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "EVENT_RECEIVED",
        "options": {
          "responseCode": 200
        }
      },
      "id": "b40aec2b-9ac2-5de0-a8dc-0fd39d8f23c3",
      "name": "Answer Meta right away",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        260,
        460
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "4cd605e7-afa2-5360-b5b9-c5e9fb5c76f4",
              "leftValue": "={{ $json.body.entry[0].messaging[0].message.text }}",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            },
            {
              "id": "f8abf324-8aef-5737-b89d-2032c4ef1f91",
              "leftValue": "={{ $json.body.entry[0].messaging[0].message.is_echo === true }}",
              "operator": {
                "type": "boolean",
                "operation": "false",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "cfa7f024-1a58-5004-86a5-82e1f95378bd",
      "name": "Only real messages",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        520,
        460
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "s1",
              "name": "senderId",
              "value": "={{ $json.body.entry[0].messaging[0].sender.id }}",
              "type": "string"
            },
            {
              "id": "s2",
              "name": "text",
              "value": "={{ $json.body.entry[0].messaging[0].message.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "b560bce8-20db-5407-a5e2-b1a1883a050f",
      "name": "Get the message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        780,
        460
      ]
    },
    {
      "parameters": {
        "jsCode": "// Conversations handed off to a person stay on this list, so the agent stays quiet.\nconst paused = $getWorkflowStaticData('global').paused || {};\nreturn $input.all().map(i => ({ json: { ...i.json, paused: !!paused[i.json.senderId] } }));"
      },
      "id": "eedf4ca4-a644-51fb-9316-846cbf78e1be",
      "name": "Paused?",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1020,
        460
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "8bb3a3e4-53cd-5333-92ab-5309c63d3b78",
              "leftValue": "={{ $json.paused }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "c3ad7e88-0a7b-5269-b33f-6bd97de6c213",
      "name": "Is this conversation paused?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1240,
        460
      ]
    },
    {
      "parameters": {},
      "id": "f99393f2-5bdf-5e61-922b-6d9981d1dc78",
      "name": "Stay quiet",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1480,
        320
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.text }}",
        "options": {
          "systemMessage": "You answer the Facebook Messenger messages of [YOUR BUSINESS]'s page.\n\n## Our information (paste only what is public and true)\n- What we offer: [your services, one line each]\n- Hours: [your hours, or delete this line]\n- Where we are: [city or address]\n- How to reach the team: [email, phone or booking link]\n- Most asked question: [the question people message you about the most, and its real answer]\n\n## Rules\n1. Use only this information. If the answer isn't here, say you'll check with the team. Never invent prices, hours, availability or promises. (What the person told you earlier in this conversation, you can use.)\n2. Before booking or promising anything, ask for the person's name and a phone number or email.\n3. If they ask for a person, seem upset, or you're not sure, answer only HANDOFF.\n\nKeep replies short and friendly, in plain text (no markdown), in the language the person writes in.\n\n(Rule 4 isn't a line in this text: keep your Meta app in development mode until rules 1 to 3 work on your own page.)"
        }
      },
      "id": "98562cb4-73de-5d49-8df0-a06ea11efb26",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1480,
        560
      ]
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-flash",
        "options": {
          "temperature": 0.2
        }
      },
      "id": "c15e4b56-aa40-5f13-9da1-e6a00ba4e9d8",
      "name": "Gemini",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1.2,
      "position": [
        1400,
        800
      ],
      "credentials": {
        "googlePalmApi": {
          "id": "",
          "name": "Gemini (Google AI Studio)"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Get the message').item.json.senderId }}",
        "contextWindowLength": 10
      },
      "id": "1df6d1c3-dc13-541a-9285-b761c97635bc",
      "name": "Memory per person",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [
        1580,
        800
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "d4552641-8789-5aae-94ac-7a31ecc43bc3",
              "leftValue": "={{ $json.output.trim().toUpperCase() }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "rightValue": "HANDOFF"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "c61f3514-5889-555c-9c5b-147576466275",
      "name": "HANDOFF?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1840,
        560
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v27.0/me/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ recipient: { id: $('Get the message').item.json.senderId }, messaging_type: 'RESPONSE', message: { text: $json.output } }) }}",
        "options": {}
      },
      "id": "c89e9696-3e62-5daf-84e7-f5731167e8ee",
      "name": "Send the reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2100,
        660
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "",
          "name": "Facebook page token"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Put this person on the pause list: the agent stays quiet until your team takes over.\nconst data = $getWorkflowStaticData('global');\ndata.paused = data.paused || {};\nconst id = $('Get the message').first().json.senderId;\ndata.paused[id] = new Date().toISOString();\nreturn [{ json: { senderId: id, pausedAt: data.paused[id] } }];"
      },
      "id": "ceb4e0ee-f873-5f60-891c-5b87c6ed46a6",
      "name": "Pause this conversation",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2100,
        440
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v27.0/me/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ recipient: { id: $('Get the message').item.json.senderId }, messaging_type: 'RESPONSE', message: { text: 'Thanks. A person from our team will follow up with you here today.' } }) }}",
        "options": {}
      },
      "id": "5c8083fb-c8e5-5b9c-8ccf-d8f17d8de2df",
      "name": "Tell them a person will follow up",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2340,
        440
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "",
          "name": "Facebook page token"
        }
      }
    },
    {
      "parameters": {
        "fromEmail": "agent@yourbusiness.com",
        "toEmail": "team@yourbusiness.com",
        "subject": "Handoff from Messenger: a person needs to answer",
        "emailFormat": "text",
        "text": "=Someone on Messenger needs a person from the team.\n\nTheir last message: {{ $('Get the message').first().json.text }}\nConversation ID: {{ $('Get the message').first().json.senderId }}\n\nThe agent is paused for this conversation. Reply from the page inbox within 24 hours of their last message.",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "7643e8a2-bebd-5d66-bffa-d55ae4bef465",
      "name": "Email the team",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        2580,
        440
      ],
      "credentials": {
        "smtp": {
          "id": "",
          "name": "Team email (SMTP)"
        }
      }
    }
  ],
  "connections": {
    "Messenger webhook": {
      "main": [
        [
          {
            "node": "Is it Meta's check?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Answer Meta right away",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is it Meta's check?": {
      "main": [
        [
          {
            "node": "Send the challenge back",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not ours",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer Meta right away": {
      "main": [
        [
          {
            "node": "Only real messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Only real messages": {
      "main": [
        [
          {
            "node": "Get the message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get the message": {
      "main": [
        [
          {
            "node": "Paused?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Paused?": {
      "main": [
        [
          {
            "node": "Is this conversation paused?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is this conversation paused?": {
      "main": [
        [
          {
            "node": "Stay quiet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Memory per person": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "HANDOFF?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HANDOFF?": {
      "main": [
        [
          {
            "node": "Pause this conversation",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send the reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pause this conversation": {
      "main": [
        [
          {
            "node": "Tell them a person will follow up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tell them a person will follow up": {
      "main": [
        [
          {
            "node": "Email the team",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}