{
  "name": "Judol Comment Cleaner (Template Publik - Realtime + Groq + Telegram)",
  "nodes": [
    {
      "parameters": {
        "path": "judol-webhook",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "n_get_hook",
      "name": "Webhook Verifikasi (GET)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -780,
        -1240
      ],
      "webhookId": "judol-webhook-get"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "cv1",
              "name": "VERIFY_TOKEN",
              "value": "",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "n_get_cfg",
      "name": "Config Verifikasi",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -560,
        -1240
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "v1",
              "leftValue": "={{ $('Webhook Verifikasi (GET)').item.json.query['hub.mode'] }}",
              "rightValue": "subscribe",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "v2",
              "leftValue": "={{ $('Webhook Verifikasi (GET)').item.json.query['hub.verify_token'] }}",
              "rightValue": "={{ $json.VERIFY_TOKEN }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "n_get_if",
      "name": "Verifikasi Token?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -340,
        -1240
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "={{ $('Webhook Verifikasi (GET)').item.json.query['hub.challenge'] }}",
        "options": {}
      },
      "id": "n_get_ok",
      "name": "Balas Challenge",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        -120,
        -1320
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "Forbidden",
        "options": {
          "responseCode": 403
        }
      },
      "id": "n_get_no",
      "name": "Tolak Verifikasi",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        -120,
        -1160
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "judol-webhook",
        "options": {}
      },
      "id": "n_post_hook",
      "name": "Webhook Komentar (POST)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -780,
        -900
      ],
      "webhookId": "judol-webhook-post"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c1",
              "name": "GRAPH_VERSION",
              "value": "v21.0",
              "type": "string"
            },
            {
              "id": "c2",
              "name": "IG_USER_ID",
              "value": "",
              "type": "string"
            },
            {
              "id": "c3",
              "name": "IG_ACCESS_TOKEN",
              "value": "",
              "type": "string"
            },
            {
              "id": "c4",
              "name": "GROQ_API_KEY",
              "value": "",
              "type": "string"
            },
            {
              "id": "c5",
              "name": "GROQ_MODEL",
              "value": "llama-3.1-8b-instant",
              "type": "string"
            },
            {
              "id": "c6",
              "name": "TELEGRAM_CHAT_ID",
              "value": "",
              "type": "string"
            },
            {
              "id": "c7",
              "name": "CONFIDENCE_THRESHOLD",
              "value": 60,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "n_cfg",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -560,
        -900
      ]
    },
    {
      "parameters": {
        "jsCode": "// Ambil IG_USER_ID dari Config (aman saat manual test)\nlet IG_USER_ID = '';\ntry { IG_USER_ID = $('Config').first().json.IG_USER_ID || ''; } catch (e) {}\n\n// Kata kunci judol yang umum -> langsung sembunyikan tanpa perlu AI\nconst blacklist = /(bet|slot|toto|4d|togel|gacor|maxwin|jackpot|zeus|olympus|cuan)|\\bwd\\b|\\bjp\\b/i;\n\nconst out = [];\nconst items = $input.all();\n\nfor (let i = 0; i < items.length; i++) {\n  const body = items[i].json?.body || items[i].json || {};\n  const entries = body.entry || [];\n\n  for (const entry of entries) {\n    for (const ch of (entry.changes || [])) {\n      // hanya proses event komentar\n      if (ch.field !== 'comments') continue;\n\n      const v = ch.value || {};\n      const text = v.text || '';\n\n      // lewati komentar dari akun kita sendiri\n      if (IG_USER_ID && v.from && String(v.from.id) === String(IG_USER_ID)) continue;\n\n      out.push({\n        json: {\n          comment_id: v.id || '',\n          text: text,\n          username: (v.from && v.from.username) || 'unknown',\n          media_id: (v.media && v.media.id) || 'unknown',\n          quick_judol: blacklist.test(text)\n        },\n        pairedItem: { item: i }\n      });\n    }\n  }\n}\n\n// kalau tidak ada komentar valid, hentikan workflow dengan tenang\nreturn out;\n"
      },
      "id": "n_extract",
      "name": "Ekstrak Komentar",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -340,
        -900
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "q1",
              "leftValue": "={{ $json.quick_judol }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "n_black_if",
      "name": "Blacklist Cepat?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -120,
        -900
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Config').first().json.GROQ_API_KEY }}"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { model: $('Config').first().json.GROQ_MODEL, max_tokens: 200, temperature: 0, response_format: { type: 'json_object' }, messages: [ { role: 'system', content: 'Kamu moderator komentar Instagram pendeteksi promosi judi online (judol). Jawab HANYA dengan JSON valid tanpa teks lain.' }, { role: 'user', content: 'Deteksi apakah komentar Instagram ini promosi judi online. Ciri judol: menyebut nama situs/brand judi (sering kombinasi huruf+angka seperti SUKABET78 atau DEWA99), memuji CS/withdraw/kemenangan yang tidak relevan dengan postingan, ajakan halus untuk daftar/bermain, atau testimoni seperti bot. Balas HANYA JSON: {\"judol\": true/false, \"confidence\": 0-100, \"alasan\": \"singkat\"}. Komentar: ' + ($json.text || '') } ] } }}",
        "options": {}
      },
      "id": "n_groq",
      "name": "AI Deteksi Judol (Groq)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        120,
        -780
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "let judol = false, confidence = 0, alasan = '';\n\ntry {\n  const raw = String($json.choices[0].message.content).replace(/```json|```/g, '').trim();\n  const parsed = JSON.parse(raw);\n  judol = parsed.judol === true;\n  confidence = Number(parsed.confidence) || 0;\n  alasan = parsed.alasan || '';\n} catch (e) {\n  alasan = 'parse_error';\n}\n\nconst threshold = Number($('Config').first().json.CONFIDENCE_THRESHOLD) || 60;\nconst src = $('Ekstrak Komentar').item.json;\n\nreturn { json: {\n  comment_id: src.comment_id,\n  username: src.username,\n  text: src.text,\n  judol: judol,\n  confidence: confidence,\n  alasan: alasan,\n  should_hide: judol && confidence >= threshold\n}};\n"
      },
      "id": "n_decide",
      "name": "Putuskan",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        -780
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "j1",
              "leftValue": "={{ $json.should_hide }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "n_judol_if",
      "name": "Judol?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        560,
        -780
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "p1",
              "name": "comment_id",
              "value": "={{ $json.comment_id }}",
              "type": "string"
            },
            {
              "id": "p2",
              "name": "username",
              "value": "={{ $json.username }}",
              "type": "string"
            },
            {
              "id": "p3",
              "name": "text",
              "value": "={{ $json.text }}",
              "type": "string"
            },
            {
              "id": "p4",
              "name": "alasan",
              "value": "={{ $json.alasan || 'terdeteksi via blacklist' }}",
              "type": "string"
            },
            {
              "id": "p5",
              "name": "confidence",
              "value": "={{ $json.confidence || 100 }}",
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "n_prep",
      "name": "Siapkan Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        800,
        -900
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/{{ $('Config').first().json.GRAPH_VERSION }}/{{ $json.comment_id }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "hide",
              "value": "true"
            },
            {
              "name": "access_token",
              "value": "={{ $('Config').first().json.IG_ACCESS_TOKEN }}"
            }
          ]
        },
        "options": {}
      },
      "id": "n_hide",
      "name": "Sembunyikan Komentar",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1020,
        -900
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $('Config').first().json.TELEGRAM_CHAT_ID }}",
        "text": "=🚫 Komentar judol disembunyikan (realtime)\n\nDari: @{{ $json.username }}\nKomentar: {{ $json.text }}\nAlasan: {{ $json.alasan }}\nConfidence: {{ $json.confidence }}",
        "additionalFields": {}
      },
      "id": "n_tele",
      "name": "Notif Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1240,
        -900
      ],
      "webhookId": "telegram-notif-judol"
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook Verifikasi (GET)": {
      "main": [
        [
          {
            "node": "Config Verifikasi",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config Verifikasi": {
      "main": [
        [
          {
            "node": "Verifikasi Token?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verifikasi Token?": {
      "main": [
        [
          {
            "node": "Balas Challenge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Tolak Verifikasi",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook Komentar (POST)": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Ekstrak Komentar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ekstrak Komentar": {
      "main": [
        [
          {
            "node": "Blacklist Cepat?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Blacklist Cepat?": {
      "main": [
        [
          {
            "node": "Siapkan Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Deteksi Judol (Groq)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Deteksi Judol (Groq)": {
      "main": [
        [
          {
            "node": "Putuskan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Putuskan": {
      "main": [
        [
          {
            "node": "Judol?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Judol?": {
      "main": [
        [
          {
            "node": "Siapkan Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Siapkan Data": {
      "main": [
        [
          {
            "node": "Sembunyikan Komentar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sembunyikan Komentar": {
      "main": [
        [
          {
            "node": "Notif Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}