n8n
Workflow
Automation
Tutorial
Beginner

สร้าง Workflow แรกใน n8n: คู่มือผู้เริ่มต้น

เรียนรู้การสร้าง Workflow อัตโนมัติใน n8n ตั้งแต่พื้นฐาน พร้อมตัวอย่าง Workflow ที่ใช้งานได้จริงสำหรับผู้เริ่มต้น

AI Unlocked Team
17/01/2568
สร้าง Workflow แรกใน n8n: คู่มือผู้เริ่มต้น

สร้าง Workflow แรกใน n8n: คู่มือผู้เริ่มต้น

หลังจากติดตั้ง n8n เรียบร้อยแล้ว ขั้นตอนต่อไปคือการสร้าง Workflow แรกของคุณ บทความนี้จะพาคุณทำความเข้าใจพื้นฐานของ n8n และสร้าง Workflow จริงที่ใช้งานได้ทันที

ยังไม่ได้ติดตั้ง? อ่านได้ที่: วิธีติดตั้ง n8n

ทำความเข้าใจ Interface ของ n8n

หน้าหลัก (Canvas)

เมื่อเปิด n8n คุณจะเห็นพื้นที่ว่างๆ ที่เรียกว่า Canvas - นี่คือที่ที่คุณจะวาง Nodes และสร้าง Workflow

ส่วนประกอบหลัก

{
  "interface_components": {
    "canvas": "พื้นที่วาง Nodes",
    "node_panel": "เมนูเลือก Nodes (กด + หรือ Tab)",
    "execution_button": "ปุ่ม Execute Workflow",
    "save_button": "ปุ่มบันทึก Workflow",
    "workflow_settings": "ตั้งค่า Workflow"
  }
}

ประเภทของ Nodes

1. Trigger Nodes (เริ่มต้น Workflow)

Nodeการใช้งาน
Manual Triggerเริ่มด้วยมือ (สำหรับทดสอบ)
Schedule Triggerตั้งเวลา (Cron)
Webhookรับข้อมูลจากภายนอก
On App Eventเมื่อเกิด Event ใน App

2. Action Nodes (ทำงาน)

Nodeการใช้งาน
HTTP Requestเรียก API
Send Emailส่งอีเมล
Google Sheetsอ่าน/เขียน Spreadsheet
Slack/Discordส่งข้อความ

3. Logic Nodes (ควบคุม Flow)

Nodeการใช้งาน
IFเงื่อนไข True/False
Switchหลายเงื่อนไข
Mergeรวม Data
Split In Batchesแบ่ง Data

Workflow 1: Hello World

เริ่มจาก Workflow ง่ายที่สุดเพื่อทำความเข้าใจพื้นฐาน

ขั้นตอน

  1. เพิ่ม Manual Trigger

    • คลิก + หรือกด Tab
    • ค้นหา "Manual"
    • เลือก "Manual Trigger"
  2. เพิ่ม Set Node

    • คลิก + ที่ขอบ Node
    • ค้นหา "Set"
    • เพิ่ม Field: message = Hello World!
  3. Execute

    • คลิกปุ่ม "Execute Workflow"
    • ดูผลลัพธ์ที่ Output

JSON Workflow

{
  "name": "Hello World",
  "nodes": [
    {
      "parameters": {},
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "message",
              "value": "Hello World!"
            }
          ]
        }
      },
      "name": "Set Message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [450, 300]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [[{"node": "Set Message", "type": "main", "index": 0}]]
    }
  }
}

Workflow 2: ดึงข้อมูลจาก API

สถานการณ์

ดึงข้อมูล Random Quote จาก API และแสดงผล

ขั้นตอน

  1. เพิ่ม Manual Trigger

  2. เพิ่ม HTTP Request Node

    • Method: GET
    • URL: https://api.quotable.io/random
  3. เพิ่ม Set Node (Optional - จัดรูปแบบข้อมูล)

    • quote = {{ $json.content }}
    • author = {{ $json.author }}

JSON Workflow

{
  "name": "Random Quote",
  "nodes": [
    {
      "parameters": {},
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "url": "https://api.quotable.io/random",
        "options": {}
      },
      "name": "Get Quote",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [450, 300]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "quote",
              "value": "={{ $json.content }}"
            },
            {
              "name": "author",
              "value": "={{ $json.author }}"
            }
          ]
        }
      },
      "name": "Format Output",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [650, 300]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [[{"node": "Get Quote", "type": "main", "index": 0}]]
    },
    "Get Quote": {
      "main": [[{"node": "Format Output", "type": "main", "index": 0}]]
    }
  }
}

Workflow 3: Schedule + Slack Notification

สถานการณ์

ส่ง Daily Quote ไปยัง Slack ทุกเช้า 9:00 น.

ขั้นตอน

  1. เพิ่ม Schedule Trigger

    • Trigger at Hour: 9
    • Timezone: Asia/Bangkok
  2. เพิ่ม HTTP Request Node

    • URL: https://api.quotable.io/random
  3. เพิ่ม Slack Node

    • Credentials: ตั้งค่า Slack OAuth
    • Channel: #general
    • Message: Quote of the Day: "{{ $json.content }}" - {{ $json.author }}

การตั้งค่า Slack Credentials

{
  "slack_setup": {
    "step_1": "ไปที่ api.slack.com/apps",
    "step_2": "สร้าง New App",
    "step_3": "เพิ่ม OAuth Scopes: chat:write, channels:read",
    "step_4": "Install to Workspace",
    "step_5": "คัดลอก Bot User OAuth Token",
    "step_6": "ใส่ใน n8n Credentials"
  }
}

JSON Workflow

{
  "name": "Daily Quote to Slack",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 24,
              "triggerAtHour": 9
            }
          ]
        }
      },
      "name": "Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "url": "https://api.quotable.io/random"
      },
      "name": "Get Quote",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [450, 300]
    },
    {
      "parameters": {
        "channel": "#general",
        "text": "=Quote of the Day:\n\n\"{{ $json.content }}\"\n\n- {{ $json.author }}"
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [650, 300]
    }
  ],
  "connections": {
    "Schedule": {
      "main": [[{"node": "Get Quote", "type": "main", "index": 0}]]
    },
    "Get Quote": {
      "main": [[{"node": "Slack", "type": "main", "index": 0}]]
    }
  }
}

Workflow 4: IF Condition

สถานการณ์

ตรวจสอบราคา Bitcoin และแจ้งเตือนถ้าราคาสูงกว่าที่กำหนด

ขั้นตอน

  1. Schedule Trigger (ทุก 1 ชั่วโมง)

  2. HTTP Request - ดึงราคา BTC

    • URL: https://api.coindesk.com/v1/bpi/currentprice.json
  3. IF Node - ตรวจสอบเงื่อนไข

    • Value 1: {{ $json.bpi.USD.rate_float }}
    • Operation: Larger
    • Value 2: 50000
  4. Slack (True) - ราคาสูง → แจ้งเตือน

  5. NoOp (False) - ราคาต่ำ → ไม่ทำอะไร

JSON Workflow

{
  "name": "Bitcoin Price Alert",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{"field": "hours", "hoursInterval": 1}]
        }
      },
      "name": "Every Hour",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [250, 300]
    },
    {
      "parameters": {
        "url": "https://api.coindesk.com/v1/bpi/currentprice.json"
      },
      "name": "Get BTC Price",
      "type": "n8n-nodes-base.httpRequest",
      "position": [450, 300]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.bpi.USD.rate_float }}",
              "operation": "larger",
              "value2": 50000
            }
          ]
        }
      },
      "name": "Price Check",
      "type": "n8n-nodes-base.if",
      "position": [650, 300]
    },
    {
      "parameters": {
        "channel": "#alerts",
        "text": "=BTC Price Alert! Current: ${{ $json.bpi.USD.rate }}"
      },
      "name": "Alert Slack",
      "type": "n8n-nodes-base.slack",
      "position": [850, 200]
    },
    {
      "parameters": {},
      "name": "Do Nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [850, 400]
    }
  ],
  "connections": {
    "Every Hour": {
      "main": [[{"node": "Get BTC Price"}]]
    },
    "Get BTC Price": {
      "main": [[{"node": "Price Check"}]]
    },
    "Price Check": {
      "main": [
        [{"node": "Alert Slack"}],
        [{"node": "Do Nothing"}]
      ]
    }
  }
}

Workflow 5: Form to Google Sheets

สถานการณ์

รับข้อมูลจาก Webhook และบันทึกลง Google Sheets

ขั้นตอน

  1. Webhook Node

    • HTTP Method: POST
    • Path: /form-submission
  2. Google Sheets Node

    • Operation: Append
    • Sheet: เลือก Sheet ที่ต้องการ
    • Mapping: Map fields จาก Webhook

การตั้งค่า Google Sheets Credentials

{
  "google_sheets_setup": {
    "step_1": "ไปที่ console.cloud.google.com",
    "step_2": "สร้าง Project ใหม่",
    "step_3": "Enable Google Sheets API",
    "step_4": "สร้าง OAuth 2.0 Client ID",
    "step_5": "ดาวน์โหลด JSON credentials",
    "step_6": "ใส่ใน n8n และ Authorize"
  }
}

ทดสอบ Webhook

curl -X POST \
  http://localhost:5678/webhook/form-submission \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "message": "Hello from API!"
  }'

Expression Syntax

n8n ใช้ JavaScript Expressions สำหรับ Dynamic Data

ตัวอย่าง Expressions

// เข้าถึงข้อมูลจาก Node ก่อนหน้า
{{ $json.fieldName }}

// เข้าถึงข้อมูลจาก Node ที่ระบุ
{{ $node["Node Name"].json.fieldName }}

// JavaScript Functions
{{ $json.name.toUpperCase() }}
{{ $json.price * 1.07 }}
{{ new Date().toISOString() }}

// Conditional
{{ $json.status === "active" ? "Yes" : "No" }}

// Array Operations
{{ $json.items.length }}
{{ $json.items.map(i => i.name).join(", ") }}

Built-in Variables

VariableDescription
$jsonข้อมูล JSON จาก Input
$binaryข้อมูล Binary (ไฟล์)
$nodeเข้าถึง Node อื่นๆ
$workflowข้อมูล Workflow
$executionข้อมูล Execution ปัจจุบัน
$envEnvironment Variables

Best Practices สำหรับผู้เริ่มต้น

1. ตั้งชื่อ Node ให้สื่อความหมาย

# ดี
"Fetch Customer Data"
"Send Welcome Email"
"Check Payment Status"

# ไม่ดี
"HTTP Request"
"Email"
"IF"

2. ใช้ Sticky Notes

เพิ่ม Sticky Notes เพื่ออธิบาย Workflow

3. Test ทุก Node

คลิก "Execute Node" เพื่อทดสอบทีละ Node

4. Enable Error Workflow

ตั้งค่า Error Workflow เพื่อรับแจ้งเตือนเมื่อเกิดข้อผิดพลาด

อ่านเพิ่มเติม: จัดการ Error ใน n8n

5. ใช้ Version Control

Export Workflow เป็น JSON และเก็บใน Git

# Export
n8n export:workflow --all --output=./workflows/

# Import
n8n import:workflow --input=./workflows/workflow.json

การ Import Workflow

วิธีที่ 1: Copy JSON

  1. Copy JSON Workflow จากบทความ
  2. ใน n8n ไปที่ Workflow ใหม่
  3. กด Ctrl/Cmd + V

วิธีที่ 2: Import File

  1. บันทึก JSON เป็นไฟล์ .json
  2. ใน n8n คลิก Menu → Import from File

Debugging Tips

ดู Execution Data

  1. เปิด Workflow
  2. คลิกที่ Node ที่ต้องการตรวจสอบ
  3. ดู Input/Output ในแท็บด้านขวา

ใช้ Console Log

// ใน Code Node
console.log($json);
return $json;

Pinning Data

Pin ข้อมูลที่ Node เพื่อใช้ในการทดสอบ

ต่อยอดการเรียนรู้

เมื่อคุณเข้าใจพื้นฐานแล้ว สามารถศึกษาต่อได้ที่:

  1. Webhook: Webhook Triggers ใน n8n
  2. Google Sheets: Automate Google Sheets
  3. AI Integration: เชื่อมต่อ n8n กับ AI
  4. Telegram Bot: สร้าง Telegram Bot

หากต้องการเรียนรู้เรื่อง AI เพิ่มเติม:

สรุป

ในบทความนี้คุณได้เรียนรู้:

  • Interface และส่วนประกอบของ n8n
  • ประเภทของ Nodes (Trigger, Action, Logic)
  • การสร้าง 5 Workflows พื้นฐาน
  • Expression Syntax
  • Best Practices สำหรับผู้เริ่มต้น

เคล็ดลับ: ทดลองสร้าง Workflow ด้วยตัวเอง วิธีที่ดีที่สุดในการเรียนรู้คือการลงมือทำ!


พร้อมสร้าง Workflow ที่ซับซ้อนมากขึ้นแล้วหรือยัง?

อ่านต่อที่ 5 Advanced Workflows ที่จะเปลี่ยนการทำงานของคุณ หรือติดตามบทความใหม่ๆ ที่ AI Unlocked


เขียนโดย

AI Unlocked Team