{
  "name": "Media Downloader - Telegram",
  "nodes": [
    {
      "parameters": {
        "operation": "sendVideo",
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "file": "={{$json[\"data\"][\"play\"]}}",
        "additionalFields": {
          "caption": "Here’s your TikTok video :p"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -112,
        0
      ],
      "id": "93f16ca2-9f7c-49f1-adcf-c029238185c4",
      "name": "Send a video",
      "webhookId": "8f651134-78d0-4006-93d7-f7d1943b0e73",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -1536,
        560
      ],
      "id": "984123a5-b761-497e-bec2-657d17432bc4",
      "name": "Get a URL",
      "webhookId": "9fd96965-9a13-4c33-bb9d-e0a945b0a84c",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "text": "❌ Please send a valid URL",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -1568,
        1040
      ],
      "id": "905364db-72ce-43a6-93b8-9c5b1f63138c",
      "name": "Send a text message on non valid URL",
      "webhookId": "ae44f99e-4891-41b7-b33c-2860cd8fb68f",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "url": "https://tiktok-video-no-watermark10.p.rapidapi.com/index/Tiktok/getVideoInfo",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "={{ $json.url }}"
            },
            {
              "name": "hd",
              "value": "1"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-rapidapi-host",
              "value": "tiktok-video-no-watermark10.p.rapidapi.com"
            },
            {
              "name": "x-rapidapi-key",
              "value": ""
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -464,
        96
      ],
      "id": "e162d9f0-d27f-4046-b2ae-76c360ff11ec",
      "name": "HTTP Request to TikTok API",
      "retryOnFail": false,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "/https?:\\/\\/(www\\.)?(vm\\.|vt\\.)?tiktok\\.com\\/[^\\s]+/i",
                    "operator": {
                      "type": "string",
                      "operation": "regex"
                    },
                    "id": "6c07a9fa-1b37-4098-a238-4d4ad18c188c"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "TikTok URL"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "3dcd1e60-5801-4ad3-a06d-5ef57e8be980",
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "^https?:\\/\\/(www\\.)?instagram\\.com\\/.*",
                    "operator": {
                      "type": "string",
                      "operation": "regex"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Instagram URL"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "bcf24ab3-7b14-4c19-8e49-1ff99ec7ca08",
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "/https?:\\/\\/(?:www\\.)?(x|twitter)\\.com\\/[^\\s]+/i",
                    "operator": {
                      "type": "string",
                      "operation": "regex"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "X URL"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        -1344,
        528
      ],
      "id": "3d47bedf-c1f8-4567-bc5c-fad5e3b8080f",
      "name": "Switch"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.url }}",
                    "rightValue": "instagram\\.com\\/reel",
                    "operator": {
                      "type": "string",
                      "operation": "regex"
                    },
                    "id": "d18c5fb2-78b8-4b51-a5e8-9bc99c16f8d6"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Reels"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "73695254-e839-46d3-b548-ea255d77005d",
                    "leftValue": "={{ $json.url }}",
                    "rightValue": "=https?:\\/\\/(www\\.)?instagram\\.com\\/p\\/[^\\s\\/]+",
                    "operator": {
                      "type": "string",
                      "operation": "regex"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Post"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        -736,
        528
      ],
      "id": "95069221-995c-4666-9b14-7da6766ca6d9",
      "name": "Switch1"
    },
    {
      "parameters": {
        "jsCode": "const text = $('Get a URL').first().json.message.text;\nconst match = text.match(/https?:\\/\\/[^\\s]*instagram\\.com[^\\s]*/);\nreturn [{ url: match ? match[0] : null, chat_id: $input.first().json.message.chat.id }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1008,
        544
      ],
      "id": "a6171c53-bf54-4500-a479-0bf9d4aaba62",
      "name": "Extract instagram URL"
    },
    {
      "parameters": {
        "jsCode": "// Function Node code\nconst text = $('Get a URL').first().json.message.text;\nconst match = text.match(/https?:\\/\\/[^\\s]+tiktok\\.com[^\\s]*/);\nreturn [{ url: match ? match[0] : null, chat_id: $input.first().json.message.chat.id }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1008,
        96
      ],
      "id": "3c2f8182-f21a-492c-ad33-97e9fb44499b",
      "name": "Extract tiktok URL"
    },
    {
      "parameters": {
        "operation": "sendVideo",
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "file": "={{ $json.urls[0].url }}",
        "additionalFields": {
          "caption": "Here’s your Instagram video :p"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        176,
        352
      ],
      "id": "137534f2-88ea-42ae-9b03-5c7b6041d338",
      "name": "Send a video1",
      "webhookId": "8f651134-78d0-4006-93d7-f7d1943b0e73",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Get the input data\nconst items = $input.all();\n\n// Initialize with proper structure for type inference\nconst mediaItems = [{ type: '', media: '' }].slice(1);\n\n// Process each item\nitems.forEach(item => {\n  const json = item.json;\n  \n  // Check if urls array exists\n  if (json.urls && Array.isArray(json.urls)) {\n    json.urls.forEach(u => {\n      const url = u.url;\n      const ext = (u.extension || '').toLowerCase();\n      \n      if (url) {\n        if (ext === 'mp4') {\n          mediaItems.push({ type: 'video', media: url });\n        } else if (ext === 'jpg' || ext === 'jpeg' || ext === 'png') {\n          mediaItems.push({ type: 'photo', media: url });\n        }\n      }\n    });\n  }\n});\n\nconst chatId = $('Get a URL').first().json.message.chat.id;\n\n// Return each media item as a separate output\nreturn mediaItems.map((item, index) => ({\n  json: {\n    chat_id: chatId,\n    type: item.type,\n    media: item.media,\n    index: index\n  }\n}));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        176,
        528
      ],
      "id": "432b10f2-e65c-4d11-a259-9e0735d69a0b",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "operation": "sendMediaGroup",
        "chatId": "={{ $json.chat_id }}",
        "media": {
          "media": [
            {
              "type": "={{ $json.type }}",
              "media": "={{ $json.media }}",
              "additionalFields": {}
            }
          ]
        },
        "additionalFields": {
          "disable_notification": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        416,
        528
      ],
      "id": "f9b74131-9a18-45b1-97eb-f991c804f2e5",
      "name": "Send a post group message",
      "webhookId": "94ebebaf-5924-4409-b307-99ad581e557d",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "text": "⚠️ Sorry, something went wrong contacting the Instagram API. Please try again later.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -112,
        448
      ],
      "id": "2b4dbdad-9c1c-447d-acc9-0c4e71d4e941",
      "name": "Send a error text message - Instagram",
      "webhookId": "ae44f99e-4891-41b7-b33c-2860cd8fb68f",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "text": "⚠️ Sorry, something went wrong contacting the TikTok API. Please try again later.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -112,
        208
      ],
      "id": "ce9af25c-aef0-41f0-8b1e-8a2cdbcb7594",
      "name": "Send a error text message - TikTok",
      "webhookId": "ae44f99e-4891-41b7-b33c-2860cd8fb68f",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://all-media-downloader1.p.rapidapi.com/all",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-rapidapi-host",
              "value": "all-media-downloader1.p.rapidapi.com"
            },
            {
              "name": "x-rapidapi-key",
              "value": ""
            },
            {
              "name": "content-type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "={{ $json.url }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -464,
        1040
      ],
      "id": "330c7ee6-0794-4d58-ab63-f9e5236d24ed",
      "name": "HTTP Request2",
      "alwaysOutputData": false,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "text": "⚠️ Sorry, something went wrong contacting the X API. Please try again later.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        192,
        1120
      ],
      "id": "ac4c6ed3-0c8f-4ffb-9e13-f2be9cdd3436",
      "name": "Send a error text message - Instagram1",
      "webhookId": "ae44f99e-4891-41b7-b33c-2860cd8fb68f",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const data = $input.all();\nconst formats = $input.first().json.formats\n\nconst mp4s = formats.filter(f => f.ext === 'mp4' && !f.has_drm);\nmp4s.sort((a, b) => (b.tbr || 0) - (a.tbr || 0));\n\nconst best = mp4s[0];\nreturn [{\n  chat_id: $('Get a URL').first().json.message.chat.id,\n  video: best?.url,       \n  caption: `${$input.first().json.uploader || ''}\\n${$input.first().json.title || ''}`\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        192,
        896
      ],
      "id": "49e88ba4-6cdf-474d-9a7d-30eb02d55166",
      "name": "Code in JavaScript1"
    },
    {
      "parameters": {
        "operation": "sendVideo",
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "file": "={{ $json.video }}",
        "additionalFields": {
          "caption": "Here’s your X video :p"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        432,
        896
      ],
      "id": "34e07e1f-e1ad-4e5b-b7cd-398fe291996f",
      "name": "Send a video2",
      "webhookId": "8f651134-78d0-4006-93d7-f7d1943b0e73",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://instagram120.p.rapidapi.com/api/instagram/links",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-rapidapi-host",
              "value": "instagram120.p.rapidapi.com"
            },
            {
              "name": "x-rapidapi-key",
              "value": ""
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "={{ $json.url }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -464,
        368
      ],
      "id": "6e7f5f64-d6a2-4f1e-bdce-43d81f065035",
      "name": "HTTP Request to Instagram API - Reels",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://instagram120.p.rapidapi.com/api/instagram/links",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-rapidapi-host",
              "value": "instagram120.p.rapidapi.com"
            },
            {
              "name": "x-rapidapi-key",
              "value": ""
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "={{ $json.url }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -464,
        544
      ],
      "id": "1318bfed-706b-4d5f-be6d-bd20a5e70ef3",
      "name": "HTTP Request to Instagram API - Post",
      "alwaysOutputData": false,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "chatId": "={{ $('Get a URL').item.json.message.chat.id }}",
        "text": "Sorry, currently supported Instagram types: Reels, Posts, Stories, Highlights.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -464,
        736
      ],
      "id": "9376772c-9764-4f63-90ef-b7cb893bec08",
      "name": "Send a error text message - Instagram2",
      "webhookId": "ae44f99e-4891-41b7-b33c-2860cd8fb68f",
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const text = $('Get a URL').first().json.message.text;\nconst match = text.match(/https?:\\/\\/[^\\s]*(x\\.com|twitter\\.com)\\/[^\\s]*/i);\nreturn [{ url: match ? match[0] : null, chat_id: $input.first().json.message.chat.id }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1024,
        1040
      ],
      "id": "3f7606e0-c850-4a66-8d60-1e51248d1f1b",
      "name": "Extract X URL"
    }
  ],
  "pinData": {},
  "connections": {
    "Get a URL": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request to TikTok API": {
      "main": [
        [
          {
            "node": "Send a video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a error text message - TikTok",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Extract tiktok URL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract instagram URL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract X URL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a text message on non valid URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract instagram URL": {
      "main": [
        [
          {
            "node": "Switch1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract tiktok URL": {
      "main": [
        [
          {
            "node": "HTTP Request to TikTok API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch1": {
      "main": [
        [
          {
            "node": "HTTP Request to Instagram API - Reels",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request to Instagram API - Post",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a error text message - Instagram2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Send a post group message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request2": {
      "main": [
        [
          {
            "node": "Code in JavaScript1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a error text message - Instagram1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript1": {
      "main": [
        [
          {
            "node": "Send a video2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request to Instagram API - Reels": {
      "main": [
        [
          {
            "node": "Send a video1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a error text message - Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request to Instagram API - Post": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a error text message - Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract X URL": {
      "main": [
        [
          {
            "node": "HTTP Request2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false,
    "errorWorkflow": "rQYpo05eRxDIQxGY",
    "timeSavedPerExecution": 10
  },
  "versionId": "f67b6e24-1d12-4e07-a92e-d7f0f097b76b",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "50f53d1356de28e9d44e86a3eb27d97298141f7ae76dd2741e8aab61cff27e34"
  },
  "id": "iUOfQOF3owSuSrVz",
  "tags": []
}