{
  "name": "Telegram Movie Search Bot - Moviesda & Isaidub",
  "nodes": [
    {
      "id": "telegram_trigger",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        300,
        300
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "token": "{{YOUR_TELEGRAM_BOT_TOKEN}}"
      }
    },
    {
      "id": "extract_movie_name",
      "name": "Extract Movie Name",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        300
      ],
      "parameters": {
        "functionCode": "const msg = $json.body.message.text;\nreturn [{ movie: msg.trim() }];"
      }
    },
    {
      "id": "search_moviesda",
      "name": "Search Moviesda",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        800,
        150
      ],
      "parameters": {
        "url": "https://moviesda19.com/?s={{$json.movie}}",
        "responseFormat": "string"
      }
    },
    {
      "id": "search_isaidub",
      "name": "Search Isaidub",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        800,
        450
      ],
      "parameters": {
        "url": "https://isaidub.love/?s={{$json.movie}}",
        "responseFormat": "string"
      }
    },
    {
      "id": "extract_download_links",
      "name": "Extract Download Links",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "functionCode": "function getQualityLinks(html) {\n  const results = {};\n\n  const regex480 = /(http[^\"']+480p[^\"']+)/i;\n  const regex720 = /(http[^\"']+720p[^\"']+)/i;\n  const regex1080 = /(http[^\"']+1080p[^\"']+)/i;\n\n  results['480p'] = html.match(regex480)?.[1] || null;\n  results['720p'] = html.match(regex720)?.[1] || null;\n  results['1080p'] = html.match(regex1080)?.[1] || null;\n\n  return results;\n}\n\nconst m19 = getQualityLinks(items[0].json);\nconst isd = getQualityLinks(items[1].json);\n\nreturn [{ links: { moviesda: m19, isaidub: isd } }];"
      }
    },
    {
      "id": "send_quality_buttons",
      "name": "Send Quality Buttons",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1400,
        300
      ],
      "parameters": {
        "operation": "sendMessage",
        "text": "Movie found! Choose quality:",
        "additionalFields": {
          "replyMarkup": "{\"inline_keyboard\": [[{\"text\":\"480p\",\"callback_data\":\"480p\"}], [{\"text\":\"720p\",\"callback_data\":\"720p\"}], [{\"text\":\"1080p\",\"callback_data\":\"1080p\"}]]}"
        },
        "chatId": "={{$json.body.message.chat.id}}",
        "token": "{{YOUR_TELEGRAM_BOT_TOKEN}}"
      }
    },
    {
      "id": "telegram_callback",
      "name": "Callback Listener",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        300,
        650
      ],
      "parameters": {
        "updates": [
          "callback_query"
        ],
        "token": "{{YOUR_TELEGRAM_BOT_TOKEN}}"
      }
    },
    {
      "id": "map_selected_quality",
      "name": "Map User Selection",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        650
      ],
      "parameters": {
        "functionCode": "return [{ quality: $json.body.callback_query.data }];"
      }
    },
    {
      "id": "return_download_link",
      "name": "Return Download Link",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        650
      ],
      "parameters": {
        "functionCode": "const q = $json.quality;\n\nconst data = $items('Extract Download Links')[0].json.links;\n\nconst link = data.moviesda[q] || data.isaidub[q] || \"Link not found for this quality\";\n\nreturn [{ text: link }];"
      }
    },
    {
      "id": "send_final_link",
      "name": "Send Final Link",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1100,
        650
      ],
      "parameters": {
        "operation": "sendMessage",
        "text": "={{$json.text}}",
        "chatId": "={{$json.body.callback_query.from.id}}",
        "token": "{{YOUR_TELEGRAM_BOT_TOKEN}}"
      }
    }
  ],
  "connections": {
    "telegram_trigger": {
      "main": [
        [
          {
            "node": "extract_movie_name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "extract_movie_name": {
      "main": [
        [
          {
            "node": "search_moviesda",
            "type": "main",
            "index": 0
          },
          {
            "node": "search_isaidub",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "search_moviesda": {
      "main": [
        [
          {
            "node": "extract_download_links",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "search_isaidub": {
      "main": [
        [
          {
            "node": "extract_download_links",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "extract_download_links": {
      "main": [
        [
          {
            "node": "send_quality_buttons",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "telegram_callback": {
      "main": [
        [
          {
            "node": "map_selected_quality",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "map_selected_quality": {
      "main": [
        [
          {
            "node": "return_download_link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "return_download_link": {
      "main": [
        [
          {
            "node": "send_final_link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}