{
  "name": "انتقال پیام از تلگرم به بله",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "channel_post"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -112,
        720
      ],
      "id": "7df562a7-0c9b-4f8f-98ea-ffa9c4c5de51",
      "name": "Telegram Trigger3",
      "webhookId": "28805009-cc0d-4bac-96d1-c3acd08543ae",
      "credentials": {
        "telegramApi": {
          "id": "HuPKVinbubq1GBHm",
          "name": "نوبت دهی"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://tapi.bale.ai/botYOUR_BALE_TOKEN/sendMessage",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "chat_id",
              "value": "YOUR_BALE_CHAT_ID"
            },
            {
              "name": "parse_mode",
              "value": "Markdown"
            },
            {
              "name": "=text",
              "value": "={{ $json.final_text }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        880,
        912
      ],
      "id": "b186030f-f5a0-4461-a6b4-e61109ab3d2f",
      "name": "HTTP Request6"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "71bca6e2-36fb-4a66-b3cb-19ff7320e838",
              "leftValue": "={{ $json.channel_post?.sender_chat?.id }}",
              "rightValue": null,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        112,
        720
      ],
      "id": "6a7d2cf0-7989-449e-a975-b8db1ae87158",
      "name": "If5"
    },
    {
      "parameters": {
        "jsCode": "for (const item of $input.all()) {\n  const post = item.json.channel_post || {};\n  \n  // تعریف امضا و لینک جدید کانال بله\n  const signature = \"\\n\\n[YOUR_NAME | YOUR_CHANNEL_TITLE](YOUR_CHANNEL_LINK)\";\n  \n  // فرمول حذف لینک‌های تلگرام و همچنین حذف متن امضای تلگرامی شما\n  const tgLinkRegex = /https:\\/\\/t\\.me\\/\\S+/g;\n  const oldSignatureRegex = /YOUR_OLD_SIGNATURE_REGEX/gi;\n\n  // حالت اول: اگر پیام فقط متن باشد\n  if (post.text) {\n    // اول متن امضای قدیمی رو حذف میکنه، بعد لینک تلگرام رو، و در آخر فضاهای خالی اضافه رو میگیره\n    let cleanedText = post.text.replace(oldSignatureRegex, \"\").replace(tgLinkRegex, \"\").trim();\n    item.json.final_text = cleanedText + signature;\n    item.json.msg_type = \"text\"; \n  } \n  \n  // حالت دوم: اگر پیام حاوی عکس باشد\n  else if (post.photo) {\n    let originalCaption = post.caption || \"\";\n    let cleanedCaption = originalCaption.replace(oldSignatureRegex, \"\").replace(tgLinkRegex, \"\").trim();\n    item.json.final_caption = cleanedCaption + signature;\n    \n    // برداشتن باکیفیت‌ترین سایز عکس\n    item.json.bale_file_id = post.photo[post.photo.length - 1].file_id;\n    item.json.msg_type = \"photo\"; \n  }\n}\n\nreturn $input.all();"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        336,
        720
      ],
      "id": "74976c00-3635-4782-9339-b076910d7f9c",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "url": "https://tapi.bale.ai/botYOUR_BALE_TOKEN/sendPhoto",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "chat_id",
              "value": "YOUR_BALE_CHAT_ID"
            },
            {
              "parameterType": "formBinaryData",
              "name": "photo",
              "inputDataFieldName": "data"
            },
            {
              "name": "caption",
              "value": "={{ $('Code in JavaScript').item.json.final_caption }}"
            },
            {
              "name": "parse_mode",
              "value": "Markdown"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1008,
        624
      ],
      "id": "6a77f108-a9a4-434f-9faa-a4886e9b28af",
      "name": "HTTP Request8"
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.channel_post.photo.slice(-1)[0].file_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        784,
        624
      ],
      "id": "c00946ff-5cd5-4cf1-806b-73ec34565254",
      "name": "Get a file4",
      "webhookId": "1e030726-513a-4dac-a566-479372354869",
      "credentials": {
        "telegramApi": {
          "id": "HuPKVinbubq1GBHm",
          "name": "نوبت دهی"
        }
      }
    },
    {
      "parameters": {
        "mode": "expression",
        "numberOutputs": 3,
        "output": "={{ $json.channel_post.photo ? 0 : ($json.channel_post.video || $json.channel_post.document ? 2 : 1) }}"
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        560,
        704
      ],
      "id": "9d62c5aa-2e55-4197-aea1-e6202a41c048",
      "name": "Switch"
    }
  ],
  "pinData": {},
  "connections": {
    "Telegram Trigger3": {
      "main": [
        [
          {
            "node": "If5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If5": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a file4": {
      "main": [
        [
          {
            "node": "HTTP Request8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Get a file4",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "81d2b834-c816-41fb-9d07-08ff7f90cc2b",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "dcdb8a353d3e869ec414e4383ac0cf1bf845f27d7f1857a2bcdbafe161c19c38"
  },
  "id": "xIA6ChjM87J4JtVo",
  "tags": []
}