你是否也常常為了「午餐吃什麼?」這個千古難題而煩惱?每天中午的靈魂拷問,不僅消耗心力,有時還會耽誤寶貴的休息時間(?
利用「Google 試算表」來儲存店家清單
n8n 排程在平日的中午 12:30 去撈這些資料,隨機取出一家
把結果傳給 Discord
在開始之前,你只需要準備一個簡單的 Google 試算表,在裡面列出你喜歡的餐廳或店家清單即可
點選右上角的「Create Workflow」來建立流程

選擇排程「On a schedule」

設定「Cron」指令:平日中午 12:30
0 30 12 * * 1-5

選擇「Google Sheet」

選擇「Get Row(s) in sheet」

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

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

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


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

來撰寫跑隨機的「JavaScript」程式碼
雖然這個隨機不是很真的隨機,但我們不是在做抽獎,用這個就可以ㄌ
想知道更隨機的程式碼可以搜尋「洗牌演算法」
// 取得前面節點送過來的所有項目 (店家列表)
const items = $input.all();
// 產生一個 0 到列表長度之間的隨機數字
const randomIndex = Math.floor(Math.random() * items.length);
// 回傳位於該隨機索引的該項目
return [items[randomIndex]];

選出了餐廳,我們需要將結果包裝成一則清楚易讀的訊息
接下來選擇「Edit Fields」

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

訊息的內容來做點客製化,可以把左邊的欄位拖過來
鏘鏘!今天的午餐命運決定是...
🍽️ 店家名稱:{{ $json.Title }}

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

選擇「Send a message」

用「Webhook」來傳送

建立 Discord 憑證

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

點選「複製網址」

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

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

Discord 也有顯示訊息

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

接著到「Settings」裡面

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

恭喜!現在你已經擁有一個專屬的午餐小幫手惹
從此,讓每天的午餐都充滿一點隨機的驚喜吧!
這邊也提供整個 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": []
}