{
  "name": "Auto-reply to Instagram DMs with an AI chatbot and Google Gemini history",
  "nodes": [
    {
      "id": "feededdf-bb71-4d9c-b869-0a3d1d88d45d",
      "name": "Instagram Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        1024,
        352
      ]
    },
    {
      "id": "b2069343-d56c-4111-9bac-7c72cf8d9330",
      "name": "Webhook Verification",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1248,
        176
      ]
    },
    {
      "id": "1d9d267e-5a09-4aad-a62b-14c6663eaeec",
      "name": "Set Context",
      "type": "n8n-nodes-base.set",
      "position": [
        1568,
        352
      ]
    },
    {
      "id": "afb4d221-8f1a-41ed-b1b9-11a596cb3e69",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        3600,
        960
      ]
    },
    {
      "id": "e4ffc56f-5c56-4fba-8651-f285abdd686f",
      "name": "Get 15 newest rows",
      "type": "n8n-nodes-base.code",
      "position": [
        3072,
        544
      ]
    },
    {
      "id": "67a2212b-fb57-4577-8a40-41b5bf83e5fb",
      "name": "Delay Between Messages",
      "type": "n8n-nodes-base.wait",
      "position": [
        4464,
        736
      ]
    },
    {
      "id": "899467e7-aa45-4ef1-ac4a-65c2f57d4dc4",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        4256,
        720
      ]
    },
    {
      "id": "003ccf45-e60b-45b9-8f0e-123b10d02d7f",
      "name": "Merge History and Find Min_ID",
      "type": "n8n-nodes-base.code",
      "position": [
        3280,
        544
      ]
    },
    {
      "id": "4cd4d408-7e4a-4c5d-9861-a6aeb96c13de",
      "name": "User is Sender",
      "type": "n8n-nodes-base.set",
      "position": [
        2064,
        352
      ]
    },
    {
      "id": "f0fada31-adc3-42cc-8fa1-22a488a636c0",
      "name": "Is from page?",
      "type": "n8n-nodes-base.if",
      "position": [
        1792,
        352
      ]
    },
    {
      "id": "532a45d0-0c3b-4dbd-88d8-038053b0bbbe",
      "name": "Seen",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2400,
        320
      ]
    },
    {
      "id": "ddc0ffa3-27d9-4713-8398-df3402c8508a",
      "name": "Insert To Unprocessed",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        2400,
        544
      ]
    },
    {
      "id": "fafc6774-42f4-4b81-b624-8bce03ab9196",
      "name": "Get history message",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        2848,
        544
      ]
    },
    {
      "id": "1ededf20-1141-45e3-905d-94dbdc83788e",
      "name": "Send Typing",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3616,
        400
      ]
    },
    {
      "id": "7d350753-3bc9-4257-9cad-163b88a7c033",
      "name": "Process Merged Message",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        3616,
        720
      ]
    },
    {
      "id": "bb140074-72ac-49a0-9b48-48d54b0a99e4",
      "name": "Send Text",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        4672,
        736
      ]
    },
    {
      "id": "64f1ffd0-d83e-443c-a43d-bd966e97da78",
      "name": "Update Page Rep",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        4528,
        464
      ]
    },
    {
      "id": "0aa989ef-ac8f-42ee-8961-ea6c5f844868",
      "name": "Update FALSE to TRUE",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        4368,
        464
      ]
    },
    {
      "id": "56786305-3ae3-4a0b-9eff-bcab59711ec7",
      "name": "Clean History",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        4672,
        464
      ]
    },
    {
      "id": "34af050c-7b86-4e29-b2c8-d6007aa6a371",
      "name": "Get MaxID and Merged Mess",
      "type": "n8n-nodes-base.code",
      "position": [
        2624,
        544
      ]
    },
    {
      "id": "a4e720f0-4e6f-4865-9834-c480ef010e09",
      "name": "Is Message?",
      "type": "n8n-nodes-base.if",
      "position": [
        1312,
        368
      ]
    },
    {
      "id": "681198f5-d345-43c6-a5c2-b4c588dee113",
      "name": "Main Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        -48
      ],
      "parameters": {
        "width": 580,
        "height": 1220,
        "content": "## Instagram DM Chatbot with AI & Conversation History\n\nThis workflow turns your **Instagram Business/Creator account** into a fully automated AI chatbot using **Google Gemini** and **n8n Data Table**"
      }
    },
    {
      "id": "0eb9dbcd-ce1b-48e0-9f26-1dc69c08cb20",
      "name": "Author Message",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1600,
        800
      ],
      "parameters": {
        "width": 628,
        "height": 380,
        "content": "## Author Message\n\nHi! I am **Nguyen Thieu Toan (Jay Nguyen)** — a Verified n8n Creator. Thank you for using this template!\n\nThis workflow is shared with you for free. If it brings value to your work,"
      }
    },
    {
      "id": "294cc9be-6f9a-4614-81a1-675048405711",
      "name": "Upgrade Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        976,
        800
      ],
      "parameters": {
        "width": 596,
        "height": 376,
        "content": "## IMPORTANT: Upgrade for Production\n\nThis is a **simplified version** — suitable for learning, testing, or low-traffic use.\n\nFor production, combine with these **100% compatible** workflows (original"
      }
    },
    {
      "id": "444fd240-d108-4d62-8c57-f84cba69c6dc",
      "name": "Section 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        976,
        -48
      ],
      "parameters": {
        "width": 1256,
        "height": 820,
        "content": "## Section 1: Webhook & Validation\nReceives Instagram DM events via webhook → returns GET verification challenge to Meta → **Is Message?** filters text-only events → **Is from page?** blocks echo mess"
      }
    },
    {
      "id": "ad1a29a3-0bd1-4c23-ba6b-94b37f36f643",
      "name": "Section 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2272,
        -48
      ],
      "parameters": {
        "width": 1144,
        "height": 820,
        "content": "## Section 2: Store Message & Load History\nInserts new user message into **Data Table** as `unprocessed` → marks as **Seen** → merges all unprocessed rows into one prompt (simple batching) → retrieves"
      }
    },
    {
      "id": "3f24989a-d18a-468a-8df3-187fbc7e3ad9",
      "name": "Section 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3472,
        -48
      ],
      "parameters": {
        "width": 632,
        "height": 1120,
        "content": "## Section 3: AI Processing\nSends `typing_on` indicator to the user → **Gemini AI Agent** generates a reply using the merged message + full session history → **Format Output** normalizes markdown, str"
      }
    },
    {
      "id": "abd97549-b79e-41e8-810f-517804724baf",
      "name": "Section 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4176,
        -48
      ],
      "parameters": {
        "width": 668,
        "height": 1124,
        "content": "## Section 4: Deliver & Update\nLoops through each message chunk → waits 1 second between sends → delivers via **Instagram Graph API** → marks all unprocessed rows `processed = true` → saves AI reply i"
      }
    },
    {
      "id": "9890977c-ae36-457e-a375-a6af54a26feb",
      "name": "Warning Activate",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        976,
        592
      ],
      "parameters": {
        "width": 440,
        "height": 148,
        "content": "## Activate before verifying!\n\nThe webhook verification in Meta App **only works with the production URL**. Activate this workflow in production mode first, then go to Meta App > Instagram > Webhooks "
      }
    },
    {
      "id": "94733b4a-4ba7-4959-83a8-cdcf2c08b168",
      "name": "Warning Token",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1504,
        160
      ],
      "parameters": {
        "width": 236,
        "height": 180,
        "content": "## Edit this node!\n\nReplace `ig_access_token` with your **long-lived Instagram Access Token** from:\nMeta App > Instagram > API Setup with credentials."
      }
    },
    {
      "id": "8558e2b3-4cc5-42ae-842b-44467d6a599e",
      "name": "Format Output",
      "type": "n8n-nodes-base.code",
      "position": [
        3968,
        720
      ]
    }
  ],
  "connections": {
    "Seen": {
      "main": [
        []
      ]
    },
    "Send Text": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Message?": {
      "main": [
        [
          {
            "node": "Set Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Context": {
      "main": [
        [
          {
            "node": "Is from page?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Output": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is from page?": {
      "main": [
        [],
        [
          {
            "node": "User is Sender",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User is Sender": {
      "main": [
        [
          {
            "node": "Insert To Unprocessed",
            "type": "main",
            "index": 0
          },
          {
            "node": "Seen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Update FALSE to TRUE",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delay Between Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Page Rep": {
      "main": [
        [
          {
            "node": "Clean History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Instagram Webhook": {
      "main": [
        [
          {
            "node": "Webhook Verification",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Webhook Verification",
            "type": "main",
            "index": 0
          },
          {
            "node": "Is Message?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get 15 newest rows": {
      "main": [
        [
          {
            "node": "Merge History and Find Min_ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get history message": {
      "main": [
        [
          {
            "node": "Get 15 newest rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update FALSE to TRUE": {
      "main": [
        [
          {
            "node": "Update Page Rep",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert To Unprocessed": {
      "main": [
        [
          {
            "node": "Get MaxID and Merged Mess",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delay Between Messages": {
      "main": [
        [
          {
            "node": "Send Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Merged Message": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Process Merged Message",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get MaxID and Merged Mess": {
      "main": [
        [
          {
            "node": "Get history message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge History and Find Min_ID": {
      "main": [
        [
          {
            "node": "Send Typing",
            "type": "main",
            "index": 0
          },
          {
            "node": "Process Merged Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}