n8n Thai
by n8n Thai

แจ้งเตือนหลายช่องทางพร้อมกันด้วย n8n: LINE + Email + Slack

สร้าง workflow n8n ส่ง notification ไปพร้อมกันทั้ง LINE Email และ Slack จาก trigger เดียว ตั้งค่า routing ตามความสำคัญ และสร้าง escalation chain อัตโนมัติ

แจ้งเตือนหลายช่องทางพร้อมกันด้วย n8n: LINE + Email + Slack

การแจ้งเตือนที่ดีต้องไปถึงคนที่ใช่ ผ่านช่องทางที่ใช่ ในเวลาที่ใช่ ถ้าส่งแจ้งเตือนทุกอย่างผ่านช่องทางเดิม ไม่ว่าจะสำคัญแค่ไหน คนรับจะเริ่มมองข้ามทั้งหมด n8n ช่วยจัดการ routing ของ notification ตามระดับความสำคัญและประเภทได้อย่างละเอียด

ตัวอย่าง notification routing ที่ใช้งานได้จริงในหลายธุรกิจ คำสั่งซื้อใหม่ส่งไป LINE OA ทันที สรุปยอดรายวันส่งมาทาง Email ทุกเช้า alert ระบบล่มไป Slack ทีม DevOps และโทรหาผู้จัดการถ้าไม่มีการตอบสนองใน 15 นาที ทั้งหมดนี้เป็น workflow เดียวที่จัดการจาก trigger อันเดียว

โครงสร้าง Notification Hub

แนวคิดคือสร้าง “Notification Hub” workflow ที่รับ notification จากทุก system แล้ว route ไปยังช่องทางที่ถูกต้อง

Webhook (รับ notification จากทุก system)
  → Code: Parse และ enrich notification data
  → Switch: Route ตาม notification type
    ├── order_new → LINE OA + Slack #sales
    ├── order_error → LINE + Email + Slack #ops (urgent)
    ├── payment_received → Email receipt + Slack #finance
    ├── system_alert → Slack #alerts + PagerDuty
    ├── daily_report → Email only
    └── low_stock → LINE + Email purchasing

ขั้นตอนที่ 1: รับ Notification ผ่าน Webhook

สร้าง Webhook endpoint ใน n8n สำหรับรับ notification จากทุก system

ตัวอย่าง payload มาตรฐาน

{
  "type": "order_new",
  "priority": "high",
  "title": "คำสั่งซื้อใหม่ #ORD-2456",
  "message": "สมชาย ใจดี สั่ง 3 รายการ ยอด ฿2,850",
  "data": {
    "order_id": "ORD-2456",
    "customer_name": "สมชาย ใจดี",
    "amount": 2850,
    "items": [...]
  },
  "channels": ["line", "slack"],
  "timestamp": "2026-04-15T14:32:00+07:00"
}

กำหนด payload format นี้ไว้ให้ทุก system ที่จะส่งมาใช้รูปแบบเดียวกัน ทำให้ workflow ด้านในง่ายขึ้นมาก

ขั้นตอนที่ 2: ตั้งค่า LINE Notification

LINE มีสองวิธีหลัก LINE Notify ซึ่งง่ายกว่าแต่ปิดบริการไปแล้วในปี 2025 ดังนั้นต้องใช้ LINE Messaging API แทน

ส่งข้อความไปยัง LINE OA (Broadcast):

HTTP Request node
Method: POST
URL: https://api.line.me/v2/bot/message/broadcast
Headers:
  Authorization: Bearer {CHANNEL_ACCESS_TOKEN}
  Content-Type: application/json
Body:
{
  "messages": [
    {
      "type": "flex",
      "altText": "{{ $json.title }}",
      "contents": {
        "type": "bubble",
        "header": {
          "type": "box",
          "layout": "vertical",
          "contents": [{
            "type": "text",
            "text": "{{ $json.title }}",
            "weight": "bold",
            "size": "md"
          }]
        },
        "body": {
          "type": "box",
          "layout": "vertical",
          "contents": [{
            "type": "text",
            "text": "{{ $json.message }}",
            "wrap": true
          }]
        }
      }
    }
  ]
}

ส่งไปยังบุคคลเฉพาะ (Push Message):

{
  "to": "{user_id}",
  "messages": [{"type": "text", "text": "{{ $json.message }}"}]
}

ขั้นตอนที่ 3: ตั้งค่า Slack Notification

Slack node ใน n8n ตั้งค่าง่าย แต่ถ้าต้องการส่งไปหลาย channel ตามประเภท alert ใช้ Incoming Webhook แยกสำหรับแต่ละ channel

Slack Block Kit สำหรับ alert สวยงาม:

{
  "channel": "#alerts",
  "blocks": [
    {
      "type": "header",
      "text": {
        "type": "plain_text",
        "text": "{{ $json.title }}"
      }
    },
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "{{ $json.message }}"
      }
    },
    {
      "type": "context",
      "elements": [{
        "type": "mrkdwn",
        "text": "เวลา: {{ $json.timestamp }} | Priority: *{{ $json.priority }}*"
      }]
    }
  ]
}

ขั้นตอนที่ 4: ตั้งค่า Email Notification

ใช้ SendGrid หรือ Gmail node ส่ง HTML email

// Code node: สร้าง HTML email body
const notification = $input.first().json;
const html = `
<div style="font-family: sans-serif; max-width: 600px; margin: 0 auto;">
  <h2 style="color: #333;">${notification.title}</h2>
  <p style="color: #666; font-size: 16px;">${notification.message}</p>
  <hr style="border: 1px solid #eee;">
  <p style="color: #999; font-size: 12px;">
    ส่งโดยระบบ Notification อัตโนมัติ | ${notification.timestamp}
  </p>
</div>
`;
return [{ json: { ...notification, html_body: html } }];

Priority Routing Logic

ใช้ Code node กำหนด routing rules แบบละเอียด

const notification = $input.first().json;
const channels = [];

// Rules ตาม priority
if (notification.priority === 'critical') {
  channels.push('line', 'slack', 'email', 'sms');
} else if (notification.priority === 'high') {
  channels.push('line', 'slack');
} else if (notification.priority === 'medium') {
  channels.push('slack', 'email');
} else {
  channels.push('email');
}

// Override rules ตาม type
if (notification.type === 'daily_report') {
  return [{ json: { ...notification, channels: ['email'] } }];
}

// ช่วงเวลา - ไม่ส่ง LINE หลังเที่ยงคืน
const hour = new Date().getHours();
if (hour >= 23 || hour < 7) {
  const filtered = channels.filter(c => c !== 'line');
  return [{ json: { ...notification, channels: filtered } }];
}

return [{ json: { ...notification, channels } }];

Escalation Chain

ถ้า notification ระดับ critical ไม่ได้รับการตอบสนองภายในเวลาที่กำหนด ให้ escalate ไปยังคนถัดไป

HTTP Request: ส่ง notification ครั้งแรก
  → Google Sheets: บันทึก notification_id + status = "sent"
  → Wait: 15 นาที
  → HTTP Request: ตรวจสอบ acknowledgment status
  → IF: ยังไม่ได้รับการ ack?
    → HTTP Request: ส่ง LINE ไปหา manager
    → Wait: 15 นาที
    → IF: ยังไม่ ack?
      → HTTP Request: โทรออกอัตโนมัติผ่าน Twilio

Notification Template Manager

เก็บ template ใน Google Sheets เพื่อให้ทีม non-technical แก้ไขข้อความได้โดยไม่ต้องแก้ workflow

Sheet: notification_templates

template_idtypechannelsubjectbody_template
order_new_lineorder_newline-มีคำสั่งซื้อใหม่ {order_id} จาก {customer_name} ยอด ฿{amount}
order_new_emailorder_newemailคำสั่งซื้อใหม่ #{order_id}HTML template เต็ม

workflow ดึง template ตาม type และ channel แล้วแทนที่ placeholder ด้วยข้อมูลจริงก่อนส่ง

ทดสอบ Notification System

ก่อน go live ควรทดสอบทุก channel และทุก priority level สร้าง workflow แยกสำหรับ testing ที่ส่ง notification ตัวอย่างไปทุก channel แล้วตรวจว่าได้รับครบและแสดงผลถูกต้อง โดยเฉพาะ Thai characters ใน LINE และ Email

อยากเรียน n8n แบบเป็นระบบ ตั้งแต่เริ่มต้นจนสร้าง Workflow ใช้งานจริงได้ ลองดู คอร์สสอน n8n ที่ aiunlock.co

Related posts