{
  "name": "tik.tools - creator went live -> Telegram",
  "nodes": [
    {
      "parameters": {
        "content": "## Creator went live -> Telegram\n\nNeeds n8n 2.0 or newer and a tik.tools **Basic** plan or higher (go-live webhooks are not part of Sandbox).\n\n**1. Credentials**\n- **Telegram bot**: Telegram API credential with your bot token from @BotFather.\n- **tik.tools webhook secret**: a Crypto credential. Paste the webhook secret (whsec_...) into *Hmac Secret*.\n\n**2. Telegram chat**: open *Send to Telegram* and replace YOUR_TELEGRAM_CHAT_ID with your chat id (a channel can use @channelname; add the bot to it first).\n\n**3. Publish this workflow** and copy the Production URL of *tik.tools webhook*.\n\n**4. Register the webhook** (replace the URL, the creators and YOUR_API_KEY):\n```\ncurl -X POST \"https://api.tik.tools/webcast/webhooks\" \\\n  -H \"x-api-key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"target_url\":\"https://YOUR-N8N/webhook/tiktools-live-start\",\"event_types\":[\"live.start\"],\"creators\":[\"creator_one\",\"creator_two\"],\"name\":\"n8n go-live to Telegram\"}'\n```\nThe response contains **secret** once. Copy it into the Crypto credential now; it is not shown again.\n\n**5. Test**: send a signed test event with\n`curl -X POST \"https://api.tik.tools/webcast/webhooks/WEBHOOK_ID/test\" -H \"x-api-key: YOUR_API_KEY\"`\n(WEBHOOK_ID is webhook.id from step 4). It arrives marked as a test.",
        "height": 820,
        "width": 560
      },
      "id": "7d04ef79-dae5-4f11-aade-499aa59555da",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -420,
        -380
      ]
    },
    {
      "parameters": {
        "content": "### How it works\nEvery delivery carries X-Tiktool-Signature: t=<unix time>,v1=<hex>. The workflow recomputes HMAC-SHA256 of \"<t>.<raw body>\" with your secret and drops any request whose signature does not match or whose timestamp is more than 5 minutes old.\n\nThe webhook answers 200 straight away, so slow Telegram calls never cause a retry. Deliveries are at least once: the same event_id can arrive twice.",
        "height": 260,
        "width": 520
      },
      "id": "dca6d804-a1ef-4b98-af7d-9984c0042496",
      "name": "How it works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        200,
        -380
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "tiktools-live-start",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "7fed1fd9-eb3b-4b10-a2e7-edb4303d31c7",
      "name": "tik.tools webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "webhookId": "bf42c342-8425-45d4-a311-9586938c853e"
    },
    {
      "parameters": {
        "jsCode": "// Pulls t and v1 out of X-Tiktool-Signature and rebuilds the exact string that\n// was signed: \"<t>.<raw body>\". The raw bytes matter, so the body comes from the\n// Webhook node's Raw Body output, not from the parsed JSON.\nconst items = $input.all();\nconst out = [];\nfor (let i = 0; i < items.length; i++) {\n  const item = items[i];\n  const headers = item.json.headers || {};\n  const header = String(headers['x-tiktool-signature'] || '');\n  const t = (header.match(/(?:^|,)\\s*t=(\\d+)/) || [])[1] || '';\n  const v1 = (header.match(/(?:^|,)\\s*v1=([a-f0-9]+)/) || [])[1] || '';\n  let raw;\n  try {\n    raw = (await this.helpers.getBinaryDataBuffer(i, 'data')).toString('utf8');\n  } catch (e) {\n    // Raw Body switched off: the payload is compact JSON, so serialising the\n    // parsed body again gives back the same bytes.\n    raw = JSON.stringify(item.json.body ?? {});\n  }\n  let body = item.json.body;\n  if (!body || typeof body !== 'object') {\n    try { body = JSON.parse(raw); } catch (e) { body = {}; }\n  }\n  out.push({\n    json: {\n      t,\n      v1,\n      signed_payload: `${t}.${raw}`,\n      event: String(headers['x-tiktool-event'] || body.event || ''),\n      body,\n    },\n  });\n}\nreturn out;\n"
      },
      "id": "46ad6a9b-0388-454d-a119-0cd91490c9fd",
      "name": "Read signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "action": "hmac",
        "binaryData": false,
        "type": "SHA256",
        "value": "={{ $json.signed_payload }}",
        "dataPropertyName": "expected_v1",
        "encoding": "hex"
      },
      "id": "eb27eee4-c56c-4725-a1ec-a9ac26ddca35",
      "name": "Compute HMAC",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "credentials": {
        "crypto": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "tik.tools webhook secret"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Drops anything that is not a correctly signed, fresh live.start, then writes\n// the Telegram message (HTML parse mode, so < > & in names are escaped).\nconst MAX_AGE_SECONDS = 300;\nconst now = Math.floor(Date.now() / 1000);\nconst esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\nconst attr = (s) => esc(s).replace(/\"/g, '&quot;');\nconst sameHex = (a, b) => {\n  if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length || a.length === 0) return false;\n  let diff = 0;\n  for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);\n  return diff === 0;\n};\n\nconst out = [];\nfor (const item of $input.all()) {\n  const j = item.json;\n  const fresh = /^\\d+$/.test(j.t || '') && Math.abs(now - Number(j.t)) <= MAX_AGE_SECONDS;\n  if (!fresh || !sameHex(j.v1, j.expected_v1)) continue;\n  const body = j.body || {};\n  if ((body.event || j.event) !== 'live.start') continue;\n\n  const creator = body.creator || {};\n  const live = body.live || {};\n  const handle = creator.unique_id || 'unknown';\n  const url = live.live_url || `https://www.tiktok.com/@${handle}/live`;\n  const lines = [\n    `<b>@${esc(handle)} is LIVE</b>`,\n    creator.nickname ? esc(creator.nickname) : '',\n    `<a href=\"${attr(url)}\">Watch on TikTok</a>`,\n    body.is_test ? '<i>Test delivery</i>' : '',\n  ].filter(Boolean);\n  out.push({ json: { text: lines.join('\\n'), event_id: body.event_id || '', creator: handle, is_test: !!body.is_test } });\n}\nreturn out;\n"
      },
      "id": "0ff55b89-efc8-4daa-afec-b2d74cc098b8",
      "name": "Verify and build message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "={{ $json.text }}",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "HTML",
          "disable_web_page_preview": true
        }
      },
      "id": "1e5c5142-b6b2-4a3a-aff4-a91d053d1d34",
      "name": "Send to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1100,
        0
      ],
      "credentials": {
        "telegramApi": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    }
  ],
  "connections": {
    "tik.tools webhook": {
      "main": [
        [
          {
            "node": "Read signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read signature": {
      "main": [
        [
          {
            "node": "Compute HMAC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute HMAC": {
      "main": [
        [
          {
            "node": "Verify and build message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify and build message": {
      "main": [
        [
          {
            "node": "Send to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
