{
  "name": "Telegram-auth",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.3,
      "position": [
        -656,
        -192
      ],
      "id": "a5d2ed62-a536-4827-adb5-ddd17265de9f",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "2d037b0b-dfd2-4197-8006-80834dd30f07",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "/start",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -496,
        -192
      ],
      "id": "ddd1740d-c97d-4488-bd5b-1f601652c49d",
      "name": "If"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Halo! Selamat datang di XFlood ! Untuk mengaktifkan peringatan dini, kirimkan kode unik anda! ",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -48,
        -320
      ],
      "id": "91844edf-2796-4910-a030-29735342f48d",
      "name": "Send a text message",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "d7c567d9-3ee9-4a51-9ef4-1c1fd88fbf6f",
              "leftValue": "={{ $('Telegram Trigger').item.json.message.text }}",
              "rightValue": "^[0-9A-Fa-f]{8}$",
              "operator": {
                "type": "string",
                "operation": "regex"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -64,
        48
      ],
      "id": "7e58e524-e235-4722-b276-fc95f7e4743a",
      "name": "If1"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Your XFlood is ready!",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        720,
        -192
      ],
      "id": "f4fa60fe-4fc4-44c7-bb6f-162b43bf4573",
      "name": "Send a text message1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "92d0ff4b-036b-48ca-a4a7-72c690f57c47",
              "leftValue": "={{ $json.created_at }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        304,
        -192
      ],
      "id": "757db1ee-3def-485b-adc2-8c8697163fc6",
      "name": "If2"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Your code is incorrect!",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        672,
        0
      ],
      "id": "068970a5-bdcf-4cb4-b48f-e1a4ae9ab323",
      "name": "Send a text message2",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "getAll",
        "tableId": "profiles",
        "returnAll": true,
        "matchType": "allFilters",
        "filters": {
          "conditions": [
            {
              "keyName": "activation_code",
              "condition": "eq",
              "keyValue": "={{ $json.message.text }}"
            },
            {
              "keyName": "role",
              "condition": "eq",
              "keyValue": "personal"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        128,
        -144
      ],
      "id": "9185b184-db01-49e4-836e-34550e8d7140",
      "name": "Get many rows",
      "alwaysOutputData": true,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "tableId": "profiles",
        "filters": {
          "conditions": [
            {
              "keyName": "activation_code",
              "condition": "eq",
              "keyValue": "={{ $('Telegram Trigger').item.json.message.text }}"
            },
            {
              "keyName": "role",
              "condition": "eq",
              "keyValue": "personal"
            }
          ]
        },
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "telegram_linked",
              "fieldValue": "TRUE"
            },
            {
              "fieldId": "chat_id",
              "fieldValue": "={{ $('Telegram Trigger').item.json.message.chat.id }}"
            },
            {
              "fieldId": "activation_code",
              "fieldValue": ""
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        512,
        -192
      ],
      "id": "c7774889-b23d-449a-addd-a87b3ddb6a07",
      "name": "Update a row",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "de856454-51f5-405c-ab8d-66fc90efe267",
              "leftValue": "={{ $json.message.chat.type }}",
              "rightValue": "private",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -352,
        144
      ],
      "id": "f4f67a4b-845f-499a-a498-a45e26e06acf",
      "name": "If3"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "3d7ebc14-b351-4fc3-b93a-90167ef823e6",
              "leftValue": "={{ $('Telegram Trigger').item.json.message.text }}",
              "rightValue": "^/link\\s+[0-9A-Fa-f]{8}$",
              "operator": {
                "type": "string",
                "operation": "regex"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -144,
        240
      ],
      "id": "73c7a173-09aa-4c0b-ba47-bfcd20ee9f05",
      "name": "If4"
    },
    {
      "parameters": {
        "jsCode": "// Extract activation code from /link XXXXXXXX\nconst text = $input.item.json.message.text;\nconst match = text.match(/\\/link\\s+([0-9A-Fa-f]{8})/i);\nconst code = match ? match[1].toUpperCase() : null;\n\nreturn {\n  code: code,\n  chat_id: $input.item.json.message.chat.id,\n  chat_title: $input.item.json.message.chat.title || 'Unknown Group',\n  chat_type: $input.item.json.message.chat.type,\n  message_id: $input.item.json.message.message_id\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        96,
        320
      ],
      "id": "extract-code-community",
      "name": "Extract Code & Group Info"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://localhost:8005/telegram/verify-code",
        "authentication": "none",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { \"code\": $json.code } }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        336,
        320
      ],
      "id": "verify-code-backend",
      "name": "Verify Code (Backend)",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "check-success",
              "leftValue": "={{ $json.status }}",
              "rightValue": "success",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "check-user-id",
              "leftValue": "={{ $json.user_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        576,
        320
      ],
      "id": "check-valid-code-community",
      "name": "Code Valid?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://localhost:8005/telegram/link-group",
        "authentication": "none",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {\n  \"user_id\": $json.user_id,\n  \"chat_id\": $('Extract Code & Group Info').item.json.chat_id,\n  \"chat_title\": $('Extract Code & Group Info').item.json.chat_title\n} }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        816,
        240
      ],
      "id": "link-group-backend",
      "name": "Link Group (Backend)"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "\u2705 **Group berhasil terhubung!**\n\nDashboard Majalaya kini akan mengirimkan broadcast peringatan dini otomatis ke group ini.\n\n\ud83d\udd14 Notifikasi akan dikirim saat:\n\u2022 Status pos pantau berubah (Normal \u2192 Siaga \u2192 Waspada \u2192 Awas)\n\u2022 Terjadi perubahan signifikan pada sensor terpantau\n\nGunakan dashboard untuk melihat data real-time dan mengirim broadcast manual.",
        "replyToMessageId": "={{ $('Extract Code & Group Info').item.json.message_id }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1056,
        240
      ],
      "id": "send-success-community",
      "name": "Send Success (Community)",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ \"\u274c **Kode aktivasi tidak valid**\\n\\n\" + ($('Verify Code (Backend)').item.json.message || 'Kode yang Anda masukkan salah atau sudah digunakan.') + \"\\n\\nSilakan:\\n1. Buka dashboard komunitas Anda\\n2. Buat kode linking baru\\n3. Kirim kembali dengan format: `/link KODE-BARU`\" }}",
        "replyToMessageId": "={{ $('Extract Code & Group Info').item.json.message_id }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        816,
        432
      ],
      "id": "send-error-community",
      "name": "Send Error (Community)",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "\u2139\ufe0f Perintah tidak dikenali.\n\nUntuk menghubungkan group ini dengan Dashboard Majalaya:\n1. Dapatkan kode linking dari dashboard komunitas Anda\n2. Kirim: `/link KODE-ANDA`\n\nContoh: `/link A1B2C3D4`",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        96,
        528
      ],
      "id": "send-help-community",
      "name": "Send Help (Community)",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Get many rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If2": {
      "main": [
        [
          {
            "node": "Update a row",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a text message2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many rows": {
      "main": [
        [
          {
            "node": "If2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update a row": {
      "main": [
        [
          {
            "node": "Send a text message1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If3": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If4": {
      "main": [
        [
          {
            "node": "Extract Code & Group Info",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Help (Community)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Code & Group Info": {
      "main": [
        [
          {
            "node": "Verify Code (Backend)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Code (Backend)": {
      "main": [
        [
          {
            "node": "Code Valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code Valid?": {
      "main": [
        [
          {
            "node": "Link Group (Backend)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Error (Community)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Link Group (Backend)": {
      "main": [
        [
          {
            "node": "Send Success (Community)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "bdca34ed-7143-477b-a2bd-28653e137f77",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "JAGvoVKyK3x66SW7",
  "tags": []
}