choco-bot
Version:
Whatsapp-bot
110 lines (98 loc) • 3.31 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>WhatsApp-like Chat</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{margin-top:20px;}
.chat-online {
color: #34ce57
}
.chat-offline {
color: #e4606d
}
.chat-messages {
display: flex;
flex-direction: column;
max-height: 800px;
overflow-y: scroll
}
.chat-message-left,
.chat-message-right {
display: flex;
flex-shrink: 0
}
.chat-message-left {
margin-right: auto
}
.chat-message-right {
flex-direction: row-reverse;
margin-left: auto
}
.py-3 {
padding-top: 1rem;
padding-bottom: 1rem;
}
.px-4 {
padding-right: 1.5rem;
padding-left: 1.5rem;
}
.flex-grow-0 {
flex-grow: 0;
}
.border-top {
border-top: 1px solid #dee2e6;
}
</style>
</head>
<body>
<main class="content">
<div class="container p-0">
<h1 class="h3 mb-3">Messages</h1>
<div class="card">
<!-- Your existing HTML structure here -->
<div class="chat-messages p-4" id="chatMessages">
<!-- Chat messages will be dynamically added here -->
</div>
<div class="flex-grow-0 py-3 px-4 border-top">
<div class="input-group">
<input type="text" class="form-control" id="messageInput" placeholder="Type your message">
<button class="btn btn-primary" onclick="sendMessage()">Send</button>
</div>
</div>
</div>
</div>
</main>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Add this script to your HTML file -->
<script type="text/javascript">
function sendMessage() {
const message = $('#messageInput').val();
// Send the message to the server
$.post('http://localhost:3000/sendMessage', { message }, function(response) {
// Handle the response if needed
console.log(response);
// Append the new message to the chat
const newMessage = `
<div class="chat-message-right mb-4">
<div>
<img src="https://bootdey.com/img/Content/avatar/avatar1.png" class="rounded-circle mr-1" alt="You" width="40" height="40">
<div class="text-muted small text-nowrap mt-2">Now</div>
</div>
<div class="flex-shrink-1 bg-light rounded py-2 px-3 mr-3">
<div class="font-weight-bold mb-1">You</div>
${message}
</div>
</div>
`;
$('#chatMessages').append(newMessage);
// Clear the message input
$('#messageInput').val('');
});
}
</script>
</body>
</html>