File size: 7,418 Bytes
01e760b
 
 
 
 
a72e660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01e760b
a72e660
 
 
 
 
 
 
 
01e760b
a72e660
 
 
01e760b
a72e660
 
 
 
 
 
 
01e760b
a72e660
 
 
 
 
 
 
 
 
 
01e760b
 
 
a72e660
 
 
 
01e760b
a72e660
 
 
 
 
 
01e760b
 
 
 
 
 
 
 
 
a72e660
 
 
 
 
01e760b
a72e660
 
 
 
 
 
 
 
 
 
 
01e760b
 
a72e660
 
 
01e760b
a72e660
01e760b
a72e660
 
 
 
 
 
 
 
01e760b
a72e660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01e760b
a72e660
01e760b
a72e660
01e760b
a72e660
01e760b
a72e660
01e760b
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SmartlyQ Chatbot Demo</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .demo-section {
            margin: 40px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background: white;
        }
        pre {
            background: #f5f5f5;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
        }
        .upload-area {
            border: 2px dashed #ddd;
            padding: 20px;
            text-align: center;
            margin: 20px 0;
            border-radius: 8px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        input[type="text"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin: 5px;
        }
        .success-message {
            color: #28a745;
            margin: 10px 0;
            font-weight: bold;
        }
        .error-message {
            color: #dc3545;
            margin: 10px 0;
            font-weight: bold;
        }
        #demo-container {
            margin-top: 20px;
        }
        .bot-link {
            display: block;
            margin: 10px 0;
            color: #007bff;
            text-decoration: none;
        }
        .bot-link:hover {
            text-decoration: underline;
        }
        .copy-button {
            background: #6c757d;
            font-size: 0.9em;
        }
        .copy-button:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <h1>SmartlyQ Chatbot Demo</h1>
    
    <div class="demo-section">
        <h2>1. Create New Bot</h2>
        <p>Upload your documents (PDFs, text files) to create a new bot:</p>
        <div class="upload-area">
            <input type="file" id="files" multiple>
        </div>
        <button onclick="uploadDocuments()">Create Bot</button>
        <div id="upload-result" class="success-message"></div>
    </div>

    <div class="demo-section">
        <h2>2. Test Your Bot</h2>
        <p>Enter your botid below to test the chatbot:</p>
        <div style="margin-bottom: 20px;">
            <input type="text" id="botid-input" placeholder="Enter your botid">
            <button onclick="loadChatbot()">Load Chatbot</button>
            <button onclick="openBotPage()" class="copy-button">Open Bot Page</button>
        </div>
        <div id="bot-link"></div>
        <div id="demo-container"></div>
    </div>

    <div class="demo-section">
        <h2>3. Embed Code</h2>
        <p>To embed this chatbot on your website, add the following code to your HTML:</p>
        <pre id="embed-code">
<!-- Add your botid to see the embed code -->
        </pre>
    </div>

    <script>
        // Keep track of current bot ID
        let currentBotId = null;

        async function uploadDocuments() {
            const files = document.getElementById('files').files;
            if (files.length === 0) {
                alert('Please select at least one file');
                return;
            }

            const formData = new FormData();
            for (let file of files) {
                formData.append('files', file);
            }

            try {
                const response = await fetch('/upload-documents', {
                    method: 'POST',
                    body: formData
                });
                
                if (!response.ok) {
                    throw new Error('Upload failed');
                }

                const data = await response.json();
                const uploadResult = document.getElementById('upload-result');
                uploadResult.textContent = `Success! Your Bot ID: ${data.botid}`;
                uploadResult.className = 'success-message';
                
                // Automatically fill the test bot ID
                document.getElementById('botid-input').value = data.botid;
                currentBotId = data.botid;
                
                // Update embed code and bot link
                updateEmbedCode(data.botid);
                updateBotLink(data.botid);
            } catch (error) {
                console.error('Error:', error);
                const uploadResult = document.getElementById('upload-result');
                uploadResult.textContent = 'Error uploading files. Please try again.';
                uploadResult.className = 'error-message';
            }
        }

        function loadChatbot() {
            const botid = document.getElementById('botid-input').value.trim();
            if (!botid) {
                alert('Please enter a botid');
                return;
            }

            currentBotId = botid;

            // Update embed code and bot link
            updateEmbedCode(botid);
            updateBotLink(botid);

            // Clear previous chatbot if exists
            const container = document.getElementById('demo-container');
            container.innerHTML = '';

            // Create new container for this instance
            const chatbotContainer = document.createElement('div');
            chatbotContainer.id = 'smartlyq-chatbot-container';
            container.appendChild(chatbotContainer);

            // Create and append script
            const script = document.createElement('script');
            script.src = `/static/chatbot.js?botid=${botid}`;
            container.appendChild(script);
        }

        function updateBotLink(botid) {
            const botLink = document.getElementById('bot-link');
            const botUrl = `${window.location.origin}/bot.html?botid=${botid}`;
            botLink.innerHTML = `
                <p>Direct bot link:</p>
                <a href="${botUrl}" target="_blank" class="bot-link">${botUrl}</a>
                <button onclick="copyToClipboard('${botUrl}')" class="copy-button">Copy Link</button>
            `;
        }

        function openBotPage() {
            const botid = document.getElementById('botid-input').value.trim();
            if (!botid) {
                alert('Please enter a botid');
                return;
            }
            window.open(`/bot.html?botid=${botid}`, '_blank');
        }

        function copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                alert('Link copied to clipboard!');
            }).catch(err => {
                console.error('Failed to copy:', err);
            });
        }

        async function updateEmbedCode(botid) {
            try {
                const response = await fetch(`/embed-code?botid=${botid}`);
                const data = await response.json();
                document.getElementById('embed-code').textContent = data.embed_code;
            } catch (error) {
                console.error('Error fetching embed code:', error);
            }
        }
    </script>
</body>
</html>