iT邦幫忙

2025 iThome 鐵人賽

DAY 28
0

在先前的文章裡,我們已經建立了備份單篇 Threads 貼文的基礎流程。現在,我們要進一步完善它,讓主流程可以自動呼叫這個備份流程,並在完成後自動刪除 Discord 中的訊息,實作更完整的自動化

前置作業

首先,我們需要回到先前建立的「備份流程」,為它新增一個觸發點,讓其他 workflow 可以呼叫它並傳送資料

  • 前往備份的流程,新增一個起始節點「When Executed by Another Workflow」

    • 設定來接收參數 url

      image 0.png

  • 下個節點同樣選擇「Edit Fields」,然後把該參數指派到新的變數 url 身上

    {
      {
        $json.url;
      }
    }
    

    image 1.png

  • 而現在備份流程的畫布看起來會長得像這樣,有兩個進入點

    image 2.png

workflow

串接與執行迴圈

  • 延續上一篇,從 Loop 的路徑拉出下一個節點「Execute Workflow」,選擇之前做好的備份流程

    • 設定傳入變數 url

      {
        {
          $json.content.split("?")[0];
        }
      }
      

      image 3.png

  • 下個節點選擇「If」來判斷假如沒有錯誤,就可以繼續進行

    {
      {
        !$json.error;
      }
    }
    

    image 4.png

處理回傳結果與合併資料

  • 假如沒有錯誤,備份流程會回傳訊息,但我們後續也需要 Discord 訊息的參數,所以接下來在「true」的路徑使用「Merge」來合併這些資料,把「Loop」節點連到「Merge」的「input1」,剛才「true」的路徑連到「Merge」的「input2」

    image 5.png

  • 接著在「Merge」後選擇「If」來判斷是檔案都上傳到雲端硬碟了

    {
      {
        $input.all()[1].json.is_downloadable;
      }
    }
    

    image 6.png

  • 接著在「true」路徑,設定下個節點「Limit」

    image 7.png

  • 接著選擇「Discord」的「Delete a message」來刪除該訊息

    {
      {
        $json.id;
      }
    }
    

    image 8.png

清理與完成迴圈

  • 而一筆資料跑完就要進行下一筆,所以把 Discord 的節點再連回去「Loop」

    • 先前的判斷假如失敗,也需要進行下一筆,而不是就此停止,所以把所有的「false」路徑也連回去「Loop」

    image 9.png

  • 底下也附上本次的 JSON

    {
      "nodes": [
        {
          "parameters": {},
          "type": "n8n-nodes-base.manualTrigger",
          "typeVersion": 1,
          "position": [80, -200],
          "id": "fe19ea5e-606a-4a3f-8350-d430586d4375",
          "name": "When clicking ‘Execute workflow’"
        },
        {
          "parameters": {
            "workflowId": {
              "__rl": true,
              "value": "YOUR_WORKFLOW_ID",
              "mode": "list",
              "cachedResultName": "Sub-Workflow Name"
            },
            "workflowInputs": {
              "mappingMode": "defineBelow",
              "value": {
                "url": "={{ $json.content.split('?')[0] }}"
              },
              "matchingColumns": ["content"],
              "schema": [
                {
                  "id": "url",
                  "displayName": "url",
                  "required": false,
                  "defaultMatch": false,
                  "display": true,
                  "canBeUsedToMatch": true,
                  "type": "string",
                  "removed": false
                }
              ],
              "attemptToConvertTypes": false,
              "convertFieldsToString": true
            },
            "options": {
              "waitForSubWorkflow": true
            }
          },
          "type": "n8n-nodes-base.executeWorkflow",
          "typeVersion": 1.2,
          "position": [740, 80],
          "id": "694dd593-07f7-4e84-be8f-54c2467a976b",
          "name": "Execute Workflow",
          "alwaysOutputData": false,
          "onError": "continueRegularOutput"
        },
        {
          "parameters": {},
          "type": "n8n-nodes-base.merge",
          "typeVersion": 3.2,
          "position": [1080, -160],
          "id": "729ab407-c778-4789-a009-0501587cce2d",
          "name": "Merge"
        },
        {
          "parameters": {},
          "type": "n8n-nodes-base.limit",
          "typeVersion": 1,
          "position": [1340, 0],
          "id": "6da8a716-0ecb-4745-9c6f-4e348f6cf9be",
          "name": "Limit"
        },
        {
          "parameters": {
            "resource": "message",
            "operation": "deleteMessage",
            "guildId": {
              "__rl": true,
              "value": "YOUR_DISCORD_GUILD_ID",
              "mode": "list",
              "cachedResultName": "Your Guild Name",
              "cachedResultUrl": "https://discord.com/channels/YOUR_DISCORD_GUILD_ID"
            },
            "channelId": {
              "__rl": true,
              "value": "YOUR_DISCORD_CHANNEL_ID",
              "mode": "list",
              "cachedResultName": "Your Channel Name",
              "cachedResultUrl": "https://discord.com/channels/YOUR_DISCORD_GUILD_ID/YOUR_DISCORD_CHANNEL_ID"
            },
            "messageId": "={{ $json.id }}"
          },
          "type": "n8n-nodes-base.discord",
          "typeVersion": 2,
          "position": [1500, 60],
          "id": "390b42ff-3883-4873-925f-59c3e0ad5705",
          "name": "DELETE",
          "webhookId": "YOUR_WEBHOOK_ID",
          "credentials": {
            "discordBotApi": {
              "id": "YOUR_CREDENTIAL_ID",
              "name": "Discord Bot account"
            }
          }
        },
        {
          "parameters": {
            "resource": "message",
            "operation": "getAll",
            "guildId": {
              "__rl": true,
              "value": "YOUR_DISCORD_GUILD_ID",
              "mode": "list",
              "cachedResultName": "Your Guild Name",
              "cachedResultUrl": "https://discord.com/channels/YOUR_DISCORD_GUILD_ID"
            },
            "channelId": {
              "__rl": true,
              "value": "YOUR_DISCORD_CHANNEL_ID",
              "mode": "list",
              "cachedResultName": "Your Channel Name",
              "cachedResultUrl": "https://discord.com/channels/YOUR_DISCORD_GUILD_ID/YOUR_DISCORD_CHANNEL_ID"
            },
            "limit": 50,
            "options": {}
          },
          "type": "n8n-nodes-base.discord",
          "typeVersion": 2,
          "position": [280, -200],
          "id": "28289e49-fa10-41cd-8e6e-759da069518b",
          "name": "GET messages",
          "webhookId": "YOUR_WEBHOOK_ID",
          "credentials": {
            "discordBotApi": {
              "id": "YOUR_CREDENTIAL_ID",
              "name": "Discord Bot account"
            }
          }
        },
        {
          "parameters": {
            "options": {}
          },
          "type": "n8n-nodes-base.splitInBatches",
          "typeVersion": 3,
          "position": [560, 0],
          "id": "6bacf2a5-8e7c-4ffd-9be2-c248c5e3f5b7",
          "name": "Loop Over Items"
        },
        {
          "parameters": {
            "conditions": {
              "options": {
                "caseSensitive": true,
                "leftValue": "",
                "typeValidation": "strict",
                "version": 2
              },
              "conditions": [
                {
                  "id": "3f4db064-3249-4aae-9e46-54eebe49515e",
                  "leftValue": "={{ !$json.error }}",
                  "rightValue": "",
                  "operator": {
                    "type": "boolean",
                    "operation": "true",
                    "singleValue": true
                  }
                }
              ],
              "combinator": "and"
            },
            "options": {}
          },
          "type": "n8n-nodes-base.if",
          "typeVersion": 2.2,
          "position": [920, 60],
          "id": "44fe353e-e047-45b3-b7d9-bd4f189e7b47",
          "name": "Check"
        },
        {
          "parameters": {
            "conditions": {
              "options": {
                "caseSensitive": true,
                "leftValue": "",
                "typeValidation": "strict",
                "version": 2
              },
              "conditions": [
                {
                  "id": "c0245864-98c9-4845-9682-c3e08e4d6349",
                  "leftValue": "={{ $input.all()[1].json.is_downloadable }}",
                  "rightValue": 2,
                  "operator": {
                    "type": "boolean",
                    "operation": "true",
                    "singleValue": true
                  }
                }
              ],
              "combinator": "and"
            },
            "options": {}
          },
          "type": "n8n-nodes-base.if",
          "typeVersion": 2.2,
          "position": [1140, 80],
          "id": "0b05e42f-f903-45a8-98b4-4e0023dde89c",
          "name": "IF upload"
        },
        {
          "parameters": {
            "conditions": {
              "options": {
                "caseSensitive": true,
                "leftValue": "",
                "typeValidation": "strict",
                "version": 2
              },
              "conditions": [
                {
                  "id": "3fb106b3-25f9-4cf1-a622-d46a65389218",
                  "leftValue": "={{ $json.embeds[0].provider.name }}",
                  "rightValue": "Threads",
                  "operator": {
                    "type": "string",
                    "operation": "equals",
                    "name": "filter.operator.equals"
                  }
                }
              ],
              "combinator": "and"
            },
            "options": {}
          },
          "type": "n8n-nodes-base.if",
          "typeVersion": 2.2,
          "position": [280, 20],
          "id": "8f723f84-401a-4385-94a5-6e188313818f",
          "name": "IF threads"
        }
      ],
      "connections": {},
      "pinData": {},
      "meta": {
        "templateCredsSetupCompleted": true,
        "instanceId": "YOUR_N8N_INSTANCE_ID"
      }
    }
    

上一篇
[Day27]_Threads 貼文備份-#4:結合 Discord
下一篇
[Day29]_自動化鐵人賽發文提醒
系列文
告別重複瑣事: n8n workflow 自動化工作實踐29
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言