{
  "name": "tik.tools - weekly roster report -> Telegram",
  "nodes": [
    {
      "parameters": {
        "content": "## Weekly roster report -> Telegram\n\nNeeds a tik.tools **Agency Global** plan (creator lists and creator history are Agency Global features).\n\n**1. Credentials**\n- **tik.tools API key**: a Header Auth credential. Name: x-api-key, Value: YOUR_API_KEY.\n- **Telegram bot**: Telegram API credential with your bot token from @BotFather.\n\n**2. Telegram chat**: replace YOUR_TELEGRAM_CHAT_ID in *Send to Telegram*.\n\nRuns every Monday at 09:00. For each of your creator lists it reports members, who is live, diamonds and battles over the last 7 days and the top earners. Covers up to 50 lists and 500 distinct creators per run. With no lists (or only empty lists) nothing is sent.\n\nTotals cover the streams we recorded. A creator is recorded from the day they are added to a list, so a newly added creator shows as \"not recorded yet\" until their next streams.",
        "height": 600,
        "width": 520
      },
      "id": "12cfc807-0d49-4ec0-aae1-8ecb94e1d333",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -460,
        -440
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "b0ca2936-9661-4bdd-abe5-cb4d74d3975b",
      "name": "Every Monday at 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        120,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://api.tik.tools/webcast/agency/lists",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": false,
        "queryParameters": {
          "parameters": []
        },
        "options": {}
      },
      "id": "ef54234d-6460-49b4-ae80-498007019dd3",
      "name": "Get lists",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        340,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "tik.tools API key"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// One item per creator list, so the next node fetches each list's members.\nconst lists = ($input.first().json.lists || []).slice(0, 50);\nreturn lists.map((l) => ({ json: { id: l.id, name: l.name, memberCount: l.memberCount, liveCount: l.liveCount } }));\n"
      },
      "id": "83900b3e-0483-4cb8-a650-06fdfdbf5575",
      "name": "One item per list",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://api.tik.tools/webcast/agency/list",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "id",
              "value": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "id": "e3439e51-ae2a-4952-aa13-8eb895d4da9e",
      "name": "Get list members",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        780,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "tik.tools API key"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// One item per creator across all lists (a creator on two lists is fetched\n// once). Capped so a very large roster stays inside the request budget.\nconst MAX_CREATORS = 500;\nconst seen = new Map();\nfor (const item of $input.all()) {\n  const list = item.json;\n  for (const c of list.creators || []) {\n    const handle = String(c.uniqueId || '').toLowerCase();\n    if (!handle) continue;\n    const entry = seen.get(handle) || { creator: handle, nickname: c.nickname || '', isLive: !!c.isLive, lists: [] };\n    entry.isLive = entry.isLive || !!c.isLive;\n    if (!entry.lists.includes(list.id)) entry.lists.push(list.id);\n    seen.set(handle, entry);\n  }\n}\nreturn [...seen.values()].slice(0, MAX_CREATORS).map((json) => ({ json }));\n"
      },
      "id": "480bb2e8-de47-43cd-aeec-84fd53ecb992",
      "name": "One item per creator",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1000,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://api.tik.tools/webcast/agency/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "creator",
              "value": "={{ $json.creator }}"
            },
            {
              "name": "type",
              "value": "summary"
            },
            {
              "name": "days",
              "value": "7"
            }
          ]
        },
        "options": {
          "batching": {
            "batch": {
              "batchSize": 10,
              "batchInterval": 1000
            }
          }
        }
      },
      "id": "3937f9bc-23af-4e50-ad3d-195123eb80c2",
      "name": "Get 7-day summary",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1220,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "tik.tools API key"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Groups the 7-day summaries back by list and writes one Telegram message.\nconst esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\nconst num = (n) => Math.round(Number(n) || 0).toLocaleString('en-US');\n\nconst summaries = new Map();\nfor (const item of $input.all()) {\n  const s = item.json;\n  if (s && s.creator) summaries.set(String(s.creator).toLowerCase(), s);\n}\nconst lists = $('One item per list').all().map((i) => i.json);\nconst creators = $('One item per creator').all().map((i) => i.json);\n\nconst sections = [];\nfor (const list of lists) {\n  const members = creators.filter((c) => c.lists.includes(list.id));\n  let diamonds = 0, battles = 0, wins = 0, notRecorded = 0;\n  const ranked = [];\n  for (const m of members) {\n    const s = summaries.get(m.creator);\n    if (!s || s.tracked === false) { notRecorded++; continue; }\n    const d = Number(s.gifts && s.gifts.diamonds) || 0;\n    diamonds += d;\n    battles += Number(s.battles && s.battles.total) || 0;\n    wins += Number(s.battles && s.battles.wins) || 0;\n    ranked.push({ creator: m.creator, diamonds: d });\n  }\n  ranked.sort((a, b) => b.diamonds - a.diamonds);\n  const top = ranked.slice(0, 3).map((r, i) => `  ${i + 1}. @${esc(r.creator)} - ${num(r.diamonds)} diamonds`);\n  sections.push([\n    `<b>${esc(list.name)}</b> - ${num(list.memberCount)} creators, ${num(list.liveCount)} live now`,\n    `Diamonds: ${num(diamonds)} | Battles: ${num(wins)} won of ${num(battles)}`,\n    ...top,\n    notRecorded ? `  <i>${notRecorded} not recorded yet this week</i>` : '',\n  ].filter(Boolean).join('\\n'));\n}\n\nlet text = '<b>Weekly roster report</b> - last 7 days';\nfor (const s of sections) {\n  if (text.length + s.length + 2 > 3900) { text += '\\n\\n...'; break; }\n  text += '\\n\\n' + s;\n}\nreturn [{ json: { text, lists: sections.length } }];\n"
      },
      "id": "5ff75786-65e9-4766-acb3-81bac0d4ddf1",
      "name": "Build weekly report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1440,
        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": "3045ad3e-a304-4427-a2d5-7e3ded514758",
      "name": "Send to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1660,
        0
      ],
      "credentials": {
        "telegramApi": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    }
  ],
  "connections": {
    "Every Monday at 09:00": {
      "main": [
        [
          {
            "node": "Get lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get lists": {
      "main": [
        [
          {
            "node": "One item per list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One item per list": {
      "main": [
        [
          {
            "node": "Get list members",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get list members": {
      "main": [
        [
          {
            "node": "One item per creator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One item per creator": {
      "main": [
        [
          {
            "node": "Get 7-day summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get 7-day summary": {
      "main": [
        [
          {
            "node": "Build weekly report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build weekly report": {
      "main": [
        [
          {
            "node": "Send to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
