iT邦幫忙

2025 iThome 鐵人賽

DAY 3
1

你是不是也常常一忙就忘記喝水呢?今天,我們來動手做一個簡單又實用的 Discord 通知器,它會在指定的時間自動發送可愛的提醒訊息,關心你和頻道裡的每一個人

核心邏輯

  • 在固定的時間與間隔內,自動傳送提醒訊息

    • 設定排程時間:決定什麼時候要發送提醒

    • 建立內容的資料:寫下幾句溫馨的提醒語錄

    • 傳送提醒訊息:將訊息自動傳送到你的 Discord 頻道

workflow

步驟一:建立新的自動化流程

  • 點選右上角的「Create Workflow」來建立新的流程
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375NIusedCln9.png

  • 點下畫布正中間的「Add first step」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375C3slZzhTPg.png

步驟二:設定排程觸發器

  • 選擇排程「On a schedule」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375pwDVsNdBe4.png

  • 選擇「Custom Cron」來更彈性地設定時間
    https://ithelp.ithome.com.tw/upload/images/20250904/201693752oMuwaysy8.png

  • 指令內容填入以下內容,代表平日的 09:00~17:00 每個小時會觸發

    0 9-17 * * 1-5
    

https://ithelp.ithome.com.tw/upload/images/20250904/20169375T2uqb4X10b.png

  • 接著到這個流程的「Settings」裡面
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375HNqI2gwqQZ.png

  • 將時區「Timezone」設定為台灣時間,這樣才會在正確的時間下去跑
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375r8f5WcaeHX.png

步驟三:用程式碼準備隨機訊息

  • 下個節點選擇「Code」,來寫簡單的程式碼
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375LUCtaetEiO.png

  • 貼上底下的程式碼,意思是會去隨機從變數「messages」裡面取一條出來當訊息

    const messages = [
      "🔔 喝水時間到囉!快去補充水分!",
      "💻 電腦看太久了,起來伸個懶腰,順便喝杯水吧!",
      "💧 身體的 70% 是水,你今天補充了嗎?",
      "嘿!你最好的朋友「水」在呼喚你!",
      "休息是為了走更長遠的路,喝水也是!"
    ];
    
    // 隨機選出一句話
    const selectedMessage = messages[Math.floor(Math.random() * messages.length)];
    
    // 將選出的話放進物件,並用陣列包起來回傳
    return [{
      json: {
        randomMessage: selectedMessage
      }
    }];
    

https://ithelp.ithome.com.tw/upload/images/20250904/20169375zU8EcdME5V.png

步驟四:串接 Discord 並發送訊息

  • 下個節點選擇「Discord」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375wwaRixq00W.png

  • 選擇「Send a message」
    https://ithelp.ithome.com.tw/upload/images/20250904/201693759bEA65Eook.png

  • 類型選擇「Webhook」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375IQ7NNrd9wg.png

  • 建立 Discord 憑證
    https://ithelp.ithome.com.tw/upload/images/20250904/201693758GRxUj7gyx.png

  • 打開 Discord 頻道的設定,選擇「整合」→「建立 Webhook」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375tGfmea2Dey.png

  • 點選「複製網址」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375sts1HOFMi6.png

  • 把我們左邊上個節點的變數,拖拉到「Message」的欄位裡面
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375AfEHuMAG56.pnghttps://ithelp.ithome.com.tw/upload/images/20250904/20169375uu3M2MZdju.png

步驟五:測試與啟動

  • 點選上方的「Execute step」測試看看,輸出「true」代表成功惹
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375i6VRwU2jIj.png

  • Discord 也有出現訊息
    https://ithelp.ithome.com.tw/upload/images/20250904/201693753wG9iFQCf8.png

  • 畫布上面完整的流程會長這樣,記得上方存檔以及要切換為「Active」才會開始運作哦

https://ithelp.ithome.com.tw/upload/images/20250904/20169375WYOb4kRCX7.png

  • 最後附上流程的 JSON 檔案,如果懶得製作也可以直接匯入使用

    {
      "name": "飲水提醒",
      "nodes": [
        {
          "parameters": {
            "rule": {
              "interval": [
                {
                  "field": "cronExpression",
                  "expression": "0 9-17 * * 1-5"
                }
              ]
            }
          },
          "type": "n8n-nodes-base.scheduleTrigger",
          "typeVersion": 1.2,
          "position": [
            0,
            0
          ],
          "id": "da9403af-f3f5-4783-9613-69f164b60d47",
          "name": "Schedule Trigger"
        },
        {
          "parameters": {
            "jsCode": "const messages = [\n  \"🔔 喝水時間到囉!快去補充水分!\",\n  \"💻 電腦看太久了,起來伸個懶腰,順便喝杯水吧!\",\n  \"💧 身體的 70% 是水,你今天補充了嗎?\",\n  \"嘿!你最好的朋友「水」在呼喚你!\",\n  \"休息是為了走更長遠的路,喝水也是!\"\n];\n\n// 隨機選出一句話\nconst selectedMessage = messages[Math.floor(Math.random() * messages.length)];\n\n// 將選出的話放進物件,並用陣列包起來回傳\nreturn [{\n  json: {\n    randomMessage: selectedMessage\n  }\n}];"
          },
          "type": "n8n-nodes-base.code",
          "typeVersion": 2,
          "position": [
            220,
            0
          ],
          "id": "a4aeb86e-37a1-4f06-9265-99dac20e4cae",
          "name": "Code"
        },
        {
          "parameters": {
            "authentication": "webhook",
            "content": "={{ $json.randomMessage }}",
            "options": {}
          },
          "type": "n8n-nodes-base.discord",
          "typeVersion": 2,
          "position": [
            440,
            0
          ],
          "id": "26b38611-8da9-46e9-afa5-76aee7815ae0",
          "name": "Discord"
        }
      ],
      "pinData": {},
      "connections": {
        "Schedule Trigger": {
          "main": [
            [
              {
                "node": "Code",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Code": {
          "main": [
            [
              {
                "node": "Discord",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      },
      "active": true,
      "settings": {
        "executionOrder": "v1",
        "timezone": "Asia/Taipei",
        "callerPolicy": "workflowsFromSameOwner",
        "executionTimeout": -1
      },
      "versionId": "d403fc95-2131-4ef0-acde-43525b233301",
      "meta": {
        "templateCredsSetupCompleted": true
      },
      "id": "DN78At8wNA7KDqTQ",
      "tags": []
    }
    
  • 匯入的方式:到上方點選「Import from File…」
    https://ithelp.ithome.com.tw/upload/images/20250904/20169375SpdJk6MiB7.png


上一篇
[Day02]_n8n
下一篇
[Day04]_自動記錄打卡時間-#1:Google 服務憑證設定教學
系列文
告別重複瑣事: n8n workflow 自動化工作實踐5
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言