เชื่อมต่อ 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
- ไปที่
Settings→Credentials - คลิก
Add Credential - เลือก
LINE Official Account - กรอกข้อมูล:
Channel Access Token: xxxxxxxxxxxxxxxx Channel Secret: xxxxxxxxxxxxxxxx - คลิก
Save
2. Create Basic Chatbot Workflow
Step 1: Add Webhook Trigger
- สร้าง workflow ใหม่
- เพิ่ม
Webhooknode - ตั้งค่า:
- HTTP Method:
POST - Path:
/line-webhook - Authentication:
None - Response Mode:
On Received
- HTTP Method:
- คัดลอก Webhook URL
Step 2: Configure LINE OA Webhook
- ไปที่ LINE Developers Console
- เลือก Channel ของคุณ
- ไปที่
Messaging API→Webhook settings - วาง webhook URL จาก n8n:
https://your-domain.com/webhook/line-webhook - เปิด
Use webhook - คลิก
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:
- Operation:
Send Reply Message - Reply Token:
{{ $json.replyToken }} - Messages:
[ { "type": "text", "text": "ได้รับข้อความ: {{ $json.message }}" } ]
3. Test Your Chatbot
- ส่งข้อความไปที่ LINE OA
- ตรวจสอบว่าได้รับการตอบกลับ
- ดู 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:
- Operation:
Append - Document ID:
your-spreadsheet-id - Sheet Name:
Messages - 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
- Google Sheets Integration - บันทึกข้อมูล
- Half-Stack Approach - พัฒนา custom features
ต้องการความช่วยเหลือ? ติดต่อเราได้ที่ ShantiLink.com 💬