{
  "name": "TechServJo \u2014 AI Blog Generator (Telegram)",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "n_tg_trigger",
      "name": "Telegram: Receive Idea",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// Extract the post idea and sender info from Telegram message\nconst msg = $json.message || $json.edited_message || {};\nconst text = msg.text || '';\nconst chatId = String(msg.chat?.id || '');\nconst username = msg.from?.first_name || msg.from?.username || '\u0639\u0632\u064a\u0632\u064a';\n\n// Strip bot commands like /blog /post /write\nconst idea = text.replace(/^\\/\\w+\\s*/,'').trim();\n\n// Reject empty messages\nif (!idea) {\n  return [{json:{ skip: true, chatId, username, idea:'' }}];\n}\n\nreturn [{json:{ idea, chatId, username, skip: false }}];"
      },
      "id": "n_extract_idea",
      "name": "Code: Extract Idea & Sender",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "id": "c_skip",
              "leftValue": "={{ $json.skip }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "notTrue"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "n_check_empty",
      "name": "IF: Has Idea?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "=\u0623\u0631\u0633\u0644 \u0644\u064a \u0641\u0643\u0631\u0629 \u0623\u0648 \u0639\u0646\u0648\u0627\u0646\u064b\u0627 \u0644\u0645\u0642\u0627\u0644\u0643\u060c \u0645\u062b\u0627\u0644:\n\n<i>\u0623\u062a\u0645\u062a\u0629 \u0627\u0644\u0641\u0648\u0627\u062a\u064a\u0631 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 n8n</i>\n<i>\u062a\u0634\u0637\u064a\u0628 \u0645\u0643\u0627\u062a\u0628 \u0639\u0645\u064e\u0651\u0627\u0646 2026</i>",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "n_tg_prompt",
      "name": "Telegram: Ask for Idea",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        660,
        480
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "=\u23f3 <b>\u0645\u0645\u062a\u0627\u0632 {{ $json.username }}!</b>\n\n\u062c\u0627\u0631\u064a \u062a\u0648\u0644\u064a\u062f \u0645\u0642\u0627\u0644 \u0627\u062d\u062a\u0631\u0627\u0641\u064a \u0639\u0646:\n<i>\"{{ $json.idea }}\"</i>\n\n\ud83e\udd16 \u064a\u0639\u0645\u0644 \u0627\u0644\u0630\u0643\u0627\u0621 \u0627\u0644\u0627\u0635\u0637\u0646\u0627\u0639\u064a \u0627\u0644\u0622\u0646...",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "n_tg_ack",
      "name": "Telegram: Acknowledge \u2014 Generating",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        660,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "text",
        "operation": "message",
        "modelId": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a professional bilingual content writer for TechServJo \u2014 a company in Jordan offering AI automation, n8n workflow automation, voice agents, and office fit-out/renovation services. Your job is to write high-quality, SEO-optimized blog posts useful for businesses in Jordan and the MENA region.\n\nRules:\n- Write substantial, valuable content (min 400 words per language).\n- Arabic content should be in Modern Standard Arabic (\u0641\u0635\u062d\u0649), professional and clear.\n- Include practical examples, numbered lists, and actionable advice.\n- Content must be relevant to TechServJo's services: AI, automation, n8n, office design, digital transformation.\n- Return ONLY a valid JSON object. No markdown, no code blocks, no explanation outside the JSON.\n\nJSON schema to return:\n{\n  \"title_ar\": \"\u0639\u0646\u0648\u0627\u0646 \u0627\u0644\u0645\u0642\u0627\u0644 \u0628\u0627\u0644\u0639\u0631\u0628\u064a\",\n  \"title_en\": \"Post title in English\",\n  \"slug\": \"url-friendly-slug-in-english\",\n  \"excerpt_ar\": \"2-3 sentences summary in Arabic\",\n  \"excerpt_en\": \"2-3 sentences summary in English\",\n  \"content_ar\": \"Full HTML content. Use <h2>, <h3>, <p>, <ul>, <li>, <strong>. Min 400 words.\",\n  \"content_en\": \"Full HTML content. Use <h2>, <h3>, <p>, <ul>, <li>, <strong>. Min 400 words.\",\n  \"category\": \"ai-automation OR fit-out OR workflow-automation OR digital-transformation\",\n  \"category_ar\": \"\u0627\u0633\u0645 \u0627\u0644\u0641\u0626\u0629 \u0628\u0627\u0644\u0639\u0631\u0628\u064a\",\n  \"category_en\": \"Category Name\",\n  \"tags\": [\"tag1\", \"tag2\", \"tag3\", \"tag4\"],\n  \"read_time\": 5,\n  \"image_url\": \"https://images.unsplash.com/photo-1677442136019-21780ecad995?auto=format&fit=crop&q=80&w=800\"\n}\n\nUse a relevant Unsplash URL from this list based on category:\n- AI/Tech: https://images.unsplash.com/photo-1677442136019-21780ecad995?auto=format&fit=crop&q=80&w=800\n- Automation/Workflow: https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&q=80&w=800\n- Office/Fitout: https://images.unsplash.com/photo-1497366216548-37526070297c?auto=format&fit=crop&q=80&w=800\n- Business/Strategy: https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?auto=format&fit=crop&q=80&w=800"
            },
            {
              "role": "user",
              "content": "=\u0627\u0643\u062a\u0628 \u0645\u0642\u0627\u0644 \u0628\u0627\u0644\u0639\u0631\u0628\u064a \u0648\u0627\u0644\u0625\u0646\u062c\u0644\u064a\u0632\u064a \u0639\u0646: \"{{ $('Code: Extract Idea & Sender').first().json.idea }}\"\n\n\u062a\u0623\u0643\u062f \u0645\u0646 \u0623\u0646 \u0627\u0644\u0645\u0642\u0627\u0644 \u0645\u0646\u0627\u0633\u0628 \u0644\u062c\u0645\u0647\u0648\u0631 \u0627\u0644\u0623\u0631\u062f\u0646 \u0648\u0627\u0644\u0645\u0646\u0637\u0642\u0629 \u0627\u0644\u0639\u0631\u0628\u064a\u0629\u060c \u0648\u0645\u0631\u062a\u0628\u0637 \u0628\u062e\u062f\u0645\u0627\u062a TechServJo."
            }
          ]
        },
        "options": {
          "maxTokens": 4000,
          "temperature": 0.7
        }
      },
      "id": "n_openai",
      "name": "OpenAI: Generate Bilingual Post",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.3,
      "position": [
        880,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// Parse the AI response and structure it for the publisher workflow\nconst aiText = $input.first().json.message?.content || $input.first().json.choices?.[0]?.message?.content || '';\nconst chatId = $('Code: Extract Idea & Sender').first().json.chatId;\n\nlet post;\ntry {\n  // Handle response with or without code fences\n  const clean = aiText.replace(/^```(?:json)?\\n?/,'').replace(/\\n?```$/,'').trim();\n  post = JSON.parse(clean);\n} catch(e) {\n  // Try to extract JSON object from response\n  const match = aiText.match(/\\{[\\s\\S]*\\}/);\n  if (match) { post = JSON.parse(match[0]); }\n  else { throw new Error('AI did not return valid JSON: ' + aiText.substring(0,200)); }\n}\n\n// Auto-generate slug if missing\nif (!post.slug) {\n  post.slug = (post.title_en||'post').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/(^-|-$)/g,'');\n}\n\n// Add metadata\npost.date = new Date().toISOString().split('T')[0];\npost.author = 'TechServJo Team';\npost.chatId = chatId;\n\nreturn [{json: post}];"
      },
      "id": "n_parse_ai",
      "name": "Code: Parse AI Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "=\u270d\ufe0f <b>\u062a\u0645 \u062a\u0648\u0644\u064a\u062f \u0627\u0644\u0645\u0642\u0627\u0644!</b>\n\n\ud83d\udcf0 <b>{{ $json.title_ar }}</b>\n\ud83c\udff7 {{ $json.category_ar }} | \u23f1 {{ $json.read_time }} \u062f\u0642\u0627\u0626\u0642\n\n\ud83d\udd04 \u062c\u0627\u0631\u064a \u0627\u0644\u0646\u0634\u0631 \u0639\u0644\u0649 \u0627\u0644\u0645\u0648\u0642\u0639...",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "n_tg_generated",
      "name": "Telegram: Status \u2014 Content Ready",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1320,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR-N8N-DOMAIN/webhook/publish-blog-post",
        "sendBody": true,
        "body": {
          "mode": "json",
          "jsonBody": "={{ JSON.stringify($('Code: Parse AI Response').first().json) }}"
        },
        "options": {
          "timeout": 60000
        }
      },
      "id": "n_call_publisher",
      "name": "HTTP: Trigger Publisher Workflow",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1540,
        300
      ]
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// Check publisher response and prepare final message\nconst result = $input.first().json;\nconst post = $('Code: Parse AI Response').first().json;\nconst chatId = post.chatId;\n\nif (result.success) {\n  return [{json:{\n    chatId,\n    success: true,\n    message: `\u2705 *\u062a\u0645 \u0646\u0634\u0631 \u0627\u0644\u0645\u0642\u0627\u0644 \u0628\u0646\u062c\u0627\u062d!*\\n\\n\ud83d\udcf0 *${post.title_ar}*\\n\ud83d\udd17 ${result.url}\\n\\n\u23f1 \u0633\u064a\u0643\u0648\u0646 \u0645\u062a\u0627\u062d\u0627\u064b \u0639\u0644\u0649 \u0627\u0644\u0645\u0648\u0642\u0639 \u062e\u0644\u0627\u0644 ~2 \u062f\u0642\u064a\u0642\u0629`,\n    url: result.url\n  }}];\n} else {\n  return [{json:{\n    chatId,\n    success: false,\n    message: `\u274c *\u062d\u062f\u062b \u062e\u0637\u0623 \u0623\u062b\u0646\u0627\u0621 \u0627\u0644\u0646\u0634\u0631*\\n\\n${result.error || 'Unknown error'}\\n\\n\u062d\u0627\u0648\u0644 \u0645\u0631\u0629 \u0623\u062e\u0631\u0649 \u0623\u0648 \u062a\u0648\u0627\u0635\u0644 \u0645\u0639 \u0627\u0644\u0645\u0637\u0648\u0631.`\n  }}];\n}"
      },
      "id": "n_check_result",
      "name": "Code: Check Publish Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1760,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "disable_web_page_preview": false
        }
      },
      "id": "n_tg_final",
      "name": "Telegram: Final Result",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1980,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram: Receive Idea": {
      "main": [
        [
          {
            "node": "Code: Extract Idea & Sender",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Extract Idea & Sender": {
      "main": [
        [
          {
            "node": "IF: Has Idea?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: Has Idea?": {
      "main": [
        [
          {
            "node": "Telegram: Acknowledge \u2014 Generating",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Telegram: Ask for Idea",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram: Acknowledge \u2014 Generating": {
      "main": [
        [
          {
            "node": "OpenAI: Generate Bilingual Post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI: Generate Bilingual Post": {
      "main": [
        [
          {
            "node": "Code: Parse AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Parse AI Response": {
      "main": [
        [
          {
            "node": "Telegram: Status \u2014 Content Ready",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram: Status \u2014 Content Ready": {
      "main": [
        [
          {
            "node": "HTTP: Trigger Publisher Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: Trigger Publisher Workflow": {
      "main": [
        [
          {
            "node": "Code: Check Publish Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Check Publish Result": {
      "main": [
        [
          {
            "node": "Telegram: Final Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true
  },
  "tags": [
    "blog",
    "telegram",
    "openai",
    "techservjo",
    "ai-generator"
  ],
  "versionId": "1"
}