iT邦幫忙

2025 iThome 鐵人賽

DAY 8
0

你是否也常常為了「午餐吃什麼?」這個千古難題而煩惱?每天中午的靈魂拷問,不僅消耗心力,有時還會耽誤寶貴的休息時間(?

主要架構

  • 利用「Google 試算表」來儲存店家清單

  • n8n 排程在平日的中午 12:30 去撈這些資料,隨機取出一家

  • 把結果傳給 Discord

前置作業

在開始之前,你只需要準備一個簡單的 Google 試算表,在裡面列出你喜歡的餐廳或店家清單即可

workflow

步驟一:建立工作流並設定排程

  • 點選右上角的「Create Workflow」來建立流程

    image 0.png

  • 選擇排程「On a schedule」

    image 1.png

  • 設定「Cron」指令:平日中午 12:30

    0 30 12 * * 1-5
    

    image 2.png

步驟二:從 Google Sheets 撈取店家清單

  • 選擇「Google Sheet」

    image 3.png

  • 選擇「Get Row(s) in sheet」

    image 4.png

  • 選擇剛才建好的「Google Sheets account」

    image 5.png

  • 下方點選我們的文件名稱與試算表名稱

    image 6.png

  • 接下來可以點點看「Execute workflow」來測試執行,可以看到輸出資料,代表有成功惹

    image 7.png

    image 8.png

步驟三:用程式碼實作隨機挑選

現在我們手上有了完整的店家清單,接下來就是要隨機選出其中一家

  • 下個節點選擇「Code」

    image 9.png

  • 來撰寫跑隨機的「JavaScript」程式碼

    • 雖然這個隨機不是很真的隨機,但我們不是在做抽獎,用這個就可以ㄌ

    • 想知道更隨機的程式碼可以搜尋「洗牌演算法」

    // 取得前面節點送過來的所有項目 (店家列表)
    const items = $input.all();
    
    // 產生一個 0 到列表長度之間的隨機數字
    const randomIndex = Math.floor(Math.random() * items.length);
    
    // 回傳位於該隨機索引的該項目
    return [items[randomIndex]];
    

    image 10.png

步驟四:客製化你的通知訊息

選出了餐廳,我們需要將結果包裝成一則清楚易讀的訊息

  • 接下來選擇「Edit Fields」

    image 11.png

  • 把產出的結果設定到變數欄位「message」上面

    image 12.png

  • 訊息的內容來做點客製化,可以把左邊的欄位拖過來

    鏘鏘!今天的午餐命運決定是...
    🍽️ 店家名稱:{{ $json.Title }}
    

    image 13.png

步驟五:發送結果到 Discord

  • 接著把訊息傳送給 Discord,下個節點選擇「Discord」

    image 14.png

  • 選擇「Send a message」

    image 15.png

  • 用「Webhook」來傳送

    image 16.png

  • 建立 Discord 憑證

    image 17.png

  • 打開 Discord 頻道的設定,選擇「整合」→「建立 Webhook」

    image 18.png

  • 點選「複製網址」

    image 19.png

  • 把上個節點的「message」拖進欄位裡面

    image 20.png

  • 點選「Execute step」來測試該節點能否運作,輸出「true」代表成功啦

    image 21.png

  • Discord 也有顯示訊息

    image 22.png

步驟六:啟用並完成設定

  • 完整 workflow 的畫布節點看起來會像這樣,記得要把「Active」打開

    image 23.png

  • 接著到「Settings」裡面

    image 24.png

  • 把時區「Timezone」改為台灣時區,這樣排程才會在台灣的時間正確傳送訊息哦

    image 25.png

恭喜!現在你已經擁有一個專屬的午餐小幫手惹

從此,讓每天的午餐都充滿一點隨機的驚喜吧!

  • 這邊也提供整個 workflow 的 JSON 內容,可以直接匯入使用

    {
      "name": "中晝食啥物",
      "nodes": [
        {
          "parameters": {
            "rule": {
              "interval": [
                {
                  "field": "cronExpression",
                  "expression": "0 30 12 * * 1-5"
                }
              ]
            }
          },
          "type": "n8n-nodes-base.scheduleTrigger",
          "typeVersion": 1.2,
          "position": [0, 0],
          "id": "928145d7-437e-457d-b1dc-095fd5cbfc5c",
          "name": "Schedule Trigger"
        },
        {
          "parameters": {
            "documentId": "",
            "sheetName": "",
            "options": {}
          },
          "type": "n8n-nodes-base.googleSheets",
          "typeVersion": 4.6,
          "position": [220, 0],
          "id": "2a8a574a-dc07-4846-94bd-74af9838b903",
          "name": "Google Sheets"
        },
        {
          "parameters": {
            "jsCode": "// 取得前面節點送過來的所有項目 (店家列表)\nconst items = $input.all();\n\n// 產生一個 0 到列表長度之間的隨機數字\nconst randomIndex = Math.floor(Math.random() * items.length);\n\n// 回傳位於該隨機索引的該項目\nreturn [items[randomIndex]];"
          },
          "type": "n8n-nodes-base.code",
          "typeVersion": 2,
          "position": [440, 0],
          "id": "ed7738cf-0b35-4d88-a164-c9cc68b6390d",
          "name": "Code"
        },
        {
          "parameters": {
            "assignments": {
              "assignments": [
                {
                  "id": "40520982-9c94-42c5-a120-6275d4d54e9b",
                  "name": "message",
                  "value": "=鏘鏘!今天的午餐命運決定是...\n🍽️ 店家名稱:{{ $json.Title }}",
                  "type": "string"
                }
              ]
            },
            "options": {}
          },
          "type": "n8n-nodes-base.set",
          "typeVersion": 3.4,
          "position": [660, 0],
          "id": "6f2ab6f6-9776-46a9-9651-0d0778072168",
          "name": "Edit Fields"
        },
        {
          "parameters": {
            "authentication": "webhook",
            "content": "={{ $json.message }}",
            "options": {}
          },
          "type": "n8n-nodes-base.discord",
          "typeVersion": 2,
          "position": [880, 0],
          "id": "eaa659d4-8603-4c71-ba37-32f8ade6916f",
          "name": "Discord"
        }
      ],
      "pinData": {},
      "connections": {
        "Schedule Trigger": {
          "main": [
            [
              {
                "node": "Google Sheets",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Google Sheets": {
          "main": [
            [
              {
                "node": "Code",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Code": {
          "main": [
            [
              {
                "node": "Edit Fields",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Edit Fields": {
          "main": [
            [
              {
                "node": "Discord",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      },
      "active": true,
      "settings": {
        "executionOrder": "v1",
        "timezone": "Asia/Taipei",
        "callerPolicy": "workflowsFromSameOwner"
      },
      "versionId": "ba7c6e4a-36a7-4f1a-b2b5-6f11c972e41c",
      "meta": null,
      "id": "rQMX0KCpzty9ujEd",
      "tags": []
    }
    

上一篇
[Day07]_自動記錄打卡時間-#4.END
系列文
告別重複瑣事: n8n workflow 自動化工作實踐8
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言