การสร้าง LINE Chatbot โดยใช้ Dialogflow ร่วมกับ Next.js 14 Startup Website Template สามารถช่วยให้บอทของคุณมีความสามารถในการประมวลผลภาษาธรรมชาติ (NLP) และตอบสนองได้อย่างชาญฉลาดยิ่งขึ้น เนื่องจาก Dialogflow สามารถวิเคราะห์ข้อความของผู้ใช้และส่งคำตอบตามบริบทที่กำหนดไว้ได้อย่างดี
หากคุณยังไม่มีโปรเจค Next.js 14 Template สามารถ clone ได้จาก GitHub หรือใช้โปรเจคที่มีอยู่แล้ว:
git clone https://github.com/your-template-repo/nextjs-14-startup.git cd nextjs-14-startup
จากนั้นติดตั้งแพ็กเกจ:
npm install
ติดตั้ง line-bot-sdk สำหรับเชื่อมต่อกับ LINE Messaging API และ dialogflow สำหรับสื่อสารกับ Dialogflow:
npm install line-bot-sdk dialogflow
npm install line-bot-sdk
https://your-domain.com/api/dialogflow-webhookที่ root ของโปรเจค ให้สร้างไฟล์ .env.local และเพิ่มข้อมูลดังนี้:
LINE_CHANNEL_ACCESS_TOKEN=ใส่_Channel_Access_Token_ของคุณ LINE_CHANNEL_SECRET=ใส่_Channel_Secret_ของคุณ DIALOGFLOW_PROJECT_ID=ใส่_Project_ID_ของคุณ DIALOGFLOW_CLIENT_EMAIL=ใส่_Client_Email_ของคุณ DIALOGFLOW_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nYourPrivateKey\n-----END PRIVATE KEY-----\n"
หมายเหตุ: ข้อมูล DIALOGFLOW_PRIVATE_KEY สามารถหาได้จากไฟล์ Service Account JSON ที่คุณดาวน์โหลดจาก Google Cloud Console
line-webhook.js ในโฟลเดอร์ pages/apiสร้าง API Route เพื่อรองรับการทำงานของ LINE Webhook:
import { Client, middleware } from 'line-bot-sdk';
import { SessionsClient } from 'dialogflow';
const lineConfig = {
channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
};
const client = new Client(lineConfig);
// สร้าง client สำหรับ Dialogflow
const dialogflowConfig = {
credentials: {
client_email: process.env.DIALOGFLOW_CLIENT_EMAIL,
private_key: process.env.DIALOGFLOW_PRIVATE_KEY.replace(/\\n/g, '\n'),
},
};
const dialogflowClient = new SessionsClient(dialogflowConfig);
const projectId = process.env.DIALOGFLOW_PROJECT_ID;
const handleEvent = async (event) => {
if (event.type === 'message' && event.message.type === 'text') {
const sessionPath = dialogflowClient.projectAgentSessionPath(
projectId,
'unique-session-id'
);
// สร้างคำขอไปยัง Dialogflow
const request = {
session: sessionPath,
queryInput: {
text: {
text: event.message.text,
languageCode: 'en-US', // กำหนดภาษาที่ใช้
},
},
};
// ส่งคำขอและรับคำตอบจาก Dialogflow
const responses = await dialogflowClient.detectIntent(request);
const result = responses[0].queryResult;
return client.replyMessage(event.replyToken, {
type: 'text',
text: result.fulfillmentText,
});
}
return Promise.resolve(null);
};
export default async function handler(req, res) {
const middlewareCheck = middleware(lineConfig);
await middlewareCheck(req, res, async () => {
const events = req.body.events;
const results = await Promise.all(events.map(handleEvent));
return res.status(200).json({ status: 'success', results });
});
}
export const config = {
api: {
bodyParser: false,
},
};
สร้างไฟล์ dialogflow-webhook.js ในโฟลเดอร์ pages/api:
export default async function handler(req, res) {
const queryResult = req.body.queryResult;
// ตอบกลับข้อความแบบต่างๆ
let responseText = 'I am not sure how to respond to that.';
if (queryResult.intent.displayName === 'Welcome Intent') {
responseText = 'Hello! How can I assist you today?';
} else if (queryResult.intent.displayName === 'Order Intent') {
responseText = 'What would you like to order?';
}
res.json({
fulfillmentText: responseText,
});
}
export const config = {
api: {
bodyParser: false,
},
};
https://your-domain.com/api/line-webhookhttps://your-domain.com/api/dialogflow-webhooknpm run dev
2. ส่งข้อความใน LINE Chatbot ของคุณ และตรวจสอบว่ามีการตอบกลับตามที่คุณตั้งค่าใน Dialogflow หรือไม่
Deploy โปรเจคไปยัง Vercel, Netlify, หรือโฮสต์อื่นๆ ที่รองรับการทำงานของ Next.js และทดสอบบน Live URL ของคุณ!
เพียงเท่านี้ก็สามารถสร้าง LINE Chatbot ที่ทำงานร่วมกับ Dialogflow บน Next.js 14 Template ได้สำเร็จแล้ว! หากต้องการรูปประกอบ หรือมีข้อสงสัยเพิ่มเติมในขั้นตอนใด สามารถแจ้งได้เลยครับ!