n8n
Automation
Slack
Bot
Integration

n8n Slack Integration: สร้าง Bot และ Automation กับ Slack

คู่มือเชื่อมต่อ n8n กับ Slack ครบทุกเรื่อง ตั้งแต่ส่งข้อความ สร้าง Bot รับ Commands ไปจนถึง Interactive Messages

AI Unlocked Team
13/01/2568
n8n Slack Integration: สร้าง Bot และ Automation กับ Slack

n8n Slack Integration: สร้าง Bot และ Automation กับ Slack

Slack เป็นหนึ่งใน integration ยอดนิยมของ n8n เรียนรู้วิธีสร้าง Bot ส่งการแจ้งเตือน และ automate งานต่างๆ กับ Slack

Setup Slack App

สร้าง Slack App

1. ไปที่ api.slack.com/apps
2. คลิก "Create New App"
3. เลือก "From scratch"
4. ตั้งชื่อ App และเลือก Workspace
5. คลิก "Create App"

เพิ่ม Bot Permissions

ไปที่ OAuth & Permissions → Bot Token Scopes

Required Scopes:
- chat:write (ส่งข้อความ)
- chat:write.public (ส่งไป public channels)
- channels:read (ดู channel list)
- users:read (ดู user info)

Optional Scopes:
- files:write (upload files)
- reactions:write (add emoji reactions)
- channels:history (อ่านข้อความเก่า)

Install App

1. ไปที่ "Install App" ใน sidebar
2. คลิก "Install to Workspace"
3. Authorize permissions
4. Copy "Bot User OAuth Token" (xoxb-...)

ใส่ Credentials ใน n8n

1. n8n → Credentials → Add Credential
2. เลือก "Slack API"
3. ใส่ Bot Token (xoxb-...)
4. Test และ Save

Basic Operations

ส่งข้อความ

Node: Slack
Operation: Send a Message

Settings:
- Select Channel: #general (หรือ channel ID)
- Text: "Hello from n8n!"

ส่งข้อความด้วย Formatting

{
  "channel": "#alerts",
  "text": "*Alert!* Something happened",
  "blocks": [
    {
      "type": "header",
      "text": {
        "type": "plain_text",
        "text": "🚨 Alert Notification"
      }
    },
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Error:* Database connection failed\n*Time:* {{ $now.toFormat('HH:mm') }}"
      }
    }
  ]
}

ส่ง Direct Message

Node: Slack
Operation: Send a Message

Channel: User ID (U0123456789)
หรือ
Channel: @username

Upload File

Node: Slack
Operation: Upload a File

Settings:
- File: {{ $binary.data }}
- Channels: #reports
- Initial Comment: "Daily report attached"

Message Formatting

Basic Markdown

Bold: *bold text*
Italic: _italic text_
Strike: ~strikethrough~
Code: `inline code`
Code block: ```code block```
Link: <https://example.com|Link Text>
User mention: <@U0123456789>
Channel: <#C0123456789>

Block Kit Messages

{
  "blocks": [
    {
      "type": "header",
      "text": {
        "type": "plain_text",
        "text": "📊 Daily Report"
      }
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Orders:*\n150"
        },
        {
          "type": "mrkdwn",
          "text": "*Revenue:*\n฿45,000"
        }
      ]
    },
    {
      "type": "divider"
    },
    {
      "type": "actions",
      "elements": [
        {
          "type": "button",
          "text": {
            "type": "plain_text",
            "text": "View Details"
          },
          "url": "https://dashboard.example.com"
        }
      ]
    }
  ]
}

Attachments (Legacy)

{
  "attachments": [
    {
      "color": "#36a64f",
      "title": "Order Completed",
      "title_link": "https://orders.example.com/123",
      "fields": [
        {
          "title": "Customer",
          "value": "John Doe",
          "short": true
        },
        {
          "title": "Amount",
          "value": "฿1,500",
          "short": true
        }
      ],
      "footer": "Order System",
      "ts": 1234567890
    }
  ]
}

Triggers

Webhook Trigger (Events)

Setup ใน Slack App:
1. Event Subscriptions → Enable
2. Request URL: n8n webhook URL
3. Subscribe to events:
   - message.channels
   - app_mention
   - reaction_added

n8n Workflow:
1. Webhook Trigger
2. IF - Check event type
3. Process accordingly

Slash Commands

Setup ใน Slack App:
1. Slash Commands → Create New
2. Command: /report
3. Request URL: n8n webhook URL
4. Description: "Generate report"

n8n Workflow:
1. Webhook Trigger
2. Parse command text
3. Generate report
4. Return response to Slack

Interactive Messages

Setup ใน Slack App:
1. Interactivity → Enable
2. Request URL: n8n webhook URL

n8n Workflow:
1. Webhook Trigger
2. Parse interaction payload
3. Update message or take action
4. Return acknowledgment

Practical Examples

Example 1: Error Alert Bot

Workflow: Send Error Alerts to Slack

Trigger: Error Trigger (จาก workflow อื่น)

1. Error Trigger

2. Set Node - Format Message
{
  "blocks": [
    {
      "type": "header",
      "text": {
        "type": "plain_text",
        "text": "🚨 Workflow Error"
      }
    },
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Workflow:* {{ $json.workflow.name }}\n*Node:* {{ $json.error.node }}\n*Message:* {{ $json.error.message }}"
      }
    },
    {
      "type": "context",
      "elements": [
        {
          "type": "mrkdwn",
          "text": "{{ $now.toFormat('dd/MM/yyyy HH:mm') }}"
        }
      ]
    }
  ]
}

3. Slack - Send Message
   Channel: #alerts
   Blocks: {{ $json.blocks }}

Example 2: Daily Standup Reminder

Workflow: Standup Reminder

Trigger: Schedule (0 9 * * 1-5)

1. Schedule Trigger (9:00 จันทร์-ศุกร์)

2. Slack - Send Message
   Channel: #team
   Text: "🌅 Good morning team! Time for standup.\n\nPlease share:\n• What you did yesterday\n• What you'll do today\n• Any blockers?"

3. Slack - Add Reaction
   Message: (from previous)
   Emoji: coffee

Example 3: Slash Command Handler

Workflow: /status Command

1. Webhook Trigger
   Path: /slack/status

2. HTTP Request - Get System Status
   URL: https://api.example.com/status

3. Set Node - Format Response
{
  "response_type": "in_channel",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*System Status*\n• API: {{ $json.api_status }}\n• Database: {{ $json.db_status }}\n• Queue: {{ $json.queue_status }}"
      }
    }
  ]
}

4. Respond to Webhook
   Response: {{ $json }}

Example 4: Approval Workflow

Workflow: Leave Request Approval

1. Webhook Trigger (from HR system)

2. Slack - Send Message with Buttons
   Channel: #approvals
   Blocks: [
     {
       "type": "section",
       "text": {
         "type": "mrkdwn",
         "text": "*Leave Request*\n*Employee:* {{ $json.employee }}\n*Dates:* {{ $json.dates }}\n*Reason:* {{ $json.reason }}"
       }
     },
     {
       "type": "actions",
       "elements": [
         {
           "type": "button",
           "text": { "type": "plain_text", "text": "✅ Approve" },
           "style": "primary",
           "action_id": "approve_{{ $json.request_id }}"
         },
         {
           "type": "button",
           "text": { "type": "plain_text", "text": "❌ Reject" },
           "style": "danger",
           "action_id": "reject_{{ $json.request_id }}"
         }
       ]
     }
   ]

3. (Separate workflow) Handle Button Click
   - Webhook receives interaction
   - Update request status
   - Update Slack message
   - Notify employee

Advanced Features

Thread Replies

Send initial message → Get ts (timestamp)
→ Reply in thread using ts

Node: Slack - Send Message
Channel: #channel
Thread TS: {{ $json.ts }}
Text: "This is a reply"

Update Message

Node: Slack - Update Message
Channel: #channel
Message TS: {{ $json.original_ts }}
Text: "Updated message content"

Schedule Message

Node: Slack - Send Message
Schedule Send Time: {{ $now.plus({hours: 1}).toUnixInteger() }}

Get Channel History

Node: Slack
Operation: Get Many Messages

Channel: #general
Limit: 100

→ ได้ข้อความล่าสุด 100 ข้อความ

Best Practices

1. Use Block Kit

✅ ดี: Rich formatting with blocks
❌ ไม่ดี: Plain text only

Block Kit ทำให้ข้อความ:
- อ่านง่ายกว่า
- มี structure ชัดเจน
- Interactive ได้

2. Rate Limiting

Slack Rate Limits:
- 1 message per second per channel
- Tier 2: ~20 requests/minute

Solution:
- Batch messages
- Add delays between sends
- Use queue system

3. Error Messages ที่ดี

Include:
- What happened
- When it happened
- Severity level
- Action needed (if any)
- Link to more info

สรุป

Slack Integration Essentials:

  1. Setup: สร้าง Slack App + Bot Token
  2. Send Messages: Basic text หรือ Block Kit
  3. Triggers: Webhooks, Slash Commands, Events
  4. Interactive: Buttons, Menus, Forms

Common Use Cases:

  • Error alerts
  • Daily reports
  • Standup reminders
  • Approval workflows
  • Status commands

Best Practices:

  • ใช้ Block Kit formatting
  • จัดการ rate limits
  • ส่งข้อมูลที่เป็นประโยชน์

อ่านเพิ่มเติม:


เขียนโดย

AI Unlocked Team