{
  "name": "Newsletter to LinkedIn Authority",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 8
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "from:(newsletter@example.com OR newsletter@example.com OR newsletter@example.com) is:unread",
          "readStatus": "unread"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        -144,
        -32
      ],
      "id": "3eaea530-5534-41fc-88d4-e50d2748b02e",
      "name": "Gmail Trigger",
      "credentials": {
        "gmailOAuth2": {
          "id": "I2ZG9c2iSpAzX2S0",
          "name": "Gmail account 9"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Newsletter Text: {{ $json.text }}",
        "options": {
          "systemMessage": "=You are a social media content expert who transforms newsletter insights into engaging LinkedIn posts, with the ability to research and enrich the content.\n\nYou have access to the SerpAPI tool to find supporting facts and current context via Google search.\nCurrent date and time: {{$now}}\n\nYour process:\na1) Analyze the newsletter and extract the core message, key insights, and most valuable takeaways\n\na2) **MANDATORY: You MUST use the SerpAPI tool before creating the post:**\n   - **YOU CANNOT SKIP THIS STEP - Search is REQUIRED**\n   - Search for current statistics or case studies related to the newsletter's main topic\n   - Include year \"2025\" to get recent data\n   - Blend the search results naturally into your post\n\na3) Create ONE comprehensive LinkedIn post that captures the newsletter's essence:\n   \n   **LINKEDIN POST REQUIREMENTS:**\n   - **LENGTH: 250-400 words (substantial and comprehensive)**\n   - Start with a powerful hook that challenges conventional thinking\n   - Include the newsletter's main insight or revelation\n   - Add 2-3 supporting points or examples from the newsletter\n   - Incorporate your researched statistics naturally\n   - Use storytelling elements to keep readers engaged\n   - Professional but conversational tone\n   - End with a thought-provoking question\n- Don't mention \"the newsletter\" more than once\n- Present insights as industry knowledge, not \"newsletter says\"\n- Use phrases like \"Recent research shows\" or \"Industry analysis reveals\"\n- Quote sparingly and naturally\n- Make it feel like YOUR analysis informed by the newsletter, not a book report\n- Include 1-2 specific tips or examples from the newsletter\n- Maintain the author's helpful, insider tone\n- Don't just summarize - extract the BEST actionable insight\n- Make readers feel they're getting insider knowledge, not a generic report\n\n   - **FORMAT: Add double line breaks between every 2-3 sentences for mobile readability**\n\na4) Generate ONE image prompt for gpt-image-1 by randomly selecting from these styles:\n   \n   **Option A - Desk Style:**\n   \"Desk photo taken from above, showing notepad with handwritten bullet points about [MAIN NEWSLETTER TOPIC - keep it SHORT, max 5 words], coffee cup, pen, natural messy desk, slightly imperfect framing, photography style\"\n   \n   **Option B - Professional/Office Style:**\n   \"Professional working at laptop in bright office, laptop screen showing [NEWSLETTER MAIN CONCEPT - max 5 words], with semi-transparent overlay containing three SHORT bullet points about [KEY TAKEAWAYS - one word each], corporate stock photo style\"\n   \n   **Option C - Document/Notes Style:**\n   \"Document with highlighted key points about [NEWSLETTER TOPIC - max 3 words] in yellow, showing [ONE MAIN STATISTIC only], clean typography, notes app style. Keep text minimal.\"\n   \n   **INSTRUCTIONS: Choose ONE of the above styles randomly and fill in the bracketed content with actual newsletter information.**\n   \n   **CRITICAL RULES to avoid safety blocks:**\n   - Keep ALL text references to 5 words or less\n   - Use only ONE statistic or number, not multiple\n   - Avoid listing multiple brand names together\n   - Replace detailed explanations with simple keywords\n   - Example: Instead of \"ChatGPT-5: 84.2% accuracy on complex tasks\" just use \"84% accuracy\"\n\na5) Return the output in this EXACT format:\n\n[IMAGE PROMPT]\n{Simple, professional image prompt}\n\n[LINKEDIN POST]\n{Comprehensive LinkedIn post summarizing the newsletter with powerful hook and researched facts}\n"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        32,
        -32
      ],
      "id": "4a5842e6-aab3-476b-9876-ca89e42a29cd",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1",
          "mode": "list",
          "cachedResultName": "gpt-4.1"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        -48,
        144
      ],
      "id": "60cd5c25-cbdb-45fd-aece-e456e2d41006",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "6LnEKBlR3JCiWGdk",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.id }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        112,
        144
      ],
      "id": "fa55542b-d657-4a4a-bc61-f74eb67d7f5a",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.toolSerpApi",
      "typeVersion": 1,
      "position": [
        256,
        144
      ],
      "id": "492acf0e-e919-4552-bdcc-d3e95ed12b53",
      "name": "SerpAPI",
      "credentials": {
        "serpApi": {
          "id": "rQFf3TaI97PCQsCq",
          "name": "SerpAPI account 2"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Extract the AI agent output\nconst aiOutput = $input.first().json.output;\n\n// Extract the image prompt and LinkedIn post\nconst imagePromptMatch = aiOutput.match(/\\[IMAGE PROMPT\\]\\n([\\s\\S]*?)(?=\\n\\[LINKEDIN POST\\])/);\nconst linkedinMatch = aiOutput.match(/\\[LINKEDIN POST\\]\\n([\\s\\S]*?)$/);\n\n// Check if we found all required sections\nif (!imagePromptMatch) {\n  throw new Error('Could not extract image prompt from AI output');\n}\nif (!linkedinMatch) {\n  throw new Error('Could not extract LinkedIn post from AI output');\n}\n\n// Helper function to add line break after every sentence\nfunction addSentenceBreaks(text) {\n  // First, replace any existing single line breaks with double\n  text = text.replace(/\\n/g, '\\n\\n');\n  \n  // Then add double line breaks after sentences that don't already have them\n  // Matches . or ! or ? followed by a space and capital letter\n  text = text.replace(/([.!?:])\\s+(?=[A-Z])/g, '$1\\n\\n');\n  \n  // Clean up any triple or quadruple line breaks\n  text = text.replace(/\\n{3,}/g, '\\n\\n');\n  \n  return text.trim();\n}\n\n// Return the extracted content with formatting fixes\nreturn [{\n  json: {\n    imagePrompt: imagePromptMatch[1].trim(),\n    linkedinPost: addSentenceBreaks(linkedinMatch[1].trim())\n      .replace(/—/g, ' - ')\n      .replace(/\\*/g, ''),  // Remove asterisks\n    newsletterTitle: $('Gmail Trigger').first().json.subject,\n    date: new Date().toISOString(),\n    fullOutput: aiOutput\n  }\n}];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        304,
        -32
      ],
      "id": "86abd9e6-0368-4322-b25e-b9175cf516c1",
      "name": "Clean"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/images/generations",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer INSERT_YOUR_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"gpt-image-1\",\n  \"prompt\": {{ $json.imagePrompt.toJsonString() }},\n  \"n\": 1,\n  \"size\": \"1024x1536\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        448,
        -32
      ],
      "id": "bd596fd6-7a4b-4f2c-834a-84354279c249",
      "name": "Generate Image"
    },
    {
      "parameters": {
        "operation": "toBinary",
        "sourceProperty": "data[0].b64_json",
        "options": {}
      },
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        592,
        -32
      ],
      "id": "2f0846e3-fbf7-4f66-a40e-ddb1497e4d64",
      "name": "Convert to File"
    },
    {
      "parameters": {
        "person": "CuV8AkviSo",
        "text": "={{ $('Clean').item.json.linkedinPost }}",
        "shareMediaCategory": "IMAGE",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.linkedIn",
      "typeVersion": 1,
      "position": [
        752,
        -32
      ],
      "id": "5d118d31-c486-4e4f-a820-2a7cbebccf66",
      "name": "Create a post",
      "credentials": {
        "linkedInOAuth2Api": {
          "id": "xjLJt46VEfuLQtyj",
          "name": "LinkedIn account 4"
        }
      }
    },
    {
      "parameters": {
        "chatId": "INSERT_CHAT_ID",
        "text": "=This post was just published: \n\nNewsletter: {{ $('Gmail Trigger').item.json.headers.subject }}\n\nPost Text:\n\n{{ $('Clean').item.json.linkedinPost }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        896,
        -32
      ],
      "id": "c6dcc8a7-0cb7-4867-822e-9da4a9febedb",
      "name": "Send a text message",
      "webhookId": "85c2a70f-d803-4de9-8687-56b19c5d72cd",
      "credentials": {
        "telegramApi": {
          "id": "S7tsoRRidBQ5H8HC",
          "name": "Telegram account 35"
        }
      }
    },
    {
      "parameters": {
        "operation": "sendPhoto",
        "chatId": "INSERT_CHAT_ID",
        "binaryData": true,
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        752,
        144
      ],
      "id": "bb8b9d03-abaf-48ad-8a55-8c035ad6d704",
      "name": "Send a photo message",
      "webhookId": "4dbd502b-044b-46e5-bd7b-6b3924ee9244",
      "credentials": {
        "telegramApi": {
          "id": "S7tsoRRidBQ5H8HC",
          "name": "Telegram account 35"
        }
      }
    },
    {
      "parameters": {
        "operation": "markAsRead",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}"
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1056,
        -32
      ],
      "id": "e9251de2-d078-4744-97cf-6f2264f3f6ae",
      "name": "Mark a message as read",
      "webhookId": "71d909e6-bb4c-48fd-a69d-576781092b77",
      "credentials": {
        "gmailOAuth2": {
          "id": "I2ZG9c2iSpAzX2S0",
          "name": "Gmail account 9"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "SerpAPI": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Clean",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean": {
      "main": [
        [
          {
            "node": "Generate Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Image": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Create a post",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send a photo message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a post": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        [
          {
            "node": "Mark a message as read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "712e635e-f26c-4b6f-98fb-d09f40c47440",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "7ff6625f0127f3e9a7865e3493009de1c48f57dc5ed87def3ae3316be7f6b238"
  },
  "id": "IrvAfbqWjJsdXbuT",
  "tags": []
}