{
  "id": "MBRhvliMi5xlWUlx",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AD Video Generator",
  "tags": [],
  "nodes": [
    {
      "id": "f89797fa-685e-412e-b186-b117c353d15e",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        208,
        -416
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0fbdf6cb-a5a9-42fd-ada3-537d57e1c53b",
      "name": "Receive Message",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -384,
        -544
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "684d32b1-f04f-42fb-a11c-5a67ca378ae3",
      "name": "Extract Image & Caption",
      "type": "n8n-nodes-base.set",
      "position": [
        -160,
        -544
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3911fb54-cca0-43bb-9482-c6da7641dc92",
              "name": "message.caption",
              "type": "string",
              "value": "={{ $json.message.caption }}"
            },
            {
              "id": "4edb62f8-7aa3-4a96-b0a2-2dfc702e0d45",
              "name": "message.photo[3].file_id",
              "type": "string",
              "value": "={{ $json.message.photo[2].file_id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "bacc3d5a-7b28-46a9-bdd7-57b6089ad91b",
      "name": "Download Telegram Image",
      "type": "n8n-nodes-base.telegram",
      "position": [
        64,
        -544
      ],
      "parameters": {
        "fileId": "={{ $json.message.photo[3].file_id }}",
        "resource": "file",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "fb4a2389-db12-4cf3-9134-90e7d8a54165",
      "name": "Generate Script",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        224,
        -544
      ],
      "parameters": {
        "text": "=Follow System Message",
        "options": {
          "systemMessage": "=You are a professional UGC ad scriptwriter. \nGenerate a 30-second UGC-style video script for Instagram Reels or TikTok \nthat sounds authentic, friendly, and relatable. \nIt should describe the product in a natural human tone, like a real person \nsharing their experience.\n\nProduct details:\n{{ $('Extract Image & Caption').item.json.message.caption }}\n{{ $json.result.file_path }}\n\nIf possible, mention benefits, sensory details, and an emotional payoff. \nEnd with a short, clear call to action like \"link in bio\" or \"try it yourself\".\n"
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "2c512f38-67d3-4c3f-a8b6-6a5fe8f6ecfe",
      "name": "Create Video Generation Task",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        800,
        -544
      ],
      "parameters": {
        "url": "https://api.kie.ai/api/v1/veo/generate",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "prompt",
              "value": "={{ $('Generate Script').item.json.output }}"
            },
            {
              "name": "imageUrls",
              "value": "={{ $json.data.downloadUrl }}"
            },
            {
              "name": "model",
              "value": "veo3_fast"
            },
            {
              "name": "aspectRatio",
              "value": "9:16"
            },
            {
              "name": "seeds",
              "value": "12345"
            },
            {
              "name": "enableFallback",
              "value": "false"
            },
            {
              "name": "enableTranslation",
              "value": "true"
            },
            {
              "name": "generationType",
              "value": "TEXT_2_VIDEO"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer <TOKEN>"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "745edc27-4b5e-4ce2-a629-636448cea7da",
      "name": "Is Video Ready?",
      "type": "n8n-nodes-base.if",
      "position": [
        1232,
        -688
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "48af49b6-272a-41d6-bc92-af8aaa2d4dd5",
              "operator": {
                "type": "number",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json.data.completeTime }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "47a51398-cf01-4e8a-8489-60931fbc2fb9",
      "name": "Wait 30 Seconds",
      "type": "n8n-nodes-base.wait",
      "position": [
        1440,
        -704
      ],
      "parameters": {
        "amount": 30
      },
      "typeVersion": 1.1
    },
    {
      "id": "cbb402a4-bf3c-4312-892d-c833972f44dc",
      "name": "Get HD Video URL",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1488,
        -352
      ],
      "parameters": {
        "url": "https://api.kie.ai/api/v1/veo/get-1080p-video",
        "options": {},
        "sendQuery": true,
        "sendHeaders": true,
        "queryParameters": {
          "parameters": [
            {
              "name": " taskId",
              "value": "={{ $json.data.taskId }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer <TOKEN>"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "dc079bec-5a2e-4b0c-a009-73799d99d3fc",
      "name": "Download Generated Video",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1664,
        -352
      ],
      "parameters": {
        "url": "={{ $('Check Video Status').item.json.data.response.resultUrls[0] }}",
        "options": {}
      },
      "typeVersion": 4.3
    },
    {
      "id": "f27ca815-8a24-4d06-9d43-49444c8edc19",
      "name": "Send Video to User",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1840,
        -352
      ],
      "parameters": {
        "chatId": "={{ $('Receive Message').item.json.message.chat.id }}",
        "operation": "sendVideo",
        "binaryData": true,
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "18a58bee-1516-49a2-8e42-eea638feca20",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        192,
        -688
      ],
      "parameters": {
        "color": 3,
        "width": 272,
        "height": 416,
        "content": "## \ud83d\udcdd Generate Script \nThis node creates a highly engaging ad script for the product using AI, written in a natural, UGC-style tone."
      },
      "typeVersion": 1
    },
    {
      "id": "431166b0-9f45-4ee8-879e-b41b95b8830c",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        -720
      ],
      "parameters": {
        "color": 4,
        "width": 192,
        "height": 368,
        "content": "## \u2601\ufe0f Upload to Kie Storage\nUploads the downloaded Telegram image to Kie.ai storage via API for further video generation."
      },
      "typeVersion": 1
    },
    {
      "id": "e40711bb-7d2f-42c9-a24c-4777ba2c6d67",
      "name": "Upload image to Kie Storage",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        560,
        -544
      ],
      "parameters": {
        "url": "https://kieai.redpandaai.co/api/file-url-upload",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "fileUrl",
              "value": "=https://api.telegram.org/file/bot7759143820:AAHyXBFghzJd5OksfEmOfPYxz6THes5-c6o/{{ $('Download Telegram Image').item.json.result.file_path }}"
            },
            {
              "name": "uploadPath",
              "value": "images/downloaded"
            },
            {
              "name": "fileName",
              "value": "my-downloaded-image.jpg"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer <TOKEN>"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "1f446bb2-0f14-4bd8-a372-cbb947487f9e",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        752,
        -736
      ],
      "parameters": {
        "color": 7,
        "width": 192,
        "height": 384,
        "content": "## \ud83c\udfa5 Create Video Task\nSends the script and image URL to Kie.ai\u2019s Veo3 API to start the UGC video generation process."
      },
      "typeVersion": 1
    },
    {
      "id": "0cb432ee-2e9d-4338-b262-44f6219cbe8e",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        992,
        -848
      ],
      "parameters": {
        "color": 4,
        "width": 192,
        "height": 336,
        "content": "## \ud83d\udef0\ufe0f Check Video Status\nChecks the current generation progress of the video on Kie.ai Veo3 using the task ID."
      },
      "typeVersion": 1
    },
    {
      "id": "57ca7b44-1e55-4f01-a43f-f9fd1adced3d",
      "name": "Check Video Status",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1040,
        -672
      ],
      "parameters": {
        "url": "https://api.kie.ai/api/v1/veo/record-info",
        "options": {},
        "sendQuery": true,
        "sendHeaders": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "taskId",
              "value": "={{ $json.data.taskId }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer <TOKEN>"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "c8417ecb-fa5a-4d9e-beb1-66b0a1f09ae0",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1216,
        -816
      ],
      "parameters": {
        "color": 5,
        "width": 352,
        "height": 272,
        "content": "##  \ud83d\udd04 Video Status Loop\nChecks if the video generation is complete.\nIf not, waits 30 seconds before rechecking until it\u2019s ready."
      },
      "typeVersion": 1
    },
    {
      "id": "12f57a10-da0f-436d-8999-2b602045ff76",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1424,
        -496
      ],
      "parameters": {
        "color": 4,
        "width": 608,
        "height": 304,
        "content": "## \ud83c\udfac Retrieve & Send Video\n\nFetches the HD video URL from Kie.ai, downloads the generated video, and sends it back to the user on Telegram."
      },
      "typeVersion": 1
    },
    {
      "id": "8d4cfb49-5bf7-43a0-bae2-1c7398f015c7",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -224,
        -736
      ],
      "parameters": {
        "height": 352,
        "content": "##  \ud83d\uddbc\ufe0f Extract Image & Caption\nThis node extracts the product image file ID and its caption text from the Telegram message input for use in later workflow steps."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "76908e76-902b-414a-870a-60c854d05106",
  "connections": {
    "Generate Script": {
      "main": [
        [
          {
            "node": "Upload image to Kie Storage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Video Ready?": {
      "main": [
        [
          {
            "node": "Wait 30 Seconds",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get HD Video URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive Message": {
      "main": [
        [
          {
            "node": "Extract Image & Caption",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 30 Seconds": {
      "main": [
        [
          {
            "node": "Check Video Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get HD Video URL": {
      "main": [
        [
          {
            "node": "Download Generated Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Video Status": {
      "main": [
        [
          {
            "node": "Is Video Ready?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Telegram Image": {
      "main": [
        [
          {
            "node": "Generate Script",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Image & Caption": {
      "main": [
        [
          {
            "node": "Download Telegram Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Generated Video": {
      "main": [
        [
          {
            "node": "Send Video to User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Script",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Upload image to Kie Storage": {
      "main": [
        [
          {
            "node": "Create Video Generation Task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Video Generation Task": {
      "main": [
        [
          {
            "node": "Check Video Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}