{
  "name": "Auto Social from Blog Post",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "wordpress-published",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-wp-publish",
      "name": "WordPress Published Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Parse WordPress webhook payload\nconst wpData = $input.first().json;\n\n// Handle both WP webhook formats\nconst post = wpData.post || wpData;\n\n// Extract post data\nconst title = post.post_title || post.title?.rendered || '';\nconst content = post.post_content || post.content?.rendered || '';\nconst excerpt = post.post_excerpt || post.excerpt?.rendered || '';\nconst url = post.guid || post.link || '';\nconst categories = post.categories || [];\nconst tags = post.tags || [];\nconst featuredImage = post.featured_media_url || post._embedded?.['wp:featuredmedia']?.[0]?.source_url || '';\n\n// Strip HTML from content\nconst stripHtml = (html) => {\n  return html\n    .replace(/<script[^>]*>[\\s\\S]*?<\\/script>/gi, '')\n    .replace(/<style[^>]*>[\\s\\S]*?<\\/style>/gi, '')\n    .replace(/<[^>]+>/g, ' ')\n    .replace(/\\s+/g, ' ')\n    .trim();\n};\n\nconst cleanContent = stripHtml(content).substring(0, 3000);\nconst cleanExcerpt = stripHtml(excerpt);\n\nreturn {\n  postId: post.ID || post.id,\n  title,\n  content: cleanContent,\n  excerpt: cleanExcerpt || cleanContent.substring(0, 300) + '...',\n  url,\n  categories,\n  tags,\n  featuredImage,\n  publishedAt: post.post_date || post.date || new Date().toISOString()\n};"
      },
      "id": "parse-wp-data",
      "name": "Parse WordPress Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extract key points from the blog post for social media\nconst post = $input.first().json;\n\n// Build context for AI generation\nconst socialContext = {\n  title: post.title,\n  keyContent: post.excerpt || post.content.substring(0, 500),\n  fullContent: post.content,\n  url: post.url,\n  tags: post.tags,\n  // Use tags as potential hashtags\n  suggestedHashtags: post.tags.map(t => t.name || t).filter(Boolean)\n};\n\nreturn socialContext;"
      },
      "id": "extract-key-points",
      "name": "Extract Key Points",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$env.MAXIMO_API_URL}}/api/n8n/social/generate-all",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"Title: {{ $json.title }}\\n\\nSummary: {{ $json.keyContent }}\\n\\nFull Article: {{ $json.fullContent }}\\n\\nArticle URL: {{ $json.url }}\",\n  \"platforms\": [\"twitter\", \"linkedin\", \"facebook\", \"instagram\"],\n  \"tone\": \"professional\",\n  \"includeUrl\": true,\n  \"url\": \"{{ $json.url }}\",\n  \"suggestedHashtags\": {{ JSON.stringify($json.suggestedHashtags) }}\n}",
        "options": {
          "timeout": 60000
        }
      },
      "id": "call-maximo-social",
      "name": "Call Maximo Social Generator",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Prepare posts with optimal scheduling\nconst generated = $input.first().json;\nconst postData = $('Parse WordPress Data').first().json;\n\n// Optimal posting times (configurable)\nconst now = new Date();\nconst optimalTimes = {\n  twitter: [9, 12, 17], // 9am, 12pm, 5pm\n  linkedin: [8, 10, 12], // 8am, 10am, 12pm\n  facebook: [13, 16, 19], // 1pm, 4pm, 7pm\n  instagram: [11, 14, 19] // 11am, 2pm, 7pm\n};\n\n// Find next optimal time for each platform\nconst getNextOptimalTime = (platform) => {\n  const times = optimalTimes[platform] || [12];\n  const currentHour = now.getHours();\n  \n  // Find next time today or tomorrow\n  for (const hour of times) {\n    if (hour > currentHour + 1) {\n      const scheduled = new Date(now);\n      scheduled.setHours(hour, 0, 0, 0);\n      return scheduled.toISOString();\n    }\n  }\n  \n  // Schedule for tomorrow's first optimal time\n  const tomorrow = new Date(now);\n  tomorrow.setDate(tomorrow.getDate() + 1);\n  tomorrow.setHours(times[0], 0, 0, 0);\n  return tomorrow.toISOString();\n};\n\nconst posts = [];\n\n// Add URL to posts and prepare for each platform\nconst addUrl = (text, url) => text.includes(url) ? text : `${text}\\n\\n\ud83d\udcd6 Read more: ${url}`;\n\nif (generated.twitter) {\n  const fullText = generated.twitter.text + \n    (generated.twitter.hashtags?.length ? ' ' + generated.twitter.hashtags.map(h => '#' + h).join(' ') : '');\n  posts.push({\n    platform: 'twitter',\n    text: addUrl(fullText, postData.url).substring(0, 280),\n    scheduledTime: getNextOptimalTime('twitter'),\n    postTitle: postData.title,\n    postUrl: postData.url\n  });\n}\n\nif (generated.linkedin) {\n  posts.push({\n    platform: 'linkedin',\n    text: addUrl(generated.linkedin.text, postData.url),\n    scheduledTime: getNextOptimalTime('linkedin'),\n    postTitle: postData.title,\n    postUrl: postData.url\n  });\n}\n\nif (generated.facebook) {\n  posts.push({\n    platform: 'facebook',\n    text: addUrl(generated.facebook.text, postData.url),\n    scheduledTime: getNextOptimalTime('facebook'),\n    postTitle: postData.title,\n    postUrl: postData.url\n  });\n}\n\nif (generated.instagram) {\n  const caption = generated.instagram.text + \n    (generated.instagram.hashtags?.length ? '\\n\\n' + generated.instagram.hashtags.map(h => '#' + h).join(' ') : '');\n  posts.push({\n    platform: 'instagram',\n    text: addUrl(caption, postData.url),\n    scheduledTime: getNextOptimalTime('instagram'),\n    postTitle: postData.title,\n    postUrl: postData.url,\n    imageUrl: postData.featuredImage\n  });\n}\n\nreturn posts.map(post => ({ json: post }));"
      },
      "id": "prepare-auto-posts",
      "name": "Prepare Posts with Scheduling",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-twitter",
              "leftValue": "={{ $json.platform }}",
              "rightValue": "twitter",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "is-linkedin",
              "leftValue": "={{ $json.platform }}",
              "rightValue": "linkedin",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "is-facebook",
              "leftValue": "={{ $json.platform }}",
              "rightValue": "facebook",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "is-instagram",
              "leftValue": "={{ $json.platform }}",
              "rightValue": "instagram",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "id": "platform-router",
      "name": "Route to Platforms",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "text": "={{ $json.text }}",
        "additionalFields": {}
      },
      "id": "post-twitter",
      "name": "Post to Twitter",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 2,
      "position": [
        1560,
        100
      ],
      "credentials": {
        "twitterOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "url": "https://api.linkedin.com/v2/shares",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": {\n    \"contentEntities\": [\n      {\n        \"entityLocation\": \"{{ $json.postUrl }}\"\n      }\n    ],\n    \"title\": \"{{ $json.postTitle }}\"\n  },\n  \"distribution\": {\n    \"linkedInDistributionTarget\": {}\n  },\n  \"owner\": \"urn:li:person:{{ $env.LINKEDIN_PERSON_ID }}\",\n  \"text\": {\n    \"text\": \"{{ $json.text.replace(/\"/g, '\\\\\"') }}\"\n  }\n}",
        "options": {}
      },
      "id": "post-linkedin",
      "name": "Post to LinkedIn",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        260
      ],
      "credentials": {
        "oAuth2Api": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "url": "https://graph.facebook.com/v18.0/{{$env.FACEBOOK_PAGE_ID}}/feed",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"message\": \"{{ $json.text.replace(/\"/g, '\\\\\"') }}\",\n  \"link\": \"{{ $json.postUrl }}\"\n}",
        "options": {}
      },
      "id": "post-facebook",
      "name": "Post to Facebook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        420
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{$env.SOCIAL_TRACKING_SHEET_ID}}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Auto Posts",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Platform": "={{ $json.platform }}",
            "Blog Title": "={{ $json.postTitle }}",
            "Blog URL": "={{ $json.postUrl }}",
            "Post Text": "={{ $json.text.substring(0, 500) }}",
            "Scheduled Time": "={{ $json.scheduledTime }}",
            "Status": "=posted",
            "Created At": "={{ new Date().toISOString() }}"
          },
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "save-to-tracking",
      "name": "Save to Tracking Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [
        1780,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Aggregate all posting results\nconst items = $input.all();\nconst wpData = $('Parse WordPress Data').first().json;\n\nconst results = {\n  success: true,\n  blogPost: {\n    title: wpData.title,\n    url: wpData.url\n  },\n  postedTo: items.map(item => ({\n    platform: item.json.platform,\n    status: 'posted',\n    scheduledTime: item.json.scheduledTime\n  })),\n  timestamp: new Date().toISOString()\n};\n\nreturn results;"
      },
      "id": "aggregate-auto-results",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2000,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "respond-auto-success",
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2220,
        300
      ]
    }
  ],
  "connections": {
    "WordPress Published Webhook": {
      "main": [
        [
          {
            "node": "Parse WordPress Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse WordPress Data": {
      "main": [
        [
          {
            "node": "Extract Key Points",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Key Points": {
      "main": [
        [
          {
            "node": "Call Maximo Social Generator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Maximo Social Generator": {
      "main": [
        [
          {
            "node": "Prepare Posts with Scheduling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Posts with Scheduling": {
      "main": [
        [
          {
            "node": "Route to Platforms",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route to Platforms": {
      "main": [
        [
          {
            "node": "Post to Twitter",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Post to LinkedIn",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Post to Facebook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post to Twitter": {
      "main": [
        [
          {
            "node": "Save to Tracking Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post to LinkedIn": {
      "main": [
        [
          {
            "node": "Save to Tracking Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post to Facebook": {
      "main": [
        [
          {
            "node": "Save to Tracking Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Tracking Sheet": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Respond Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "staticData": null,
  "tags": [
    {
      "name": "Social Media",
      "id": "social-media-tag"
    },
    {
      "name": "WordPress",
      "id": "wordpress-tag"
    },
    {
      "name": "Automation",
      "id": "automation-tag"
    }
  ],
  "meta": {
    "templateId": "maximo-auto-social-blog"
  }
}