File size: 1,499 Bytes
ceffc74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
document.addEventListener('DOMContentLoaded', function() {
    const userInput = document.getElementById('user-input');
    const sendButton = document.getElementById('send-button');
    const chatMessages = document.getElementById('chat-messages');

    // Simple bot responses
    const botResponses = {
        'hello': 'Hello! How can I help you?',
        'hi': 'Hi there! What\'s up?',
        'bye': 'Goodbye! Have a great day!',
        'default': 'Sorry, I didn\'t understand that. Try saying hello!'
    };

    function addMessage(text, sender) {
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('message', sender);
        messageDiv.textContent = text;
        chatMessages.appendChild(messageDiv);
        chatMessages.scrollTop = chatMessages.scrollHeight;
    }

    function getBotResponse(input) {
        const lowerInput = input.toLowerCase();
        return botResponses[lowerInput] || botResponses['default'];
    }

    function handleSend() {
        const message = userInput.value.trim();
        if (message) {
            addMessage(message, 'user');
            const response = getBotResponse(message);
            setTimeout(() => addMessage(response, 'bot'), 500); // Simulate delay
            userInput.value = '';
        }
    }

    sendButton.addEventListener('click', handleSend);
    userInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            handleSend();
        }
    });
});