From 6e99954b4681921b010cd32a4ec62dc0572ccedf Mon Sep 17 00:00:00 2001 From: marszayn Date: Thu, 28 Aug 2025 13:36:45 +0700 Subject: [PATCH] update: sessionId --- .gitignore | 10 ++++++--- Controllers/ApiProxyController.cs | 5 +++-- Views/Shared/Partials/_Footer.cshtml | 32 +++++++++++++++++----------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index db18bf0d..dbf6da6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ -/obj -/publish -/node_modules \ No newline at end of file +bin/ +obj/ +publish/ +appsettings.Development.json +*.user +*.db +node_modules/ diff --git a/Controllers/ApiProxyController.cs b/Controllers/ApiProxyController.cs index 117fb5f9..9faf3644 100644 --- a/Controllers/ApiProxyController.cs +++ b/Controllers/ApiProxyController.cs @@ -11,7 +11,7 @@ public class ApiProxyController : ControllerBase { private readonly HttpClient _httpClient; - private const string N8nWebhookUrl = "http://10.50.50.61:5678/webhook/f2129606-7716-415b-a83b-b9c0e84b752f/chat"; + private const string N8nWebhookUrl = "http://10.50.50.61:5678/webhook/f2129606-7716-415b-a83b-b9c0e84b752f/chat"; public ApiProxyController(IHttpClientFactory httpClientFactory) { @@ -23,7 +23,7 @@ public class ApiProxyController : ControllerBase { try { - var payload = new { message = request.Message }; + var payload = new { message = request.Message, sessionId = request.SessionId }; var json = JsonSerializer.Serialize(payload); var content = new StringContent(json, Encoding.UTF8, "application/json"); @@ -57,5 +57,6 @@ public class ApiProxyController : ControllerBase public class ChatRequest { public string Message { get; set; } = string.Empty; + public string SessionId { get; set; } = string.Empty; } } diff --git a/Views/Shared/Partials/_Footer.cshtml b/Views/Shared/Partials/_Footer.cshtml index ec753b2a..d057da15 100644 --- a/Views/Shared/Partials/_Footer.cshtml +++ b/Views/Shared/Partials/_Footer.cshtml @@ -443,11 +443,9 @@ document.addEventListener('DOMContentLoaded', function() { const sendMessage = document.getElementById("sendMessage"); const userInput = document.getElementById("userInput"); - // Session configuration const SESSION_DURATION = 30 * 60 * 1000; const STORAGE_KEY = 'ecobot_chat_session'; - // Load chat history from localStorage function loadChatHistory() { try { const sessionData = localStorage.getItem(STORAGE_KEY); @@ -455,12 +453,9 @@ document.addEventListener('DOMContentLoaded', function() { const data = JSON.parse(sessionData); const now = new Date().getTime(); - // Check if session is still valid if (now - data.timestamp < SESSION_DURATION) { const chatContent = document.querySelector(".chat-content"); - // Clear default welcome message chatContent.innerHTML = ''; - // Load saved messages data.messages.forEach(msg => { const bubble = document.createElement("div"); bubble.className = `chat-bubble ${msg.type}`; @@ -470,9 +465,8 @@ document.addEventListener('DOMContentLoaded', function() { chatContent.appendChild(bubble); }); chatContent.scrollTop = chatContent.scrollHeight; - return true; // Session restored + return true; } else { - // Session expired, clear storage localStorage.removeItem(STORAGE_KEY); } } @@ -480,10 +474,9 @@ document.addEventListener('DOMContentLoaded', function() { console.error('Error loading chat history:', error); localStorage.removeItem(STORAGE_KEY); } - return false; // New session + return false; } - // Save message to localStorage function saveMessage(type, content) { try { let sessionData = localStorage.getItem(STORAGE_KEY); @@ -505,10 +498,12 @@ document.addEventListener('DOMContentLoaded', function() { } } - // Initialize chat session + function generateSessionId() { + return 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); + } + const sessionRestored = loadChatHistory(); if (!sessionRestored) { - // New session - show welcome message and save it const welcomeMsg = `

Halo! Selamat datang di layanan informasi Dinas Lingkungan Hidup DKI Jakarta! 👋

Saya di sini untuk membantu Anda. Ada yang ingin ditanyakan seputar layanan kami?

@@ -561,6 +556,17 @@ document.addEventListener('DOMContentLoaded', function() { chatContent.appendChild(typingIndicator); chatContent.scrollTop = chatContent.scrollHeight; + // Get sessionId from localStorage + let sessionId = null; + try { + const sessionData = localStorage.getItem(STORAGE_KEY); + if (sessionData) { + const data = JSON.parse(sessionData); + sessionId = data.sessionId; + } + } catch (error) { + console.error('Error retrieving sessionId:', error); + } fetch('/api/apiproxy/chatbot', { method: 'POST', headers: { @@ -570,12 +576,12 @@ document.addEventListener('DOMContentLoaded', function() { body: JSON.stringify({ message: message }) + sessionId: sessionId }) .then(response => { return response.json().catch(() => response.text()); }) .then(data => { - // Remove typing indicator typingIndicator.remove(); let reply = ""; @@ -632,7 +638,7 @@ document.addEventListener('DOMContentLoaded', function() {