Files
karyaman-project/kloowear-app/old file/assets/js/chat.js
karyamanswasta 4fe4b13f39 chore(adventure-rental-app): strip down application and remove obsolete files
This commit performs a major cleanup of the `adventure-rental-app` by removing all existing components, pages, layouts, and project brief documents. The application is reduced to a minimal Vite shell.

- Deleted all authentication, chatbot, and dashboard components and pages.
- Removed outdated project brief and PRD markdown files.
- Updated Vite and `@vitejs/plugin-react` dependencies.

Additionally, the `framer-motion` dependency was added to the `bookoomoo-app`.
2025-08-10 10:27:39 +07:00

82 lines
2.5 KiB
JavaScript
Executable File

<script>
document.addEventListener('DOMContentLoaded', function() {
const chatMessages = document.getElementById('chat-messages');
const userInput = document.getElementById('user-message');
const sendBtn = document.getElementById('send-btn');
// Webhook Configuration
const WEBHOOK_URL = 'https://your-api-endpoint.com/webhook';
const SESSION_ID = generateSessionId(); // Generate unique session ID
// Send Message via Webhook
async function sendToWebhook(message) {
try {
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: SESSION_ID,
message: message,
timestamp: new Date().toISOString()
})
});
return await response.json();
} catch (error) {
console.error('Webhook error:', error);
return { error: true };
}
}
// Receive Message from Webhook
async function receiveFromWebhook() {
// Implement webhook listener (via WebSocket or polling)
// Example using polling (simplified):
setInterval(async () => {
const response = await fetch(`${WEBHOOK_URL}/get?session_id=${SESSION_ID}`);
const data = await response.json();
if (data.new_messages) {
data.new_messages.forEach(msg => {
addMessage(msg.content, 'ai');
});
}
}, 3000); // Poll every 3 seconds
}
// UI Functions
function addMessage(content, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}-message mb-3`;
messageDiv.innerHTML = `
<div class="message-content ${sender === 'ai' ? 'bg-dark text-white' : 'bg-light'} p-3 rounded">
${content}
</div>
<small class="text-muted">Baru saja</small>
`;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Event Listeners
sendBtn.addEventListener('click', async () => {
const message = userInput.value.trim();
if (message) {
addMessage(message, 'user');
userInput.value = '';
// Send to webhook
const response = await sendToWebhook(message);
if (response.error) {
addMessage("Maaf, Bakul Tali sedang sibuk. Coba lagi nanti.", 'ai');
}
}
});
// Start listening for responses
receiveFromWebhook();
// Helper function
function generateSessionId() {
return 'session_' + Math.random().toString(36).substr(2, 9);
}
});
</script>