Update routes/facebook.js
Browse files- routes/facebook.js +8 -49
routes/facebook.js
CHANGED
|
@@ -35,7 +35,7 @@ module.exports = (processChatLogicWithTrace) => {
|
|
| 35 |
}
|
| 36 |
|
| 37 |
if (body && body.object === 'page') {
|
| 38 |
-
// 🌟 PHẢN HỒI 200 OK NGAY LẬP TỨC CHO GAS/FACEBOOK ĐỂ
|
| 39 |
res.status(200).send('EVENT_RECEIVED');
|
| 40 |
|
| 41 |
for (const entry of body.entry) {
|
|
@@ -50,7 +50,7 @@ module.exports = (processChatLogicWithTrace) => {
|
|
| 50 |
let sender_psid = webhook_event.sender.id;
|
| 51 |
let userMessage = "";
|
| 52 |
|
| 53 |
-
// Đọc
|
| 54 |
if (webhook_event.message && webhook_event.message.text) {
|
| 55 |
userMessage = webhook_event.message.text;
|
| 56 |
} else if (webhook_event.postback && webhook_event.postback.payload) {
|
|
@@ -68,53 +68,12 @@ module.exports = (processChatLogicWithTrace) => {
|
|
| 68 |
const result = await processChatLogicWithTrace(sender_psid, userMessage);
|
| 69 |
|
| 70 |
if (result && result.reply) {
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
//
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
while ((match = regex.exec(mainReply)) !== null) {
|
| 78 |
-
extractedButtons.push(match[1]);
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
// Làm sạch văn bản thô gửi đi
|
| 82 |
-
let cleanReply = mainReply.replace(/\[\[?([^\]]+)\]?\]/g, '').replace(/\s+/g, ' ').trim();
|
| 83 |
-
if (!cleanReply) cleanReply = mainReply;
|
| 84 |
-
|
| 85 |
-
// 🎯 CƠ CHẾ FIRE & FORGET (CHẠY NGẦM KHÔNG CHỜ PHẢN HỒI TỪ FACEBOOK)
|
| 86 |
-
if (mainReply.includes('SHOW_ORDER_FORM')) {
|
| 87 |
-
console.log("📋 [FB] Gửi form chốt đơn gợi ý (Chạy ngầm)...");
|
| 88 |
-
let guideText = `${cleanReply}\n\n(Mẹ nhấn vào nút "📝 Nhập thông tin" phía dưới hoặc nhắn tin theo mẫu: Tên: Hồng, SĐT: 093xxx, Địa chỉ: số nhà...)`;
|
| 89 |
-
|
| 90 |
-
// Đóng gói trực tiếp thành văn bản sạch để tránh lỗi logic chuỗi trong Service
|
| 91 |
-
facebookService.sendResponse(sender_psid, guideText)
|
| 92 |
-
.catch(err => console.error("❌ Lỗi gửi văn bản Form ngầm:", err.message));
|
| 93 |
-
|
| 94 |
-
} else if (extractedButtons.length > 0) {
|
| 95 |
-
console.log("🔘 [FB] Gửi danh sách nút bấm hành động (Chạy ngầm)...");
|
| 96 |
-
|
| 97 |
-
// Xây dựng mảng cấu trúc quick_replies hợp lệ
|
| 98 |
-
let quickRepliesPayload = extractedButtons.map(btnText => {
|
| 99 |
-
let parts = btnText.split('|');
|
| 100 |
-
return {
|
| 101 |
-
content_type: "text",
|
| 102 |
-
title: parts[0].trim().substring(0, 20),
|
| 103 |
-
payload: parts[1] ? parts[1].trim() : btnText.trim()
|
| 104 |
-
};
|
| 105 |
-
}).filter(item => item.title !== "");
|
| 106 |
-
|
| 107 |
-
// Gửi qua một Object có thuộc tính văn bản tường minh
|
| 108 |
-
facebookService.sendResponse(sender_psid, {
|
| 109 |
-
text: cleanReply,
|
| 110 |
-
quick_replies: quickRepliesPayload
|
| 111 |
-
}).catch(err => console.error("❌ Lỗi gửi nút bấm ngầm:", err.message));
|
| 112 |
-
|
| 113 |
-
} else {
|
| 114 |
-
console.log("💬 [FB] Gửi tin nhắn text thuần túy (Chạy ngầm)...");
|
| 115 |
-
facebookService.sendResponse(sender_psid, cleanReply)
|
| 116 |
-
.catch(err => console.error("❌ Lỗi gửi text ngầm:", err.message));
|
| 117 |
-
}
|
| 118 |
}
|
| 119 |
} catch (logicErr) {
|
| 120 |
console.error(`❌ Lỗi tại bộ não xử lý lõi Facebook:`, logicErr.message);
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
if (body && body.object === 'page') {
|
| 38 |
+
// 🌟 PHẢN HỒI 200 OK NGAY LẬP TỨC CHO GAS/FACEBOOK ĐỂ GIẢI PHÓNG TIẾN TRÌNH NHẬN
|
| 39 |
res.status(200).send('EVENT_RECEIVED');
|
| 40 |
|
| 41 |
for (const entry of body.entry) {
|
|
|
|
| 50 |
let sender_psid = webhook_event.sender.id;
|
| 51 |
let userMessage = "";
|
| 52 |
|
| 53 |
+
// Đọc tin nhắn chat hoặc mã Payload từ các nút bấm phản hồi nhanh
|
| 54 |
if (webhook_event.message && webhook_event.message.text) {
|
| 55 |
userMessage = webhook_event.message.text;
|
| 56 |
} else if (webhook_event.postback && webhook_event.postback.payload) {
|
|
|
|
| 68 |
const result = await processChatLogicWithTrace(sender_psid, userMessage);
|
| 69 |
|
| 70 |
if (result && result.reply) {
|
| 71 |
+
console.log(`💬 [FB] Đang chuyển tiếp chuỗi text thô sang facebookService xử lý ngầm...`);
|
| 72 |
+
|
| 73 |
+
// 🎯 CƠ CHẾ CHẠY NGẦM AN TOÀN TUYỆT ĐỐI (FIRE & FORGET):
|
| 74 |
+
// Truyền CHUỖI TEXT THUẦN (String) để facebookService tự bóc tách Carousel, Webview, Quick Replies
|
| 75 |
+
facebookService.sendResponse(sender_psid, result.reply)
|
| 76 |
+
.catch(err => console.error("❌ Lỗi gửi tin nhắn ngầm:", err.message));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
} catch (logicErr) {
|
| 79 |
console.error(`❌ Lỗi tại bộ não xử lý lõi Facebook:`, logicErr.message);
|