{
  "name": "Auto Comment Reply",
  "nodes": [
    {
      "parameters": {
        "path": "YOUR_WEBHOOK_PATH",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "0e609d34-fe71-4f00-909f-062819d5e2b5",
      "name": "Webhook Verify (GET)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -448,
        80
      ],
      "webhookId": "YOUR_WEBHOOK_ID"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "leftValue": "={{ $json.query['hub.verify_token'] }}",
              "rightValue": "YOUR_VERIFY_TOKEN",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "id": "bf56c89d-c4cf-4fc6-9780-f7f6399e4fe9"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "79e6802b-62ab-4f7e-9699-2f45fa708670",
      "name": "Check Verify Token",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -224,
        80
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "={{ $json.query['hub.challenge'] }}",
        "options": {}
      },
      "id": "2180856c-bf2c-47aa-b608-0aedea0ae205",
      "name": "Respond Challenge",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "Forbidden",
        "options": {
          "responseCode": 403
        }
      },
      "id": "918a59f7-384e-416d-9edc-fc25158b3420",
      "name": "Respond Forbidden",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        0,
        192
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "YOUR_WEBHOOK_PATH",
        "options": {}
      },
      "id": "f6932236-f4f0-4a21-a2a2-3b5f205b5196",
      "name": "Webhook Event (POST)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -528,
        368
      ],
      "webhookId": "YOUR_WEBHOOK_ID"
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.entry",
        "options": {}
      },
      "id": "d48428ac-9287-4cf9-b109-93b4bb013344",
      "name": "Split Entries",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -320,
        368
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "changes",
        "options": {}
      },
      "id": "db469144-8f64-483a-b2d1-5a9c9c3e7170",
      "name": "Split Changes",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -96,
        368
      ]
    },
    {
      "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": "0840afe1-77a3-49bd-8032-c00cbdf314be",
      "name": "Extract Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        304,
        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. 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 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. Luôn tự xưng bản thân là Sự AI.\n\nNếu khách hàng hỏi xin prompt thì trả lời với nội dung chính là: Prompt Sự AI có ghim ở comment nhé.\n\nNếu khách hàng hỏi về  Voucher thì trả lời với nội dung chính: Đủ 20k follow Sự AI sẽ gủi voucher ngay cho mình nhé\n\nNếu khách hỏi có dạy AI hay có lớp học AI không thì trả lời nội dung chính là: Dạ có, kêu người ta nhắn riêng để gủi khóa học."
            }
          ]
        },
        "simplify": false,
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1.2,
      "position": [
        512,
        48
      ],
      "id": "375b6aef-4578-429d-a105-6f6ce3e6aaa3",
      "name": "Generate Reply",
      "credentials": {}
    },
    {
      "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": [
        896,
        48
      ],
      "id": "f843d83a-ef4c-476e-ae33-a6490dd524a0",
      "name": "Parse Reply"
    },
    {
      "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": "YOUR_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "id": "90cbe900-cb4c-44f4-978b-2670d7ba5dcd",
      "name": "Reply Comment",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        864,
        416
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v25.0/{{ $('Parse Reply').item.json.page_id }}/messages",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "recipient",
              "value": "={{ JSON.stringify({ comment_id: $('Parse Reply').item.json.comment_id }) }}"
            },
            {
              "name": "message",
              "value": "={{ JSON.stringify({ text: 'Chào ' + $('Parse Reply').item.json.commenter_name + ', cảm ơn bạn đã comment: \"' + $('Parse Reply').item.json.comment_message + '\". Bạn cần hỗ trợ gì thêm cứ nhắn mình nhé.\\n Đăng ký khóa học AI văn phòng của SỰ AI ở đây nhé: https://www.suai.vn/khoa-hoc-ai-van-phong/  \\nTất cả các tài liệu hướng dẫn trên mỗi video Sự đều gủi vào nhóm này :\\n  https://zalo.me/g/5ykszkkk10uag4n1kj8i \\nMọi người tham gia để lây tài liệu nhé.\\n ' }) }}"
            },
            {
              "name": "access_token",
              "value": "YOUR_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "id": "fa8cf2a8-e2df-48c3-93bd-bb9104839020",
      "name": "Send Inbox Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        512,
        448
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.value ? $json.value.item : $json.body.entry[0].changes[0].value.item }}",
                    "rightValue": "comment",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "cb6655d8-0121-477f-9cf0-daf1373d706e"
                  },
                  {
                    "leftValue": "={{ $json.value ? $json.value.verb : $json.body.entry[0].changes[0].value.verb }}",
                    "rightValue": "add",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "248f22b1-8a2e-4a1d-ae07-bec6a2ad7b96"
                  },
                  {
                    "leftValue": "={{ $json.value.from.id }}",
                    "rightValue": "={{ $('Split Entries').item.json.id }}",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    },
                    "id": "8752094e-c24d-42f8-984b-473d821ff259"
                  },
                  {
                    "leftValue": "={{[\"YOUR_PAGE_ID_1\",\"YOUR_PAGE_ID_2\",\"YOUR_PAGE_ID_3\",\"YOUR_PAGE_ID_4\"]}}",
                    "rightValue": "={{ $json.value.from.id }}",
                    "operator": {
                      "type": "array",
                      "operation": "notContains",
                      "rightType": "any"
                    },
                    "id": "9a7e0dca-5128-4361-b2ff-2eb2e5661cd0"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.value ? $json.value.item : $json.body.entry[0].changes[0].value.item }}",
                    "rightValue": "video",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "a2e00102-2667-479a-9fe8-3d3867af426b"
                  },
                  {
                    "leftValue": "={{ $json.value ? $json.value.verb : $json.body.entry[0].changes[0].value.verb }}",
                    "rightValue": "add",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "04e506bd-71d7-4097-9ee2-a9458db6d0bb"
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {
          "fallbackOutput": "none"
        }
      },
      "id": "c7e00965-2bcf-4825-bff8-80b97371d54f",
      "name": "Phân loại sự kiện Feed",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        144,
        400
      ]
    }
  ],
  "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": "Split Entries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Entries": {
      "main": [
        [
          {
            "node": "Split Changes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Changes": {
      "main": [
        [
          {
            "node": "Phân loại sự kiện Feed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Fields": {
      "main": [
        [
          {
            "node": "Generate Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Reply": {
      "main": [
        [
          {
            "node": "Parse Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Reply": {
      "main": [
        [
          {
            "node": "Reply Comment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply Comment": {
      "main": [
        [
          {
            "node": "Send Inbox Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Phân loại sự kiện Feed": {
      "main": [
        [
          {
            "node": "Extract Fields",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "YOUR_VERSION_ID",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "YOUR_INSTANCE_ID"
  },
  "nodeGroups": [],
  "id": "YOUR_WORKFLOW_ID",
  "tags": []
}