{
  "name": "FB Comment Auto Reply + Inbox",
  "nodes": [
    {
      "parameters": {
        "path": "PLACEHOLDER_WEBHOOK_PATH",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a4683304-f376-4ad7-ad67-ad8a6d4579f1",
      "name": "Webhook Verify (GET)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        144,
        160
      ],
      "webhookId": "PLACEHOLDER_WEBHOOK_ID"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "leftValue": "={{ $json.query['hub.verify_token'] }}",
              "rightValue": "DAN_TOKEN_BAN_DAT",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "id": "bf56c89d-c4cf-4fc6-9780-f7f6399e4fe9"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "728bab1f-40df-47bd-b5d1-210f3c490b6b",
      "name": "Check Verify Token",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        368,
        160
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "={{ $json.query['hub.challenge'] }}",
        "options": {}
      },
      "id": "adb93282-bdd0-494d-980c-aae6e2d9b3b8",
      "name": "Respond Challenge",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        592,
        80
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "Forbidden",
        "options": {
          "responseCode": 403
        }
      },
      "id": "37e01bb3-9202-4302-9346-b098e08d80e5",
      "name": "Respond Forbidden",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        592,
        272
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "PLACEHOLDER_WEBHOOK_PATH",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "e8361fc7-b83b-4e5f-9969-cdc9efa52216",
      "name": "Webhook Event (POST)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        144,
        448
      ],
      "webhookId": "PLACEHOLDER_WEBHOOK_ID"
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "EVENT_RECEIVED",
        "options": {}
      },
      "id": "a4e243df-fa35-4635-be24-71f4c4b815ff",
      "name": "Respond OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        368,
        448
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.entry",
        "options": {}
      },
      "id": "a05a0c3f-3cbe-4fbf-9994-2f84efe3369c",
      "name": "Split Entries",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        592,
        448
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "changes",
        "options": {}
      },
      "id": "c78d86ec-0c4c-49ca-8bef-a4bb0fc0805a",
      "name": "Split Changes",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        816,
        448
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "leftValue": "={{ $json.value.item }}",
              "rightValue": "comment",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "id": "79e1c4f1-7915-4e21-96ca-42cd85c60549"
            },
            {
              "leftValue": "={{ $json.value.verb }}",
              "rightValue": "add",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "id": "00013ab3-c7ed-4b70-93be-808b32298f1c"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "bdf64ff7-aa0c-4017-9ca3-7c9d74bc042b",
      "name": "Is New Comment?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1040,
        448
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "comment_id",
              "type": "string",
              "value": "={{ $json.value.comment_id }}"
            },
            {
              "id": "2",
              "name": "page_id",
              "type": "string",
              "value": "={{ $json.value.recipient_id || $('Split Entries').item.json.id }}"
            },
            {
              "id": "3",
              "name": "commenter_id",
              "type": "string",
              "value": "={{ $json.value.from.id }}"
            },
            {
              "id": "4",
              "name": "commenter_name",
              "type": "string",
              "value": "={{ $json.value.from.name }}"
            },
            {
              "id": "5",
              "name": "comment_message",
              "type": "string",
              "value": "={{ $json.value.message }}"
            }
          ]
        },
        "options": {}
      },
      "id": "823489b4-3aed-4473-b72b-364388d94753",
      "name": "Extract Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1264,
        384
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v25.0/{{ $json.comment_id }}/comments",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "message",
              "value": "={{ $json.replyText }}"
            },
            {
              "name": "access_token",
              "value": "DAN_PAGE_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "id": "61c94863-8eec-49b8-98d3-a72585d4bf2f",
      "name": "Reply Comment",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1824,
        288
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v25.0/{{ $json.page_id }}/messages",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "recipient",
              "value": "={{ JSON.stringify({ id: $json.commenter_id }) }}"
            },
            {
              "name": "message",
              "value": "={{ JSON.stringify({ text: 'Chào ' + $json.commenter_name + ', cảm ơn bạn đã comment: \"' + $json.comment_message + '\". Bạn cần hỗ trợ gì thêm cứ nhắn mình nhé' }) }}"
            },
            {
              "name": "messaging_type",
              "value": "RESPONSE"
            },
            {
              "name": "access_token",
              "value": "DAN_PAGE_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "id": "8b2400a7-90f5-4f35-af13-6cad254fc317",
      "name": "Send Inbox Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1856,
        48
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-2.5-flash",
          "mode": "list",
          "cachedResultName": "models/gemini-2.5-flash"
        },
        "messages": {
          "values": [
            {
              "content": "=Bạn là nhân viên chăm sóc khách hàng trên Fanpage Facebook. Có một bình luận mới:\n\nTên người comment: {{ $json.commenter_name }}\nNội dung comment: {{ $json.comment_message }}\n\nHãy viết một câu trả lời ngắn gọn (1-2 câu), tự nhiên, thân thiện, đúng ngữ cảnh nội dung comment, bằng tiếng Việt, có thể nhắc tên người comment. Không thêm lời giải thích, chỉ trả về đúng câu trả lời."
            }
          ]
        },
        "simplify": false,
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1.2,
      "position": [
        1328,
        192
      ],
      "id": "4edfc932-5d09-4499-9bd4-6945e000fe0c",
      "name": "Generate Reply"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const raw = $json.candidates[0].content.parts[0].text;\nconst replyText = raw.trim();\n\nconst original = $('Extract Fields').item.json;\n\nreturn {\n  json: {\n    ...original,\n    replyText\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1648,
        288
      ],
      "id": "7a47a0d3-d7c4-47f5-a43e-762decb45755",
      "name": "Parse Reply"
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook Verify (GET)": {
      "main": [
        [
          {
            "node": "Check Verify Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Verify Token": {
      "main": [
        [
          {
            "node": "Respond Challenge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Forbidden",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook Event (POST)": {
      "main": [
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Entries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Entries": {
      "main": [
        [
          {
            "node": "Split Changes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Changes": {
      "main": [
        [
          {
            "node": "Is New Comment?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is New Comment?": {
      "main": [
        [
          {
            "node": "Extract Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Fields": {
      "main": [
        [
          {
            "node": "Generate Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply Comment": {
      "main": [
        [
          {
            "node": "Send Inbox Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Reply": {
      "main": [
        [
          {
            "node": "Parse Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Reply": {
      "main": [
        [
          {
            "node": "Reply Comment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "66a82123-0bac-4107-aecb-c672bfa5e7a2",
  "meta": {},
  "nodeGroups": [],
  "tags": []
}