From f09516e9dc2ab11eee1cf7d20abbfbc4fa9c1cc4 Mon Sep 17 00:00:00 2001 From: Ihor_Zhekov Date: Thu, 26 Feb 2026 10:39:05 +0100 Subject: [PATCH] s --- WebApp/app.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/WebApp/app.js b/WebApp/app.js index 8bc47fa..83f9d13 100644 --- a/WebApp/app.js +++ b/WebApp/app.js @@ -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,11 +301,11 @@ 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) - || 'Hi! Ich bin dein Festival-Guide. Sag mir kurz: Tagesbesuch oder Wochenende?'; // #5.2 fallback + 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 (_) { if (typing) typing.remove(); @@ -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();