{
  "name": "ZaChat – Nhận và gửi tin Zalo",
  "active": false,
  "nodes": [
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000001",
      "name": "Webhook ZaChat",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "5b8e2c61-7d3a-4f0e-9a41-0c4a7e000001",
      "parameters": {
        "httpMethod": "POST",
        "path": "zachat",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000002",
      "name": "Tính chữ ký",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        220,
        0
      ],
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{ $json.headers['x-zachat-timestamp'] + '.' + JSON.stringify($json.body) }}",
        "dataPropertyName": "expected",
        "secret": "whsec_DAN_SECRET_WEBHOOK_CUA_BAN",
        "encoding": "hex"
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000003",
      "name": "Chữ ký hợp lệ?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "sig",
              "leftValue": "={{ 'sha256=' + $json.expected }}",
              "rightValue": "={{ $json.headers['x-zachat-signature'] }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "age",
              "leftValue": "={{ Math.abs($now.toSeconds() - Number($json.headers['x-zachat-timestamp'])) }}",
              "rightValue": 300,
              "operator": {
                "type": "number",
                "operation": "lte"
              }
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000004",
      "name": "Trả 200 cho ZaChat",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        -100
      ],
      "parameters": {
        "respondWith": "text",
        "responseBody": "ok",
        "options": {
          "responseCode": 200
        }
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000005",
      "name": "Từ chối (401)",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        100
      ],
      "parameters": {
        "respondWith": "text",
        "responseBody": "bad signature",
        "options": {
          "responseCode": 401
        }
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000006",
      "name": "Chỉ tin khách gửi",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        880,
        -100
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ev",
              "leftValue": "={{ $('Webhook ZaChat').item.json.body.event }}",
              "rightValue": "message.received",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000007",
      "name": "Lấy thông tin tin nhắn",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1100,
        -100
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "msg_id",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.id }}"
            },
            {
              "id": "a2",
              "name": "account_id",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.account_id }}"
            },
            {
              "id": "a3",
              "name": "thread_id",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.thread_id }}"
            },
            {
              "id": "a4",
              "name": "khach",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.sender.name }}"
            },
            {
              "id": "a5",
              "name": "noi_dung",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.text }}"
            },
            {
              "id": "a6",
              "name": "loai",
              "type": "string",
              "value": "={{ $('Webhook ZaChat').item.json.body.data.type }}"
            },
            {
              "id": "a7",
              "name": "thoi_gian",
              "type": "string",
              "value": "={{ DateTime.fromMillis($('Webhook ZaChat').item.json.body.data.timestamp).setZone('Asia/Ho_Chi_Minh').toFormat('yyyy-MM-dd HH:mm:ss') }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000008",
      "name": "Gửi tin Zalo (gọi tay để thử)",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        320
      ],
      "parameters": {}
    },
    {
      "id": "b0f5a2d1-0001-4a00-9000-000000000009",
      "name": "ZaChat API: gửi tin",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        320
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.zachat.vn/api/v1/accounts/acc_ID_NICK/threads/THREAD_ID/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer zk_live_DAN_API_KEY_CUA_BAN"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ idempotency_key: 'n8n-' + $execution.id, type: 'text', text: 'Tin nhắn thử từ n8n qua ZaChat' }) }}",
        "options": {
          "timeout": 40000
        }
      }
    }
  ],
  "connections": {
    "Webhook ZaChat": {
      "main": [
        [
          {
            "node": "Tính chữ ký",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tính chữ ký": {
      "main": [
        [
          {
            "node": "Chữ ký hợp lệ?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chữ ký hợp lệ?": {
      "main": [
        [
          {
            "node": "Trả 200 cho ZaChat",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Từ chối (401)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trả 200 cho ZaChat": {
      "main": [
        [
          {
            "node": "Chỉ tin khách gửi",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chỉ tin khách gửi": {
      "main": [
        [
          {
            "node": "Lấy thông tin tin nhắn",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Gửi tin Zalo (gọi tay để thử)": {
      "main": [
        [
          {
            "node": "ZaChat API: gửi tin",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}