{
  "name": "Tôi Là Tùng - Zalo OA + FPT AI Chat + Google Sheets Lead Generation Pipeline",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "zalo-oa-webhook",
        "options": {}
      },
      "id": "webhook-zalo-oa",
      "name": "Zalo OA Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.fpt.ai/v2/bot/predict",
        "sendHeaders": true,
        "headersUi": {
          "values": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.FPT_AI_BOT_TOKEN }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"content\": \"{{ $json.message.text }}\",\n  \"save_history\": true,\n  \"channel\": \"zalo\",\n  \"sender_id\": \"{{ $json.sender.id }}\"\n}",
        "options": {}
      },
      "id": "fpt-ai-nlp",
      "name": "FPT AI NLP Classifier",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{ $json.data.intent }}",
        "rules": {
          "rules": [
            {
              "value2": "consultation_request",
              "output": 1
            },
            {
              "value2": "pricing_inquiry",
              "output": 1
            }
          ]
        },
        "fallbackOutput": 0
      },
      "id": "intent-switch",
      "name": "Switch on Intent",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        520,
        300
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID_HERE",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Sheet1",
          "mode": "name"
        },
        "options": {},
        "columnsUi": {
          "values": [
            {
              "column": "Time",
              "value": "={{ new Date().toISOString() }}"
            },
            {
              "column": "Zalo User ID",
              "value": "={{ $('Zalo OA Webhook').item.json.sender.id }}"
            },
            {
              "column": "User Name",
              "value": "={{ $('Zalo OA Webhook').item.json.sender.displayName || 'Khách hàng Zalo' }}"
            },
            {
              "column": "Message",
              "value": "={{ $('Zalo OA Webhook').item.json.message.text }}"
            },
            {
              "column": "Detected Intent",
              "value": "={{ $json.data.intent }}"
            },
            {
              "column": "Confidence Score",
              "value": "={{ $json.data.confidence }}"
            },
            {
              "column": "Status",
              "value": "Pending Sales Assignment"
            }
          ]
        }
      },
      "id": "save-to-sheets",
      "name": "Save Lead to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        780,
        200
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "google-sheets-oauth-credentials-id"
        }
      }
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID_HERE",
        "text": "🚨 **CÓ LEAD MỚI CẦN TƯ VẤN THỰC CHIẾN**\n\n👤 **Khách hàng:** {{ $('Zalo OA Webhook').item.json.sender.displayName || 'Khách hàng Zalo' }}\n🆔 **Zalo ID:** \`{{ $('Zalo OA Webhook').item.json.sender.id }}\`\n💬 **Nội dung chat:** \"_{{ $('Zalo OA Webhook').item.json.message.text }}_\"\n🏷️ **Phân loại Intent:** {{ $json.data.intent }} (Độ tin cậy: {{ Math.round($json.data.confidence * 100) }}%)\n\n👉 *Hãy mở Zalo OA Admin Center để chat trực tiếp và hỗ trợ khách hàng ngay lập tức!*"
      },
      "id": "telegram-alert",
      "name": "Send Sales Alert to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1000,
        200
      ],
      "credentials": {
        "telegramApi": {
          "id": "telegram-api-credentials-id"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "{\"status\":\"ignored_intent\"}",
        "options": {}
      },
      "id": "respond-ignored",
      "name": "Respond Ignored",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        780,
        420
      ]
    }
  ],
  "connections": {
    "Zalo OA Webhook": {
      "main": [
        [
          {
            "node": "FPT AI NLP Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FPT AI NLP Classifier": {
      "main": [
        [
          {
            "node": "Switch on Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch on Intent": {
      "main": [
        [
          {
            "node": "Respond Ignored",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Save Lead to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Lead to Google Sheets": {
      "main": [
        [
          {
            "node": "Send Sales Alert to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}
