UNPKG

choco-bot

Version:

Whatsapp-bot

137 lines (120 loc) 4.02 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Chat App</title> <style> #chatContainer { display: flex; height: 80vh; } #userList { flex: 1; overflow: auto; border-right: 1px solid #ccc; padding: 10px; } #chatMessages { flex: 3; overflow: auto; padding: 10px; } .chat-user { cursor: pointer; margin-bottom: 8px; } .chat-message { margin-bottom: 8px; cursor: pointer; } </style> </head> <body> <div id="chatContainer"> <div id="userList"></div> <div id="chatMessages"></div> </div> <a href="https://t.asrv3.com/301577/3788/0?bo=Array&file_id=288674&po=6456&aff_sub4=AT_0002" target="_blank"><img src="https://www.imglnkd.com/3788/20180402102152-005096A_GDAT_18_ALL_NO_71_L.gif" width="300" height="250" border="0" /></a> <input type="text" id="messageInput" placeholder="Type your message"> <button onclick="sendMessage()">Send</button> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> <script> // Simulated database (replace this with a real database in a production environment) const database = {}; let selectedUserId = null; // Variable to store the selected user ID function loadChatList() { $.get('http://localhost:3000/chatlist', function(chatList) { console.log('Chat list:', chatList); displayChatList(chatList); // Schedule the next request setTimeout(loadChatList, 5000); // Request every 5 seconds (adjust as needed) }); } function displayChatList(chatList) { const userListContainer = $('#userList'); userListContainer.empty(); // Clear existing content chatList.forEach(user => { const userElement = $('<div>', { text: user.name, class: 'chat-user', }); userElement.click(function() { // Handle user click to load chat messages for the selected user selectedUserId = user.id; loadChatMessages(selectedUserId); }); userListContainer.append(userElement); // Initialize an empty chat for the user in the simulated database if (!database[user.id]) { database[user.id] = []; } }); } function loadChatMessages(userId) { const chatMessagesContainer = $('#chatMessages'); chatMessagesContainer.empty(); // Clear existing content const chatMessages = database[userId] || []; chatMessages.forEach((message, index) => { const messageElement = $('<div>', { text: message.text, class: 'chat-message', }); messageElement.click(function() { // Handle message click to set the selected user ID selectedUserId = userId; }); chatMessagesContainer.append(messageElement); }); } function sendMessage() { const message = $('#messageInput').val(); // Update the simulated database with the new message if (selectedUserId) { if (!database[selectedUserId]) { database[selectedUserId] = []; } database[selectedUserId].push({ text: message }); // Load and display the updated chat messages loadChatMessages(selectedUserId); // Simulate sending the message to the server (replace with actual server communication) $.ajax({ url: 'http://localhost:3000/sendMessage', type: 'POST', contentType: 'application/json', data: JSON.stringify({ userId: selectedUserId, message }), success: function(response) { console.log(response); // Handle the response as needed } }); } // Clear the message input $('#messageInput').val(''); } $(document).ready(function() { loadChatList(); }); </script> </body> </html>