File size: 10,115 Bytes
b32ac0d
 
 
 
 
59eea30
d776cdd
b32ac0d
59eea30
 
 
 
1a5192d
59eea30
b32ac0d
1a5192d
27c6e32
b32ac0d
 
9ad25d2
3ae6950
d776cdd
b32ac0d
 
dffd83c
b32ac0d
 
59eea30
d776cdd
3ae6950
9ad25d2
d776cdd
59eea30
 
 
d776cdd
 
9ad25d2
d776cdd
 
 
 
59eea30
 
d776cdd
59eea30
 
0669d5d
59eea30
 
d776cdd
59eea30
 
3cfb372
59eea30
d776cdd
 
 
 
59eea30
 
 
 
b32ac0d
 
59eea30
d776cdd
27c6e32
b32ac0d
59eea30
 
 
 
d776cdd
59eea30
 
 
27c6e32
 
 
b78bdd5
27c6e32
 
 
 
 
 
 
 
 
 
 
 
 
 
d776cdd
 
 
 
 
 
 
 
9ad25d2
d776cdd
b32ac0d
 
 
 
 
59eea30
9ad25d2
338564e
32786fd
338564e
59eea30
d776cdd
27c6e32
59eea30
d776cdd
 
 
59eea30
 
f03a379
27c6e32
b32ac0d
d776cdd
b32ac0d
 
27c6e32
 
b32ac0d
 
 
d776cdd
9ad25d2
8210f80
 
 
 
 
 
 
 
 
 
 
 
 
f03a379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d776cdd
9ad25d2
d776cdd
 
 
 
 
 
9ad25d2
d776cdd
 
27c6e32
d776cdd
27c6e32
e50be3f
27c6e32
 
 
f03a379
 
 
 
 
 
 
 
7ea5fdb
f03a379
7ea5fdb
f03a379
7ea5fdb
f03a379
7ea5fdb
f03a379
7ea5fdb
f03a379
 
 
 
 
 
a1b8878
f03a379
 
 
 
 
27c6e32
 
 
 
 
 
dffd83c
27c6e32
a91d3d2
27c6e32
 
 
 
 
 
d776cdd
 
b32ac0d
 
f03a379
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!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: 0;
        }
        #chat-container {
            max-width: 500px;
            position: relative;
            margin: auto;
            padding: 20px;
            border-radius: 30px;
            background-color: #fafafa;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }
        #chat-history {
            height: 490px;
            overflow-y: auto;
            margin-bottom: 10px;
            padding: 10px;
            background-color: #f9f9f9;
            background: linear-gradient(135deg, #f0f4ff 0%, #ffd1d1 100%);
            border-radius: 20px;
            box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
        }
        .message {
            position: relative;
            padding: 8px 12px;
            margin: 5px 0;
            border-radius: 20px;
            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;
            border-radius: 15px 15px 0 15px;
        }
        .bot-message {
            background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
            color: #721c24;
            margin-right: auto;
            border-radius: 15px 15px 15px 0;
        }
        .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;
            resize: none; /* Prevents resizing */
        }
        #user-input:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn-icon {
            background-color: #007bff;
            color: white;
        }
        .btn-close {
            position: absolute; /* Keep it absolute within the chat container */
            top: 10px; /* Adjust top position */
            right: 10px; /* Adjust left position */
            background-color: #dc3545; /* Red color for close button */
            color: white;
            border: none; /* Remove border for a cleaner look */
            border-radius: 50%; /* Round shape */
            width: 30px; /* Set width */
            height: 30px; /* Set height */
            display: flex; /* Flexbox for centering icon */
            align-items: center; /* Center vertically */
            justify-content: center; /* Center horizontally */
            transition: background-color 0.3s; /* Add transition for hover effect */
        }
        .btn-close:hover {
            background-color: #c82333; /* Darker red on hover */
        }
        .typing-indicator {
            font-style: italic;
            color: #aaa;
            margin: 5px 0;
        }
        .timestamp {
            font-size: 0.75em;
            color: #aaa;
            display: none;
            margin-top: 5px;
        }
    </style>
    <title>Chat Interface</title>
</head>
<body>
    <div id="chat-container" class="rounded p-4 shadow">
        <input type="hidden" id="user-id" value="{{ user_id }}">
     <button type="hidden" id="close-button" class="btn btn-close" aria-label="Close chat" hidden>
            <i class="fas fa-arrow-left"></i>
        </button>
        <div id="chat-history" class="mb-3"></div>
        <div class="input-group mb-2">
            <textarea id="user-input" class="form-control" rows="2" placeholder="Type your message..." aria-label="Message input"></textarea>
            <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>
            </div>
        </div>
        <small class="form-text text-muted">Press Shift + Enter for a new line</small>
    </div>    
    <script>
        let chatHistoryArray = [];
        document.getElementById("send-button").addEventListener("click", sendMessage);
        document.getElementById("user-input").addEventListener("keypress", function(event) {
            if (event.key === "Enter" && !event.shiftKey) { // Only send if Enter is pressed without Shift
                event.preventDefault(); // Prevent default newline behavior
                sendMessage();
            }
        });
        document.getElementById("close-button").addEventListener("click", closeChat);
        
       window.addEventListener('DOMContentLoaded', () => {
    sendInitialBotMessage(); // Trigger initial message when chatbot opens
});

function sendInitialBotMessage() {
    const userId = document.getElementById("user-id").value;
    const initialMessage = "Hello! How can I assist you today?"; // Initial bot message

    addMessage("Bot", initialMessage, "bot-message"); // Display the initial bot message
    chatHistoryArray.push({ userId, sender: "Bot", message: initialMessage }); // Save to chat history
}

async function sendMessage() {
    const input = document.getElementById("user-input");
    const userId = document.getElementById("user-id").value;
    const message = input.value.trim();
    if (message === "") {
        return;
    }
    addMessage("User", message, "user-message");
    chatHistoryArray.push({ userId, sender: "User", message });
    input.value = "";
    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({ userId, sender: "Bot", message: data.response });
    } catch (error) {
        console.error('Error:', error);
        addMessage("Bot", "Sorry, something went wrong.", "bot-message");
    } finally {
        hideTypingIndicator();
    }
}
        async function closeChat() {
            const userId = document.getElementById("user-id").value;
            try {
                await fetch("/hist/", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ history: chatHistoryArray, userId })
                });
            } catch (error) {
                console.error('Error sending chat history on close:', error);
            } finally {
                const chatContainer = document.getElementById("chat-container");
                window.top.location.href = 'https://redfernstech.com/'; // Or use chatContainer.remove(); to completely remove it
            }
        }
        function addMessage(sender, message, className) {
    const chatHistory = document.getElementById("chat-history");
    const messageElement = document.createElement("div");
    messageElement.className = `message ${className}`;
    // Check if the message contains a URL
    const linkRegex = /(https?:\/\/[^\s]+)/g;
    const formattedMessage = message.replace(linkRegex, function(url) {
        let linkText;
        if (url === "https://appexchange.salesforce.com/appxListingDetail?listingId=cf4b19d4-8667-49f7-83b0-d2bc4032527b") {
            linkText = "Click here";
        } else if (url === "https://appexchange.salesforce.com/appxListingDetail?listingId=e671f4fe-92fb-4760-99e5-7a5df5754cfe") {
            linkText = "Click here";
        } else if (url === "https://appexchange.salesforce.com/appxListingDetail?listingId=3473ffd3-d530-462f-828f-d2c69f80d89d") {
            linkText = "Click here";
        } else if (url === "https://appexchange.salesforce.com/appxListingDetail?listingId=6d605bd9-de3c-49d3-9fa4-ec3caabd5d63") {
            linkText = "Click here";
        } else if (url === "https://redfernstech.com/careers/") {
            linkText = "Click here";
        } else {
            linkText = "Visit this link."; // Fallback for other URLs
        }
        return `<a href="${url}" target="_blank">${linkText}</a>`;
    });
    messageElement.innerHTML = `<span class="${sender.toLowerCase()}-icon">
        ${sender === "User" ? '<i class="fas fa-user"></i>' : '<img src="https://i.ibb.co/khk6Ywx/bot.jpg" alt="Bot" style="width: 20px; height: 20px; border-radius: 50%;">'}
    </span>${formattedMessage}<div class="timestamp">${new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</div>`;
    chatHistory.appendChild(messageElement);
    chatHistory.scrollTop = chatHistory.scrollHeight; // Scroll to bottom
}

        
        function showTypingIndicator() {
            const chatHistory = document.getElementById("chat-history");
            const typingIndicator = document.createElement("div");
            typingIndicator.className = "typing-indicator";
            typingIndicator.id = "typing-indicator";
            typingIndicator.innerText = "Clara is typing...";
            chatHistory.appendChild(typingIndicator);
            chatHistory.scrollTop = chatHistory.scrollHeight; 
        }
        
        function hideTypingIndicator() {
            const typingIndicator = document.getElementById("typing-indicator");
            if (typingIndicator) {
                typingIndicator.remove();
            }
        }
    </script>
</body>
</html>