update: sessionId

main
marszayn 2025-08-28 13:36:45 +07:00
parent c160b15e2f
commit 6e99954b46
3 changed files with 29 additions and 18 deletions

10
.gitignore vendored
View File

@ -1,3 +1,7 @@
/obj
/publish
/node_modules
bin/
obj/
publish/
appsettings.Development.json
*.user
*.db
node_modules/

View File

@ -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;
}
}

View File

@ -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 = `
<p>Halo! Selamat datang di layanan informasi Dinas Lingkungan Hidup DKI Jakarta! 👋</p>
<p>Saya di sini untuk membantu Anda. Ada yang ingin ditanyakan seputar layanan kami?</p>
@ -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() {
<script>
document.addEventListener('DOMContentLoaded', function() {
const UPDATE_INTERVAL = 5 * 60 * 1000; // 5 menit
const UPDATE_INTERVAL = 5 * 60 * 1000;
async function fetchVisitorStats(attempt = 1) {
const RETRY_ATTEMPTS = 3;