{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "d637a8dd-55f9-4f4d-97d8-8d182974baa3",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -192,
        0
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                5
              ]
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "aa118bd6-5408-4798-bf64-2b9e0b9491e4",
      "name": "Get many playlist items",
      "type": "n8n-nodes-base.youTube",
      "position": [
        16,
        0
      ],
      "parameters": {
        "limit": 5,
        "options": {},
        "resource": "playlistItem",
        "operation": "getAll",
        "playlistId": "PLvT_6E7D1NZLQdEUg8cFXLZDnHD7FdRvs"
      },
      "credentials": {
        "youTubeOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f37c8601-a8f6-4f06-8d99-56c271fe6751",
      "name": "video-ids",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        0
      ],
      "parameters": {
        "jsCode": "return $input.all().map(item => ({ videoId: item.json.snippet.resourceId.videoId, videoTitle: item.json.snippet.title }));"
      },
      "typeVersion": 2
    },
    {
      "id": "79c19f39-fd4d-4281-95c5-b6e3c0cb2ae3",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        432,
        0
      ],
      "parameters": {
        "url": "https://www.googleapis.com/youtube/v3/commentThreads",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "videoId",
              "value": "={{ $json.videoId }}"
            },
            {
              "name": "part",
              "value": "snippet"
            },
            {
              "name": "maxResults",
              "value": "100"
            }
          ]
        },
        "nodeCredentialType": "youTubeOAuth2Api"
      },
      "credentials": {
        "youTubeOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "a4ef4caf-ac25-44c3-940a-4ead0b03263d",
      "name": "Code in JavaScript",
      "type": "n8n-nodes-base.code",
      "position": [
        640,
        0
      ],
      "parameters": {
        "jsCode": "\nconst commentItems = $input.all();\n\nconst videoItems = $('video-ids').all();\n\nconst results = [];\n\n\nfor (let i = 0; i < commentItems.length; i++) {\n\n  const currentCommentItem = commentItems[i].json;\n  \n  const correspondingVideoItem = videoItems[i].json;\n  const videoTitle = correspondingVideoItem.videoTitle;\n\n  const commentThreads = currentCommentItem.items || [];\n\n  for (const thread of commentThreads) {\n    const topComment = thread.snippet.topLevelComment;\n    const commentData = {\n      videoTitle: videoTitle, \n      publishedAt: topComment.snippet.publishedAt,\n      author: topComment.snippet.authorDisplayName,\n      videoId: topComment.snippet.videoId,\n      text: topComment.snippet.textDisplay,\n      hasReplies: thread.snippet.totalReplyCount > 0,\n    };\n    results.push({ json: commentData });\n\n    if (thread.replies && thread.replies.comments) {\n      for (const reply of thread.replies.comments) {\n        const replyData = {\n          videoTitle: videoTitle,\n          publishedAt: reply.snippet.publishedAt,\n          author: reply.snippet.authorDisplayName,\n          videoId: reply.snippet.videoId,\n          text: reply.snippet.textDisplay,\n          hasReplies: false,\n        };\n        results.push({ json: replyData });\n      }\n    }\n  }\n}\n\nreturn results;"
      },
      "typeVersion": 2
    },
    {
      "id": "8b10730c-1ae5-4489-9822-b959169feb70",
      "name": "Append row in sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        848,
        0
      ],
      "parameters": {
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "b6a3d178-0b1d-4cde-b283-ba8d6f0ace91",
      "name": "Code in JavaScript1",
      "type": "n8n-nodes-base.code",
      "position": [
        1056,
        0
      ],
      "parameters": {
        "jsCode": "const items = $input.all();\nlet allCommentsText = \"\";\n\nfor (const item of items) {\n  const videoTitle = item.json.videoTitle || 'N/A';\n  const commentText = item.json.text || 'N/A';\n  \n  allCommentsText += `Video: \"${videoTitle}\"\\nComment: \"${commentText}\"\\n---\\n`;\n}\n\n// Yeh check karega ke agar comments na hon to workflow ruk jaye\nif (allCommentsText.length === 0) {\n  return []; // Return empty array to stop workflow\n}\n\nreturn [{ json: { formattedComments: allCommentsText } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "edee7dbd-eab4-45b5-bd53-3769d9071ab7",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1264,
        0
      ],
      "parameters": {
        "text": "=You are a YouTube content strategist for my channel \"WebSensePro\".\n\nAnalyse the following comments and identify the TOP 5 most requested video topics for next week.\n\nMake each title SEO-optimized, and start with \"How to\".\n\n{{ $json.formattedComments }}",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 3
    },
    {
      "id": "72163a2a-5f01-4080-9154-2a20fe225dbb",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1136,
        208
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "gpt-4o-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "28205d71-73ae-4db6-8222-441920c311df",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1440,
        208
      ],
      "parameters": {
        "jsonSchemaExample": "{\n    \"video1\": \"Video 1 Title\",\n    \"video2\": \"Video 2 Title\",\n    \"video3\": \"Video 3 Title\",\n    \"video4\": \"Video 4 Title\",\n    \"video5\": \"Video 5 Title\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "956a3d91-bcfa-4310-8657-1f7d62b56c79",
      "name": "Send a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1584,
        16
      ],
      "parameters": {
        "message": "=Hi, <br><br>  I am your AI Agent and I suggest you should create following videos as per analysis of your audience comments which I did this week. <br><br>  Video 1: {{ $json.output.video1 }} <br> Video 2: {{ $json.output.video2 }} <br> Video 3: {{ $json.output.video3 }} <br> Video 4: {{ $json.output.video4 }} <br> Video 5: {{ $json.output.video5 }} <br> <br>  Thanks!",
        "options": {}
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "d5c51702-4bfd-43bc-9de3-a29290e37f2d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        -256
      ],
      "parameters": {
        "color": 6,
        "width": 288,
        "height": 208,
        "content": "## Step 1: Authenticate APIs \ud83d\udd10\n\nConnect your own credentials for:\n\u2022 YouTube, Google Sheets, & Gmail (OAuth2).\n\u2022 OpenAI (API Key) in the Chat Model node."
      },
      "typeVersion": 1
    },
    {
      "id": "94dd4d07-74db-4c4c-bc17-2d1ce2ad607b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        -304
      ],
      "parameters": {
        "color": 6,
        "height": 224,
        "content": "## Step 2: Configure Data \u2699\ufe0f \n\n\u2022 YouTube Node: Enter your specific Playlist ID.\n\u2022 Google Sheets: Select your Spreadsheet and Sheet Name to log comments."
      },
      "typeVersion": 1
    },
    {
      "id": "e3a0313a-1204-4839-a266-68052244616f",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1152,
        -288
      ],
      "parameters": {
        "color": 6,
        "height": 208,
        "content": "## Step 3: Run & Automate \ud83d\ude80\n\u2022 Click 'Execute Workflow' to test the AI analysis and Email.\n\u2022 Switch to 'Active' for weekly automated reports (Every Friday)."
      },
      "typeVersion": 1
    },
    {
      "id": "d2446441-3681-4144-952d-fcbd426319a4",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -656,
        -512
      ],
      "parameters": {
        "width": 416,
        "height": 432,
        "content": "## \u2699\ufe0f SETUP INSTRUCTIONS:\n1. Connect Credentials \ud83d\udd10\nLink your own accounts to the following nodes:\nGoogle Services: YouTube, Google Sheets, and Gmail.\nAI Power: OpenAI (Add your API Key in the Chat Model node).\n2. Personalize Settings \ud83d\udee0\ufe0f\nYouTube Node: Replace the Playlist ID with your own channel's playlist ID.\nGoogle Sheets: Select your target Spreadsheet and Sheet Name to store comments.\nAI Agent: You can customize the System Prompt if you want a different analysis style.\n3. Test & Activate \ud83d\ude80\nTest: Click 'Execute Workflow' to ensure the AI analyzes comments and sends the email correctly.\nGo Live: Toggle the workflow to 'Active'.\nNote: It is scheduled to run every Friday automatically.\n**Created for WebSensePro | Happy Automating! \ud83e\udd16**"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "video-ids": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get many playlist items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Code in JavaScript1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript1": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many playlist items": {
      "main": [
        [
          {
            "node": "video-ids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}