s
This commit is contained in:
@@ -254,6 +254,28 @@ function showFallback() {
|
||||
|
||||
// ─── n8n Webhook (#5.2, #6) ──────────────────────────────────
|
||||
|
||||
/** Extract reply text from various n8n response formats */
|
||||
function extractReply(data) {
|
||||
if (!data) return null;
|
||||
// Handle array responses (n8n sometimes returns [{ ... }])
|
||||
const obj = Array.isArray(data) ? data[0] : data;
|
||||
if (!obj || typeof obj !== 'object') return typeof data === 'string' ? data : null;
|
||||
// Check common field names
|
||||
const keys = ['reply', 'output', 'text', 'message', 'response', 'answer', 'content'];
|
||||
for (const k of keys) {
|
||||
if (obj[k] && typeof obj[k] === 'string' && obj[k].trim()) return obj[k];
|
||||
}
|
||||
// Check nested: obj.data.text, obj.result.text, etc.
|
||||
for (const wrapper of ['data', 'result', 'body']) {
|
||||
if (obj[wrapper] && typeof obj[wrapper] === 'object') {
|
||||
for (const k of keys) {
|
||||
if (obj[wrapper][k] && typeof obj[wrapper][k] === 'string' && obj[wrapper][k].trim()) return obj[wrapper][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function postWebhook(body) {
|
||||
const url = (CFG.webhook_base || '').replace(/\/$/, '');
|
||||
const res = await fetch(url, {
|
||||
@@ -279,10 +301,10 @@ async function startSession(entry) {
|
||||
const typing = showTyping();
|
||||
try {
|
||||
const data = await postWebhook(payload);
|
||||
console.log('[FestivalGuide] start response:', JSON.stringify(data));
|
||||
if (typing) typing.remove();
|
||||
const reply = (data?.reply && typeof data.reply === 'string')
|
||||
? data.reply
|
||||
: (entry === 'truck' ? CFG.chat_welcome_truck : CFG.chat_welcome_family)
|
||||
const reply = extractReply(data)
|
||||
|| (entry === 'truck' ? CFG.chat_welcome_truck : CFG.chat_welcome_family)
|
||||
|| 'Hi! Ich bin dein Festival-Guide. Sag mir kurz: Tagesbesuch oder Wochenende?'; // #5.2 fallback
|
||||
appendBubble('bot', reply);
|
||||
} catch (_) {
|
||||
@@ -316,9 +338,9 @@ async function sendMessage(text) {
|
||||
const typing = showTyping();
|
||||
try {
|
||||
const data = await postWebhook(payload);
|
||||
console.log('[FestivalGuide] message response:', JSON.stringify(data));
|
||||
if (typing) typing.remove();
|
||||
const reply = (data?.reply && typeof data.reply === 'string' && data.reply.trim())
|
||||
? data.reply : '…';
|
||||
const reply = extractReply(data) || '…';
|
||||
appendBubble('bot', reply);
|
||||
} catch (_) {
|
||||
if (typing) typing.remove();
|
||||
|
||||
Reference in New Issue
Block a user