// Replace the addMessage function in whatsapp-template.html // Replace these functions in your whatsapp-template.html file function addMessage(text, type, source = null) { const chatContainer = document.getElementById('chat-container'); const now = new Date(); const timeString = now.getHours() + ':' + (now.getMinutes() < 10 ? '0' : '') + now.getMinutes(); const messageDiv = document.createElement('div'); messageDiv.className = `message ${type}`; let sourceHtml = ''; if (source) { sourceHtml = `
`; } messageDiv.innerHTML = ` `; chatContainer.appendChild(messageDiv); chatContainer.scrollTop = chatContainer.scrollHeight; } function fetchTelegramMessages() { fetch('fetch-telegram-messages.php') .then(response => response.json()) .then(data => { if (data.messages && data.messages.length > 0) { const chatContainer = document.getElementById('chat-container'); // Add new messages data.messages.forEach(msg => { // Cek apakah pesan sudah ada di chat (hindari duplikasi) const msgText = msg.text; const existingMessages = document.querySelectorAll('.message-text'); let isDuplicate = false; existingMessages.forEach(existingMsg => { if (existingMsg.textContent === msgText) { isDuplicate = true; } }); if (!isDuplicate) { addMessage(msg.text, msg.type, msg.source); } }); // Scroll to bottom chatContainer.scrollTop = chatContainer.scrollHeight; } }) .catch(error => { console.error('Error fetching Telegram messages:', error); }); } // Also update the fetchTelegramMessages function function fetchTelegramMessages() { fetch('fetch-telegram-messages.php') .then(response => response.json()) .then(data => { if (data.messages && data.messages.length > 0) { const chatContainer = document.getElementById('chat-container'); // Clear existing messages if needed // chatContainer.innerHTML = ''; // Add new messages data.messages.forEach(msg => { // Cek apakah pesan sudah ada di chat (hindari duplikasi) const msgText = msg.text; const existingMessages = document.querySelectorAll('.message-text'); let isDuplicate = false; existingMessages.forEach(existingMsg => { if (existingMsg.textContent === msgText) { isDuplicate = true; } }); if (!isDuplicate) { addMessage(msg.text, msg.type, msg.source); } }); // Scroll to bottom chatContainer.scrollTop = chatContainer.scrollHeight; } }) .catch(error => { console.error('Error fetching Telegram messages:', error); }); }