{
  "id": "VjbCxJ8Hgo44WZdf",
  "name": "TelegramAutoCopier",
  "tags": [],
  "nodes": [
    {
      "id": "aeb0620d-095c-4238-8dd0-0232c4905e19",
      "name": "Daily Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "notes": "Triggers the workflow daily at 9 AM. Adjust the time according to your needs.",
      "position": [
        -1220,
        -60
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 9
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3ea234cc-4f02-4216-8013-554eb1fba0fd",
      "name": "Fetch Channel Page",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "Fetches the HTML content of the source Telegram channel",
      "position": [
        -780,
        -60
      ],
      "parameters": {
        "url": "=https://t.me/s/{{ $('Set Telegram Channels').item.json.sourceChannel }}",
        "options": {},
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/0.0.0.0 Safari/537.36"
            }
          ]
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "eb0d33be-2e66-43cc-90fa-d1b9878615d9",
      "name": "Extract Post Content",
      "type": "n8n-nodes-base.html",
      "notes": "Extracts text, images, and videos from the latest Telegram post",
      "position": [
        -560,
        -60
      ],
      "parameters": {
        "options": {
          "trimValues": false,
          "cleanUpText": false
        },
        "operation": "extractHtmlContent",
        "extractionValues": {
          "values": [
            {
              "key": "post",
              "cssSelector": "div.tgme_widget_message_wrap:last-of-type div.js-message_text"
            },
            {
              "key": "postId",
              "cssSelector": "div.tgme_widget_message_wrap:last-of-type div.tgme_widget_message_info span.tgme_widget_message_meta"
            },
            {
              "key": "video",
              "attribute": "src",
              "cssSelector": "div.tgme_widget_message_wrap:last-of-type div.tgme_widget_message_bubble a div.tgme_widget_message_video_wrap > video",
              "returnValue": "attribute"
            },
            {
              "key": "photo",
              "attribute": "style",
              "cssSelector": "div.tgme_widget_message_wrap:last-of-type div.tgme_widget_message_bubble div.media_supported_cont > a.tgme_widget_message_photo_wrap",
              "returnValue": "attribute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "715418a9-9598-460a-817e-e2b681f336c4",
      "name": "Clean Post Content",
      "type": "n8n-nodes-base.code",
      "notes": "Removes original channel signature and cleans up the text",
      "position": [
        -340,
        -60
      ],
      "parameters": {
        "jsCode": "// Clean up the post content by removing original channel signature\nif ($input.first().json.post) {\n  let cleanPost = $input.first().json.post    \n    .replaceAll($('Set Telegram Channels').first().json.channelSignature, '')\n    .trim();\n  \n  $input.first().json.post = cleanPost;\n}\n\nreturn $input.all();"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "31508b88-fd99-4b7c-abb7-52084caf9373",
      "name": "Translate Text",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "notes": "Uses OpenAI to translate the post content to the target language",
      "position": [
        -120,
        -60
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "=Translate the following text to {{ $('Set Telegram Channels').item.json.targetLanguage }} in a friendly, natural style.\nReturn only the translated text without any additional commentary or formatting.\n\nText to translate:\n{{ $json.post }}"
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 1.8
    },
    {
      "id": "e0017199-a33a-4866-9fa8-3be34fbf20e8",
      "name": "Process Media URLs",
      "type": "n8n-nodes-base.code",
      "notes": "Extracts clean URLs from CSS style attributes for images",
      "position": [
        256,
        -60
      ],
      "parameters": {
        "jsCode": "// Process photo URL from CSS style attribute\nif ($('Extract Post Content').first().json.photo) {\n  const styleAttr = $('Extract Post Content').first().json.photo;\n  const urlMatch = styleAttr.match(/background-image:url\\('([^']+)'\\)/);\n  \n  if (urlMatch && urlMatch[1]) {\n    $input.first().json.photo = urlMatch[1];\n  } else {\n    // Fallback: try to extract from quotes\n    const arr = styleAttr.split(\"'\");\n    if (arr.length >= 2) {\n      $input.first().json.photo = arr[arr.length - 2];\n    }\n  }\n}\n\nreturn $input.all();"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "8f05161f-776b-4684-9acc-d2cc426ad78c",
      "name": "Has Photo?",
      "type": "n8n-nodes-base.if",
      "notes": "Checks if the post contains a photo",
      "position": [
        476,
        -260
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "202a35e0-040c-47d2-9aaa-2f7d295ac2dd",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.photo }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "70c14a77-b297-4ac2-9ec4-a9d7124b7ce4",
      "name": "Has Video?",
      "type": "n8n-nodes-base.if",
      "notes": "Checks if the post contains a video",
      "position": [
        476,
        140
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "95dcac89-39c4-4a0a-8c1c-e1c560be755f",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $('Extract Post Content').item.json.video }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "11abbf2e-043b-4129-b15b-f625bb12802a",
      "name": "Text Only?",
      "type": "n8n-nodes-base.if",
      "position": [
        476,
        -60
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "7d0504ca-12c6-4496-8879-3f7d295ac2dd",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $('Extract Post Content').item.json.video }}",
              "rightValue": ""
            },
            {
              "id": "3cca8e10-546a-4b42-ac6f-0cc91773949c",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $('Extract Post Content').item.json.photo }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "926e4cca-4028-43bf-98ae-4f1e6f38fc08",
      "name": "Download Photo",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "Downloads the photo from Telegram servers",
      "position": [
        696,
        -260
      ],
      "parameters": {
        "url": "={{ $json.photo }}",
        "options": {
          "timeout": 30000
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "a2349dfe-45f9-4688-a915-0af0587a0e48",
      "name": "Download Video",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "Downloads the video from Telegram servers",
      "position": [
        696,
        140
      ],
      "parameters": {
        "url": "={{ $('Extract Post Content').item.json.video }}",
        "options": {
          "timeout": 60000
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "28a45272-c1a7-44cc-b396-aa40485baf37",
      "name": "Send Photo to Channel",
      "type": "n8n-nodes-base.telegram",
      "notes": "Sends the translated post with photo to target channel",
      "position": [
        916,
        -260
      ],
      "parameters": {
        "chatId": "={{ $('Set Telegram Channels').item.json.targetChannel }}",
        "operation": "sendPhoto",
        "binaryData": true,
        "additionalFields": {
          "caption": "={{ $json.message.content }}\\n{{ $('Set Telegram Channels').item.json.channelSignature }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "cd3f5b26-0250-4a12-9dec-2b82891b1daf",
      "name": "Send Video to Channel",
      "type": "n8n-nodes-base.telegram",
      "notes": "Sends the translated post with video to target channel",
      "position": [
        916,
        140
      ],
      "parameters": {
        "chatId": "={{ $('Set Telegram Channels').item.json.targetChannel }}",
        "operation": "sendVideo",
        "binaryData": true,
        "additionalFields": {
          "caption": "={{ $json.message.content }}\\n{{ $('Set Telegram Channels').item.json.channelSignature }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2737cc48-5c12-442a-8f6f-15a4e77f91c4",
      "name": "Send Text to Channel",
      "type": "n8n-nodes-base.telegram",
      "notes": "Sends text-only translated posts to target channel",
      "position": [
        696,
        -60
      ],
      "parameters": {
        "text": "={{ $json.message.content }}\\n{{ $('Set Telegram Channels').item.json.channelSignature }}",
        "chatId": "={{ $('Set Telegram Channels').item.json.targetChannel }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "9cc2d449-c59a-460f-bb50-236cd914783c",
      "name": "\ud83d\udcd6 Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1400,
        -760
      ],
      "parameters": {
        "width": 500,
        "height": 620,
        "content": "## Telegram Channel Content Copier & Translator\n**Automatically copies and translates content from one Telegram channel to another**\n\n### What this workflow does:\n- \ud83d\udcc5 Runs daily at a scheduled time\n- \ud83d\udd0d Fetches the latest post from a source Telegram channel\n- \ud83c\udf10 Translates the content using OpenAI\n- \ud83d\udce4 Republishes to your target channel\n- \ud83d\uddbc\ufe0f Supports text, images, and videos\n\n### Setup Instructions:\n1. **Configure channels** in the 'Set Source Channel' node\n2. **Set up Telegram Bot** credentials\n3. **Add OpenAI API** credentials\n4. **Adjust schedule** as needed\n\n### Required Credentials:\n- Telegram Bot API\n- OpenAI API\n\n[Documentation & Support](https://n8n.io/integrations/telegram/)"
      },
      "typeVersion": 1
    },
    {
      "id": "5900c319-3a6f-4057-ab5c-209ceb0d9d61",
      "name": "Set Telegram Channels",
      "type": "n8n-nodes-base.set",
      "notes": "Configure your source channel, target channel, and other settings here",
      "position": [
        -1000,
        -60
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "source_channel",
              "name": "sourceChannel",
              "type": "string",
              "value": "channel_username"
            },
            {
              "id": "target_channel",
              "name": "targetChannel",
              "type": "string",
              "value": "@channel_username"
            },
            {
              "id": "target_language",
              "name": "targetLanguage",
              "type": "string",
              "value": "Persian"
            },
            {
              "id": "channel_signature",
              "name": "channelSignature",
              "type": "string",
              "value": "@channel_username"
            }
          ]
        }
      },
      "notesInFlow": true,
      "typeVersion": 3.4
    },
    {
      "id": "7902b67b-24f5-4d7b-b54e-85b22e97c073",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1020,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 800,
        "height": 220,
        "content": "## 1. Get the html content of the source Telegram channel\nIn this part of the workflow we fetch the html content of source channel and then extract text, images, and videos from the latest Telegram post.\nRemoves original channel signatures and cleans up the text"
      },
      "typeVersion": 1
    },
    {
      "id": "5c68259a-a22d-4c35-8bbb-7853233e3cb8",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        -180
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "content": "## 2. Use AI for translation\n\nUses OpenAI to translate the post content to the target language"
      },
      "typeVersion": 1
    },
    {
      "id": "56690988-fe75-43b6-9919-2e7d52a66bc6",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        226,
        -420
      ],
      "parameters": {
        "color": 7,
        "width": 600,
        "height": 720,
        "content": "## 3. Extract Media URLs and Identify Media Type\nTelegram uses different methods for handling image and video content. In this step, we identify the type of media included in the post.\n\nIf the post contains an image or video, the corresponding media file is downloaded."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6d57d267-bda4-417a-b3f5-3be54a01e92d",
  "connections": {
    "Has Photo?": {
      "main": [
        [
          {
            "node": "Download Photo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Video?": {
      "main": [
        [
          {
            "node": "Download Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text Only?": {
      "main": [
        [
          {
            "node": "Send Text to Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Schedule": {
      "main": [
        [
          {
            "node": "Set Telegram Channels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Photo": {
      "main": [
        [
          {
            "node": "Send Photo to Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Video": {
      "main": [
        [
          {
            "node": "Send Video to Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Translate Text": {
      "main": [
        [
          {
            "node": "Process Media URLs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean Post Content": {
      "main": [
        [
          {
            "node": "Translate Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Channel Page": {
      "main": [
        [
          {
            "node": "Extract Post Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Media URLs": {
      "main": [
        [
          {
            "node": "Has Photo?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Has Video?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Text Only?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Post Content": {
      "main": [
        [
          {
            "node": "Clean Post Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Telegram Channels": {
      "main": [
        [
          {
            "node": "Fetch Channel Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}