{
  "name": "A - UGC Opportunity Alert Bot",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 5,
              "unit": "minutes"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "(sideshift OR billo OR joinbrands OR insense OR collabstr OR \"UGC opportunity\" OR \"new brief\") newer_than:1d"
        }
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        -400,
        0
      ],
      "id": "gmail-trigger-1",
      "name": "New UGC Platform Email",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Pull the useful bits out of the raw Gmail item\nconst items = $input.all();\nreturn items.map(item => {\n  const j = item.json;\n  return {\n    json: {\n      subject: j.subject || j.Subject || '',\n      from: j.from || j.From || '',\n      snippet: j.snippet || j.text || j.textPlain || '',\n      gmail_link: j.threadId ? `https://mail.google.com/mail/u/0/#inbox/${j.threadId}` : (j.id ? `https://mail.google.com/mail/u/0/#inbox/${j.id}` : ''),\n      received_at: j.date || j.internalDate || new Date().toISOString()\n    }\n  };\n});\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -160,
        0
      ],
      "id": "code-extract-1",
      "name": "Extract Brief Info"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: \"gpt-4.1-mini\", response_format: { type: \"json_object\" }, messages: [ { role: \"system\", content: \"You screen UGC (user-generated content) brand brief emails for a creator whose strategy is to chase niches that are TRENDING right now or where demand is rising faster than the supply of creators applying (low competition, high demand). Given the email subject/snippet, respond ONLY with compact JSON, no prose, in this exact shape: {\\\"brand\\\":\\\"\\\",\\\"platform\\\":\\\"\\\",\\\"niche\\\":\\\"\\\",\\\"trend_score\\\":0,\\\"low_supply_high_demand\\\":true,\\\"should_alert\\\":true,\\\"reasoning\\\":\\\"\\\"}. trend_score is 1-10 (10 = extremely hot/rising niche or urgent high-paying brief). should_alert should be true only when trend_score is 7 or higher OR the niche looks under-served.\" }, { role: \"user\", content: `Subject: ${$json.subject}\\nFrom: ${$json.from}\\nSnippet: ${$json.snippet}` } ] }) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        80,
        0
      ],
      "id": "openai-score-1",
      "name": "Score Opportunity",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nreturn items.map((item, i) => {\n  const source = $('Extract Brief Info').all()[i]?.json || {};\n  let parsed = {};\n  try {\n    const raw = item.json.choices?.[0]?.message?.content || item.json.message?.content || item.json.content || item.json.text || '{}';\n    parsed = JSON.parse(raw);\n  } catch (e) {\n    parsed = { brand: 'Unknown', platform: 'Unknown', niche: 'Unknown', trend_score: 0, low_supply_high_demand: false, should_alert: false, reasoning: 'Could not parse AI response' };\n  }\n  return {\n    json: {\n      date: new Date().toISOString().slice(0,10),\n      brand: parsed.brand || 'Unknown',\n      platform: parsed.platform || 'Unknown',\n      niche: parsed.niche || 'Unknown',\n      trend_score: parsed.trend_score || 0,\n      reasoning: parsed.reasoning || '',\n      should_alert: !!parsed.should_alert,\n      status: 'Not Applied',\n      gmail_link: source.gmail_link || ''\n    }\n  };\n});\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        0
      ],
      "id": "code-parse-1",
      "name": "Parse AI Score"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Opportunities",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Date": "={{ $json.date }}",
            "Brand": "={{ $json.brand }}",
            "Platform": "={{ $json.platform }}",
            "Niche": "={{ $json.niche }}",
            "TrendScore": "={{ $json.trend_score }}",
            "Status": "={{ $json.status }}",
            "GmailLink": "={{ $json.gmail_link }}",
            "Reasoning": "={{ $json.reasoning }}"
          }
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        560,
        0
      ],
      "id": "sheets-log-1",
      "name": "Log to Opportunities Sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.should_alert }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        780,
        0
      ],
      "id": "if-highpotential-1",
      "name": "Is High Potential?"
    },
    {
      "parameters": {
        "chatId": "REPLACE_WITH_YOUR_TELEGRAM_CHAT_ID",
        "text": "=\ud83d\udd25 New UGC opportunity ({{ $json.trend_score }}/10)\nBrand: {{ $json.brand }}\nPlatform: {{ $json.platform }}\nNiche: {{ $json.niche }}\nWhy: {{ $json.reasoning }}\nOpen: {{ $json.gmail_link }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1000,
        0
      ],
      "id": "telegram-alert-1",
      "name": "Send Telegram Alert",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "New UGC Platform Email": {
      "main": [
        [
          {
            "node": "Extract Brief Info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Brief Info": {
      "main": [
        [
          {
            "node": "Score Opportunity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score Opportunity": {
      "main": [
        [
          {
            "node": "Parse AI Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Score": {
      "main": [
        [
          {
            "node": "Log to Opportunities Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log to Opportunities Sheet": {
      "main": [
        [
          {
            "node": "Is High Potential?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is High Potential?": {
      "main": [
        [
          {
            "node": "Send Telegram Alert",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {}
}