{
  "name": "VietlinhAI | Chatbot Telegram nghe được tin nhắn thoại",
  "nodes": [
    {
      "parameters": {
        "modelName": "models/gemini-3.5-flash",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        384,
        -16
      ],
      "id": "a15b9267-8629-4aed-90b9-f21878749a37",
      "name": "Google Gemini Chat Model"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Telegram Trigger').first().json.message.chat.id }}",
        "contextWindowLength": 100
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        544,
        -32
      ],
      "id": "1e2fd02f-c2d5-4e3f-83b5-334805cefb54",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -1936,
        -272
      ],
      "id": "55a2e23d-a658-4983-a5a1-a271fdac1784",
      "name": "Telegram Trigger",
      "webhookId": "0af46c84-acc3-4f0e-8c33-2e0e89920421"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1040,
        -320
      ],
      "id": "dc9d6d57-6fc8-4c27-812e-b9b77a4151a2",
      "name": "Send a text message",
      "webhookId": "052fb769-f9ae-459e-8045-ca62395070ea"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.user_message }}",
        "options": {
          "systemMessage": "=Bạn là trợ lý ảo chính thức của [TÊN DOANH NGHIỆP CỦA BẠN], trả lời khách qua Telegram thay cho chủ doanh nghiệp.\n\n[Điền ngắn gọn: doanh nghiệp bạn làm gì, sản phẩm/dịch vụ chính, khách hàng mục tiêu là ai]\n\nPhong cách trả lời:\n- Lịch sự, chuyên nghiệp, tự nhiên như người thật.\n- Trả lời ngắn gọn, không lan man.\n- Không viết kiểu bài tư vấn dài.\n- Không liệt kê quá nhiều ý nếu khách chưa hỏi sâu.\n- Không dùng giọng quảng cáo.\n- Không hứa chắc kết quả, doanh thu hay cam kết vô căn cứ.\n- Có thể hài hước nhẹ, nhưng vừa đủ.\n\nQuy tắc độ dài:\n- Mỗi câu trả lời nên dưới 100 từ.\n- Tối đa 2 đoạn ngắn.\n- Nếu cần hỏi thêm, chỉ hỏi 1 câu.\n- Không hỏi dồn nhiều câu cùng lúc.\n- Không liệt kê toàn bộ sản phẩm/dịch vụ trừ khi khách hỏi rõ \"cho tôi danh sách đầy đủ\".\n\nQuy tắc khi khách hỏi về sản phẩm, dịch vụ hoặc giá:\n- Không tự bịa giá, ưu đãi, cam kết hoặc kết quả.\n- Nếu chưa có thông tin giá cụ thể, nói ngắn gọn: \"Phần giá sẽ tùy nhu cầu, mình cần bạn cho biết thêm để gợi ý đúng hơn.\"\n- (Gợi ý: bạn có thể gắn thêm 1 node Google Docs Tool hoặc Google Sheets Tool làm nguồn tra cứu sản phẩm/giá thật, rồi nối vào AI Agent này như 1 \"ai_tool\" — mẫu này đã tháo phần tool riêng ra để anh/chị tự gắn tài liệu của mình vào.)\n\nNếu khách hỏi chung chung:\n- Hỏi lại 1 câu để phân loại nhu cầu.\n- Không vội bán.\n\nNếu khách hỏi chủ đề nhạy cảm hoặc ngoài phạm vi:\nTrả lời lịch sự, tránh tư vấn sâu qua chatbot vì dễ thiếu bối cảnh, gợi ý hướng an toàn hơn.\n\nCách xưng hô:\n- Xưng là \"mình\".\n- Gọi khách là \"bạn\".\n- Không nói \"tôi là AI\" nếu không cần.\n\nCâu hỏi của khách:\n{{ $json.message.text }}\n\nHãy trả lời bằng tiếng Việt tự nhiên, ngắn gọn, giống người thật đang chat Telegram."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        432,
        -320
      ],
      "id": "ee4c2ee2-3df1-4cdc-b9a4-8d00e4b227ed",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "c584059a-db1f-4fc8-abcf-b4e3f81bb2ae",
                    "leftValue": "={{ $('Telegram Trigger').item.json.message.text }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Text"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('Telegram Trigger').item.json.message.voice.file_id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "id": "a05a482b-e4fc-40c4-a0ff-6baf14324da4"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Voice"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        -512,
        -288
      ],
      "id": "196fe4cf-d005-4b70-96c5-1897a0a6d49d",
      "name": "Switch"
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $('Telegram Trigger').item.json.message.voice.file_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -240,
        -32
      ],
      "id": "cf17f8cc-0d88-4dd8-8f9d-4cba693f2420",
      "name": "Get a file",
      "webhookId": "f38d8194-d6b8-48c9-bd4a-f2fb2fb089da"
    },
    {
      "parameters": {
        "resource": "audio",
        "modelId": {
          "__rl": true,
          "value": "models/gemini-3.5-flash",
          "mode": "list",
          "cachedResultName": "models/gemini-3.5-flash"
        },
        "inputType": "binary",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1.1,
      "position": [
        -32,
        -32
      ],
      "id": "cc23fd90-5470-4f62-a9cf-996a96e444c6",
      "name": "Transcribe a recording"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "089d8387-4334-411b-af35-fbd99ceeb990",
              "name": "user_message",
              "value": "={{ $('Telegram Trigger').item.json.message.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -48,
        -480
      ],
      "id": "fb54a0b5-0ab2-451c-b97d-a0381a171777",
      "name": "Get text1"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "29ee072c-a9c7-4766-8003-3393e10c416e",
              "name": "user_message",
              "value": "={{ $json.content.parts[0].text }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": "={{ false }}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        160,
        -32
      ],
      "id": "bbd6b631-7f79-44f4-8e14-358d84546193",
      "name": "Get text from voice"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "18db7686-51ec-492b-a6cb-42f70cb1c1e1",
              "leftValue": "={{ $json.exists }}",
              "rightValue": 0,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -1328,
        -272
      ],
      "id": "1e0623dd-09eb-4539-9963-ebd82e3a6888",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "-- Chon Google Sheet cua ban --",
          "cachedResultUrl": ""
        },
        "sheetName": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": ""
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "sender_id": "={{ $json.sender_id }}",
            "order_time": "={{ 'ORD-' + $now.toMillis() }}",
            "status": "new"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "order_id",
              "displayName": "order_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "sender_id",
              "displayName": "sender_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_name",
              "displayName": "customer_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "address",
              "displayName": "address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "menu_items",
              "displayName": "menu_items",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "total",
              "displayName": "total",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "note",
              "displayName": "note",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "order_time",
              "displayName": "order_time",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -960,
        -112
      ],
      "id": "2ef6ee1a-8aea-4017-aa93-398add925a13",
      "name": "Append row in sheet"
    },
    {
      "parameters": {
        "content": "## LẮNG NGHE \n",
        "height": 512,
        "width": 272,
        "color": "#273F2B"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -2032,
        -464
      ],
      "id": "a07af865-f3e6-44a8-b00a-d0ac33670635",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "-- Chon Google Sheet cua ban --",
          "cachedResultUrl": ""
        },
        "sheetName": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": ""
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "sender_id",
              "lookupValue": "={{ $json.message.from.id }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -1680,
        -272
      ],
      "id": "2637916b-e4e7-49a7-8140-3a45212f4432",
      "name": "Kiểm tra Sheet Customer",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "jsCode": "// Lấy data từ đúng node Telegram Trigger\nconst telegramData = $('Telegram Trigger').first().json;\n\n// Lấy kết quả từ node \"Get row(s) in sheet\"\nconst sheetData = $('Kiểm tra Sheet Customer').all();\nconst firstItem = sheetData[0];\nconst fields = firstItem?.json;\n\n// Kiểm tra id đã tồn tại trong sheet chưa\nconst hasData = fields && Object.keys(fields).length > 0 && fields.sender_id !== undefined;\n\nreturn [{\n  json: {\n    sender_id: telegramData.message.from.id,\n    exists: hasData,\n    first_name: telegramData.message.from.first_name,\n    last_name: telegramData.message.from.last_name,\n    username: telegramData.message.from.username,\n    language_code: telegramData.message.from.language_code\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1504,
        -272
      ],
      "id": "5fe96753-31c0-4e55-9e22-aeffdee4c44e",
      "name": "Code Kiểm tra KH Cũ hay Mới"
    },
    {
      "parameters": {
        "content": "## KIỂM TRA KHÁCH HÀNG CŨ MỚI\n",
        "height": 512,
        "width": 560,
        "color": "#070D36"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1712,
        -464
      ],
      "id": "00a04a33-b142-4494-a6cd-6d8a702ce916",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## CẬP NHẬT KHÁCH HÀNG\n",
        "height": 512,
        "width": 400,
        "color": "#070D36"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1088,
        -464
      ],
      "id": "20eaeb55-33cc-4a3c-be8d-45a48029ce96",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "content": "## TRẢ LỜI KHÁCH HÀNG\n",
        "height": 928,
        "width": 1472,
        "color": "#070D36"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -624,
        -672
      ],
      "id": "7e767526-7d13-47a4-8621-78bcc6ffb56f",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "content": "## GỬI VỀ TELEGRAM\n",
        "height": 512,
        "width": 384,
        "color": "#273F2B"
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        912,
        -496
      ],
      "id": "a165f315-8e34-4c4f-b175-dacf8c9d1426",
      "name": "Sticky Note4"
    }
  ],
  "pinData": {},
  "connections": {
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Kiểm tra Sheet Customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Get text1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get a file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a file": {
      "main": [
        [
          {
            "node": "Transcribe a recording",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transcribe a recording": {
      "main": [
        [
          {
            "node": "Get text from voice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get text1": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get text from voice": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Kiểm tra Sheet Customer": {
      "main": [
        [
          {
            "node": "Code Kiểm tra KH Cũ hay Mới",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code Kiểm tra KH Cũ hay Mới": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "nodeGroups": [],
  "tags": []
}
