{
  "name": "05 - Song Inbox (افزودن آهنگ با فوروارد به بات)",
  "nodes": [
    {
      "parameters": { "path": "song-inbox", "httpMethod": "POST", "options": {} },
      "id": "a5000000-0000-4000-8000-000000000001",
      "name": "Telegram Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [-280, 0],
      "webhookId": "song-inbox",
      "notes": "آدرس Production این وبهوک را با setWebhook به بات وصل کنید (راهنما در README)."
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "f1", "name": "api_base", "value": "https://YOUR-PROJECT.pages.dev", "type": "string" },
            { "id": "f2", "name": "api_key", "value": "PASTE_YOUR_API_KEY", "type": "string" },
            { "id": "f3", "name": "bot_token", "value": "PASTE_YOUR_BOT_TOKEN", "type": "string" },
            { "id": "f4", "name": "admin_id", "value": "YOUR_NUMERIC_TELEGRAM_ID", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "a5000000-0000-4000-8000-000000000002",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-80, 0]
    },
    {
      "parameters": {
        "jsCode": "const upd = $('Telegram Webhook').first().json.body || {};\nconst msg = upd.message || upd.channel_post || {};\nconst audio = msg.audio || msg.document || null;\nconst from = String(msg.from?.id || '');\nconst admin = String($('Config').first().json.admin_id || '');\n\nif (!audio || !audio.file_id) {\n  return [{ json: { ok: false, reason: 'no_audio', chat_id: msg.chat?.id || null } }];\n}\nif (admin && admin !== 'YOUR_NUMERIC_TELEGRAM_ID' && from !== admin) {\n  return [{ json: { ok: false, reason: 'not_admin', chat_id: msg.chat?.id || null } }];\n}\n\n// عنوان و خواننده را از متادیتای فایل یا کپشن می‌سازیم\n// کپشن پیشنهادی:  نام آهنگ | خواننده | سال | برچسب‌ها\nconst cap = (msg.caption || '').trim();\nconst parts = cap.split('|').map(s => s.trim());\nconst title = parts[0] || audio.title || (audio.file_name || 'بدون نام').replace(/\\.(mp3|m4a|ogg|flac|wav)$/i, '');\nconst artist = parts[1] || audio.performer || 'نامشخص';\nconst year = parts[2] || '';\nconst tags = parts[3] || '';\nconst story = parts[4] || '';\n\nreturn [{ json: {\n  ok: true,\n  chat_id: msg.chat?.id || null,\n  payload: { title, artist, year, tags, story, file_id: audio.file_id }\n} }];"
      },
      "id": "a5000000-0000-4000-8000-000000000003",
      "name": "Parse Audio",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [140, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose", "version": 2 },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.ok }}",
              "rightValue": "",
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "a5000000-0000-4000-8000-000000000004",
      "name": "Valid Audio?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [360, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Config').item.json.api_base }}/api/songs",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [{ "name": "x-api-key", "value": "={{ $('Config').item.json.api_key }}" }]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.payload) }}",
        "options": {}
      },
      "id": "a5000000-0000-4000-8000-000000000005",
      "name": "Save Song",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [580, -100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $('Config').item.json.bot_token }}/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ chat_id: $('Parse Audio').item.json.chat_id, text: '✅ آهنگ به آرشیو اضافه شد (کد ' + $json.id + ')\\n🎵 ' + $('Parse Audio').item.json.payload.title + ' — ' + $('Parse Audio').item.json.payload.artist }) }}",
        "options": {}
      },
      "id": "a5000000-0000-4000-8000-000000000006",
      "name": "Confirm To Admin",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [800, -100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $('Config').item.json.bot_token }}/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ chat_id: $json.chat_id, text: 'ℹ️ برای افزودن آهنگ، فایل صوتی را با این قالب کپشن بفرست:\\nنام آهنگ | خواننده | سال | برچسب‌ها | روایت کوتاه' }) }}",
        "options": {}
      },
      "id": "a5000000-0000-4000-8000-000000000007",
      "name": "Send Help",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [580, 120],
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Telegram Webhook": { "main": [[{ "node": "Config", "type": "main", "index": 0 }]] },
    "Config": { "main": [[{ "node": "Parse Audio", "type": "main", "index": 0 }]] },
    "Parse Audio": { "main": [[{ "node": "Valid Audio?", "type": "main", "index": 0 }]] },
    "Valid Audio?": {
      "main": [
        [{ "node": "Save Song", "type": "main", "index": 0 }],
        [{ "node": "Send Help", "type": "main", "index": 0 }]
      ]
    },
    "Save Song": { "main": [[{ "node": "Confirm To Admin", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1", "timezone": "Asia/Tehran" },
  "pinData": {}
}
