LINE OA Integration

Connect LINE Official Account with n8n for automated chat responses

เชื่อมต่อ LINE Official Account กับ n8n เพื่อสร้าง chatbot อัตโนมัติ

Prerequisites

LINE OA Requirements

  • ✅ LINE Official Account (Business Type)
  • ✅ LINE OA ต้อง active แล้ว
  • ✅ มีสิทธิ์ใช้ Messaging API
  • ✅ Webhook URL สามารถเข้าถึงได้จาก internet

Required Information

จาก LINE Developers Console:

  • Channel Access Token (Long-lived)
  • Channel Secret
  • Webhook URL

Setup LINE OA in n8n

1. Add LINE OA Credential

  1. ไปที่ SettingsCredentials
  2. คลิก Add Credential
  3. เลือก LINE Official Account
  4. กรอกข้อมูล:
    Channel Access Token: xxxxxxxxxxxxxxxx
    Channel Secret: xxxxxxxxxxxxxxxx
    
  5. คลิก Save

2. Create Basic Chatbot Workflow

Step 1: Add Webhook Trigger

  1. สร้าง workflow ใหม่
  2. เพิ่ม Webhook node
  3. ตั้งค่า:
    • HTTP Method: POST
    • Path: /line-webhook
    • Authentication: None
    • Response Mode: On Received
  4. คัดลอก Webhook URL

Step 2: Configure LINE OA Webhook

  1. ไปที่ LINE Developers Console
  2. เลือก Channel ของคุณ
  3. ไปที่ Messaging APIWebhook settings
  4. วาง webhook URL จาก n8n:
    https://your-domain.com/webhook/line-webhook
    
  5. เปิด Use webhook
  6. คลิก Verify ต้องขึ้น Success

Step 3: Process LINE Messages

เพิ่ม Function node เพื่อประมวลผลข้อความ:

// ดึงข้อความจาก LINE
const events = $input.all()[0].json.events || [];
const messages = [];

for (const event of events) {
  if (event.type === 'message' && event.message.type === 'text') {
    messages.push({
      replyToken: event.replyToken,
      userId: event.source.userId,
      message: event.message.text,
      timestamp: event.timestamp
    });
  }
}

return messages.map(msg => ({
  json: msg
}));

Step 4: Send Reply

เพิ่ม LINE Official Account node:

  1. Operation: Send Reply Message
  2. Reply Token: {{ $json.replyToken }}
  3. Messages:
    [
      {
        "type": "text",
        "text": "ได้รับข้อความ: {{ $json.message }}"
      }
    ]
    

3. Test Your Chatbot

  1. ส่งข้อความไปที่ LINE OA
  2. ตรวจสอบว่าได้รับการตอบกลับ
  3. ดู execution history ใน n8n

Advanced Features

Auto Reply Based on Keywords

เพิ่ม IF node หลัง Function node:

// ใน Function node
const message = $json.message.toLowerCase();
let response = '';

if (message.includes('สวัสดี') || message.includes('ดี')) {
  response = 'สวัสดีครับ! มีอะไรให้ช่วยไหม?';
} else if (message.includes('ราคา') || message.includes('บริการ')) {
  response = 'สอบถามราคาและบริการได้ที่:\n📱 081-234-5678\n🌐 shantilink.com';
} else if (message.includes('ทำงาน') || messageเรื่อง('เวลา')) {
  response = 'เราเปิดบริการ:\nจันทร์-ศุกร์: 9:00-18:00\nเสาร์-อาทิตย์: ปิดบริการ';
} else {
  response = 'ขอบคุณสำหรับข้อความครับ\nทีมงานจะติดต่อกลับโดยเร็ว';
}

return [{
  json: {
    replyToken: $json.replyToken,
    response: response
  }
}];

Save Messages to Google Sheets

เพิ่ม Google Sheets node หลัง Function node:

  1. Operation: Append
  2. Document ID: your-spreadsheet-id
  3. Sheet Name: Messages
  4. Columns:
    [
      { "column": "Timestamp", "value": "{{ $now }}" },
      { "column": "User ID", "value": "{{ $json.userId }}" },
      { "column": "Message", "value": "{{ $json.message }}" }
    ]
    

Send Rich Messages

ใช้ Flex Message สำหรับข้อความสวยงาม:

// ใน Function node
const message = $json.message;

if (message.includes('เมนู')) {
  return [{
    json: {
      replyToken: $json.replyToken,
      flexMessage: {
        type: "flex",
        altText: "เมนูบริการ",
        contents: {
          type: "bubble",
          body: {
            type: "box",
            layout: "vertical",
            contents: [
              {
                type: "text",
                text: "🏠 ShantiLink",
                weight: "bold",
                size: "xl"
              },
              {
                type: "text",
                text: "บริการของเรา:",
                margin: "md"
              },
              {
                type: "text",
                text: "🤖 ระบบอัตโนมัติ\n💻 พัฒนาระบบ\n📱 LINE Application",
                margin: "sm"
              }
            ]
          },
          footer: {
            type: "box",
            layout: "vertical",
            spacing: "sm",
            contents: [
              {
                type: "button",
                style: "primary",
                height: "sm",
                action: {
                  type: "uri",
                  label: "ดูเว็บไซต์",
                  uri: "https://shantilink.com"
                }
              }
            ]
          }
        }
      }
    }
  }];
}

Common Issues

Webhook Verification Failed

  • ✅ ตรวจสอบว่า webhook URL ถูกต้อง
  • ✅ ตรวจสอบว่า server ออนไลน์
  • ✅ ตรวจสอบว่า n8n กำลังทำงาน

No Response from LINE OA

  • ✅ ตรวจสอบ Channel Access Token
  • ✅ ตรวจสอบว่า LINE OA active
  • ✅ ดู execution history ใน n8n

Rate Limit

LINE จำกัด 1,000 messages/minute:

  • ✅ ใช้ queue สำหรับ bulk messages
  • ✅ ตั้งเวลาส่งข้อความ
  • ✅ ใช้ LINE Notify สำหรับ broadcast

Best Practices

Security

  • ✅ ตรวจสอบ source ของข้อความ
  • ✅ กรองคำหยาบ
  • ✅ ไม่เปิดเผยข้อมูลส่วนตัว

Performance

  • ✅ ใช้ Function node อย่างมีประสิทธิภาพ
  • ✅ จัดเก็บข้อมูลอย่างมีระเบียบ
  • ✅ ตรวจสอบ error logs

User Experience

  • ✅ ตอบกลับเร็ว
  • ✅ ใช้ emoji ให้น่าสนใจ
  • ✅ มีตัวเลือกให้เลือก

Next Steps


ต้องการความช่วยเหลือ? ติดต่อเราได้ที่ ShantiLink.com 💬