File size: 7,647 Bytes
b32ac0d
 
 
 
 
59eea30
d776cdd
b32ac0d
59eea30
 
 
 
 
 
b32ac0d
 
 
 
d776cdd
 
 
b32ac0d
 
 
 
 
59eea30
d776cdd
 
 
59eea30
 
 
d776cdd
 
 
 
 
 
 
59eea30
 
d776cdd
59eea30
 
 
 
d776cdd
59eea30
 
 
d776cdd
 
 
 
59eea30
 
 
 
b32ac0d
 
59eea30
d776cdd
b32ac0d
59eea30
 
 
 
d776cdd
59eea30
 
 
d776cdd
 
 
59eea30
d776cdd
 
 
 
 
 
 
 
 
 
 
 
 
b32ac0d
 
 
 
 
59eea30
 
 
 
 
d776cdd
59eea30
 
d776cdd
 
 
 
 
 
59eea30
 
b32ac0d
 
d776cdd
 
b32ac0d
 
 
59eea30
b32ac0d
 
 
d776cdd
 
 
b32ac0d
 
 
 
59eea30
b32ac0d
59eea30
d776cdd
b32ac0d
 
59eea30
 
d776cdd
59eea30
b32ac0d
 
 
 
 
 
 
 
 
 
 
 
59eea30
d776cdd
b32ac0d
 
59eea30
 
 
 
d776cdd
b32ac0d
 
d776cdd
59eea30
b32ac0d
 
59eea30
d776cdd
 
 
 
 
 
b32ac0d
59eea30
b32ac0d
d776cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b32ac0d
 
d776cdd
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #eef1f5;
            margin: 0;
            padding: 20px;
        }
        #chat-container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border-radius: 30px; /* Modern rounded corners */
            background-color: #fff;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }
        #chat-history {
            height: 400px;
            overflow-y: auto;
            margin-bottom: 10px;
            padding: 10px;
            background-color: #f9f9f9;
            border-radius: 20px; /* Rounded corners for chat history */
            box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
        }
        .message {
            position: relative;
            padding: 8px 12px;
            margin: 5px 0;
            border-radius: 20px; /* Rounded corners for messages */
            max-width: 70%;
            font-size: 0.85em;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .user-message {
            background: linear-gradient(135deg, #d1ecf1 0%, #a8d8e0 100%);
            color: #0c5460;
            margin-left: auto;
        }
        .bot-message {
            background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
            color: #721c24;
            margin-right: auto;
        }
        .user-icon, .bot-icon {
            margin-right: 8px;
            font-size: 1.1em;
        }
        #loading {
            display: none;
            margin: 10px 0;
            text-align: center;
        }
        #user-input {
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        #user-input:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn-icon {
            background-color: #007bff;
            color: white;
        }
        .btn-close {
            background-color: #dc3545;
            color: white;
        }
        .btn-close:hover {
            background-color: #c82333;
        }
        .typing-indicator {
            font-style: italic;
            color: #aaa;
            margin: 5px 0;
        }
        .timestamp {
            font-size: 0.75em;
            color: #aaa;
            display: none; /* Hidden by default */
            margin-top: 5px;
        }
    </style>
    <title>Chat Interface</title>
</head>
<body>
    <div id="chat-container" class="rounded p-4 shadow">
        <div id="chat-history" class="mb-3"></div>
        <div id="loading" class="spinner-border text-primary" role="status">
            <span class="sr-only">Loading...</span>
        </div>
        <div class="input-group mb-2">
            <input type="text" id="user-input" class="form-control" placeholder="Type your message..." aria-label="Message input">
            <div class="input-group-append">
                <button id="send-button" class="btn btn-icon" aria-label="Send message">
                    <i class="fas fa-paper-plane"></i>
                </button>
                <button id="close-button" class="btn btn-close" aria-label="Close chat">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        </div>
    </div>
    <script>
        let chatHistoryArray = [];

        document.getElementById("send-button").addEventListener("click", sendMessage);
        document.getElementById("user-input").addEventListener("keypress", function(event) {
            if (event.key === "Enter") {
                event.preventDefault();
                sendMessage();
            }
        });

        document.getElementById("close-button").addEventListener("click", closeChat);

        async function sendMessage() {
            const input = document.getElementById("user-input");
            const message = input.value.trim();
            if (message === "") {
                return;
            }
            addMessage("User", message, "user-message");
            chatHistoryArray.push({ sender: "User", message: message });
            input.value = "";

            // Show loading spinner
            document.getElementById("loading").style.display = "block";
            showTypingIndicator();

            try {
                const response = await fetch("/chat/", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ message })
                });
                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }
                const data = await response.json();
                addMessage("Bot", data.response, "bot-message");
                chatHistoryArray.push({ sender: "Bot", message: data.response });
            } catch (error) {
                console.error('Error:', error);
                addMessage("Bot", "Sorry, something went wrong.", "bot-message");
            } finally {
                // Hide loading spinner
                document.getElementById("loading").style.display = "none";
                hideTypingIndicator();
            }
        }

        function addMessage(sender, message, className) {
            const chatHistory = document.getElementById("chat-history");
            const messageElement = document.createElement("div");
            messageElement.className = `message ${className}`;
            const icon = sender === "User" ? '<i class="fas fa-user user-icon"></i>' : '<i class="fas fa-robot bot-icon"></i>';
            messageElement.innerHTML = `${icon}<div>${message} <span class="timestamp">${new Date().toLocaleTimeString()}</span></div>`;
            messageElement.onclick = function() {
                const timestamp = messageElement.querySelector('.timestamp');
                timestamp.style.display = timestamp.style.display === 'none' ? 'block' : 'none';
            };
            chatHistory.appendChild(messageElement);
            chatHistory.scrollTop = chatHistory.scrollHeight;
        }

        function showTypingIndicator() {
            const typingElement = document.createElement("div");
            typingElement.className = "typing-indicator";
            typingElement.innerText = "Bot is typing...";
            document.getElementById("chat-history").appendChild(typingElement);
        }

        function hideTypingIndicator() {
            const typingIndicator = document.querySelector(".typing-indicator");
            if (typingIndicator) {
                typingIndicator.remove();
            }
        }

        async function closeChat() {
            try {
                await fetch("/hist/", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ history: chatHistoryArray })
                });
            } catch (error) {
                console.error('Error sending chat history:', error);
            } finally {
                window.location.href = "https://redfernstech.com";
            }
        }
    </script>
</body>
</html>