|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Copilot Studio Intelligent Suggestions Version 1.0</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + font-family: Arial, sans-serif; |
| 10 | + background-color: #f4f4f4; |
| 11 | + margin: 0; |
| 12 | + padding: 0; |
| 13 | + height: 100vh; |
| 14 | + display: flex; |
| 15 | + background: url('https://github.com/paragdessai/PVAClickAnalytics/blob/main/Chatbotbackground.png?raw=true') no-repeat center center; |
| 16 | + background-size: cover; |
| 17 | + justify-content: center; |
| 18 | + align-items: center; |
| 19 | + } |
| 20 | + |
| 21 | + /* Chat bubble style */ |
| 22 | + #chatBubble { |
| 23 | + position: fixed; |
| 24 | + bottom: 30px; |
| 25 | + right: 30px; |
| 26 | + background-color: #0078D4; |
| 27 | + color: white; |
| 28 | + padding: 15px; |
| 29 | + border-radius: 50%; |
| 30 | + cursor: pointer; |
| 31 | + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); |
| 32 | + } |
| 33 | + |
| 34 | + /* Bot container hidden initially */ |
| 35 | + #botContainer { |
| 36 | + position: fixed; |
| 37 | + display: none; |
| 38 | + justify-content: center; |
| 39 | + align-items: center; |
| 40 | + height: 100%; |
| 41 | + width: 100%; |
| 42 | + z-index: 999; |
| 43 | + background: rgba(0, 0, 0, 0.5); /* Semi-transparent background */ |
| 44 | + } |
| 45 | + |
| 46 | + /* Chat window style */ |
| 47 | + .chat-window { |
| 48 | + background-color: white; |
| 49 | + border-radius: 8px; |
| 50 | + width: 600px; |
| 51 | + height: 600px; |
| 52 | + display: flex; |
| 53 | + flex-direction: column; |
| 54 | + position: relative; |
| 55 | + padding: 10px; |
| 56 | + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
| 57 | + } |
| 58 | + |
| 59 | + /* Close button inside chat */ |
| 60 | + #closeBot { |
| 61 | + position: absolute; |
| 62 | + top: 10px; |
| 63 | + right: 50px; |
| 64 | + background-color: #ff4d4f; |
| 65 | + color: white; |
| 66 | + border: none; |
| 67 | + border-radius: 50%; |
| 68 | + width: 30px; |
| 69 | + height: 30px; |
| 70 | + cursor: pointer; |
| 71 | + text-align: center; |
| 72 | + font-weight: bold; |
| 73 | + line-height: 30px; |
| 74 | + } |
| 75 | + |
| 76 | + /* Refresh button inside chat */ |
| 77 | + #refreshBot { |
| 78 | + position: absolute; |
| 79 | + top: 10px; |
| 80 | + right: 10px; |
| 81 | + background-color: #4CAF50; /* Green */ |
| 82 | + color: white; |
| 83 | + border: none; |
| 84 | + border-radius: 50%; |
| 85 | + width: 30px; |
| 86 | + height: 30px; |
| 87 | + cursor: pointer; |
| 88 | + text-align: center; |
| 89 | + font-weight: bold; |
| 90 | + line-height: 30px; |
| 91 | + } |
| 92 | + |
| 93 | + #webchat { |
| 94 | + flex: 1; |
| 95 | + border: 1px solid #ddd; |
| 96 | + border-radius: 4px; |
| 97 | + background-color: rgba(255, 255, 255, 0.9); |
| 98 | + margin-top: 20px; |
| 99 | + overflow: hidden; |
| 100 | + position: relative; /* Make it a positioning context */ |
| 101 | + } |
| 102 | + |
| 103 | + /* Suggestions list Styling */ |
| 104 | + .suggestion-list { |
| 105 | + position: relative; |
| 106 | + background-color: white; |
| 107 | + border: 1px solid #ddd; |
| 108 | + border-radius: 4px; |
| 109 | + width: calc(100% - 20px); |
| 110 | + max-width: 560px; |
| 111 | + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
| 112 | + z-index: 1000; |
| 113 | + padding: 0; |
| 114 | + display: none; |
| 115 | + margin-top: 5px; |
| 116 | + } |
| 117 | + |
| 118 | + .suggestion-item { |
| 119 | + padding: 10px; |
| 120 | + cursor: pointer; |
| 121 | + list-style-type: none; |
| 122 | + border-bottom: 1px solid #ddd; |
| 123 | + } |
| 124 | + |
| 125 | + .suggestion-item:hover { |
| 126 | + background-color: #f4f4f4; |
| 127 | + } |
| 128 | + |
| 129 | + .suggestion-item:last-child { |
| 130 | + border-bottom: none; |
| 131 | + } |
| 132 | + </style> |
| 133 | +</head> |
| 134 | +<body> |
| 135 | + |
| 136 | + <!-- Chat bubble to open the bot --> |
| 137 | + <div id="chatBubble">💬</div> |
| 138 | + |
| 139 | + <!-- Bot container with chat window --> |
| 140 | + <div id="botContainer"> |
| 141 | + <div class="chat-window"> |
| 142 | + <button id="closeBot">X</button> |
| 143 | + <button id="refreshBot">🔄</button> <!-- Refresh button --> |
| 144 | + <div id="webchat" role="main"></div> |
| 145 | + <ul id="suggestions" class="suggestion-list"></ul> <!-- Suggestions now positioned relative to the input --> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + |
| 149 | + <!-- Include BotFramework WebChat --> |
| 150 | + <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script> |
| 151 | + |
| 152 | + <script> |
| 153 | + let directLine; |
| 154 | + |
| 155 | + // Function to retrieve the Direct Line token |
| 156 | + async function getDirectLineToken() { |
| 157 | + try { |
| 158 | + const response = await fetch('Insert your directline token API'); |
| 159 | + if (!response.ok) throw new Error('Network response was not ok'); |
| 160 | + const data = await response.json(); |
| 161 | + return data.token; // Assuming the token is in the 'token' field |
| 162 | + } catch (error) { |
| 163 | + console.error('Error fetching token:', error); |
| 164 | + return null; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Direct Line connection initialization |
| 169 | + async function initializeDirectLine() { |
| 170 | + const token = await getDirectLineToken(); // Fetch the token |
| 171 | + if (token) { |
| 172 | + directLine = window.WebChat.createDirectLine({ |
| 173 | + token: token // Use the fetched token |
| 174 | + }); |
| 175 | + |
| 176 | + // Render Web Chat in bot container |
| 177 | + window.WebChat.renderWebChat( |
| 178 | + { |
| 179 | + directLine: directLine, |
| 180 | + userID: 'user1', |
| 181 | + username: 'User', |
| 182 | + locale: 'en-US' |
| 183 | + }, |
| 184 | + document.getElementById('webchat') |
| 185 | + ); |
| 186 | + |
| 187 | + // Listen for user input in Web Chat (via Direct Line connection) |
| 188 | + directLine.activity$.subscribe(activity => { |
| 189 | + if (activity.type === 'message' && activity.from.id === 'user1') { |
| 190 | + handleInput(activity.text); // Handle input to show suggestions |
| 191 | + } |
| 192 | + }); |
| 193 | + } else { |
| 194 | + console.error('Failed to initialize Direct Line due to missing token'); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // Initialize topics array |
| 199 | + let topics = []; |
| 200 | + |
| 201 | + // List of values to exclude from API Response |
| 202 | + const excludedTopics = [ |
| 203 | + 'Reset Conversation', |
| 204 | + 'Thank you', |
| 205 | + 'Sign in', |
| 206 | + 'Sign in ', |
| 207 | + 'Conversation Start', |
| 208 | + 'End of Conversation', |
| 209 | + 'On Error', |
| 210 | + 'Fallback', |
| 211 | + 'Conversational boosting', |
| 212 | + 'Multiple Topics Matched', |
| 213 | + 'Escalate', |
| 214 | + 'Start Over' |
| 215 | + ]; |
| 216 | + |
| 217 | + const suggestionsList = document.getElementById('suggestions'); |
| 218 | + |
| 219 | + // Function to fetch Suggestion using API. The API needs to return an Array of Suggestions |
| 220 | + async function fetchTopics() { |
| 221 | + try { |
| 222 | + const response = await fetch('https://prod-177.westus.logic.azure.com:443/workflows/7539cccafbaa45a3b139c8e83492aa44/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=1iEAjAT50LuQI_54hHJYult5P1ygpj9G3i2qxQsoOKI'); // Add your RestAPI here |
| 223 | + if (!response.ok) throw new Error('Network response was not ok'); |
| 224 | + const fetchedTopics = await response.json(); // Get the response data |
| 225 | + |
| 226 | + // Filter out excluded topics |
| 227 | + topics = fetchedTopics.filter(topic => !excludedTopics.includes(topic)); |
| 228 | + console.log('Topics fetched:', topics); |
| 229 | + } catch (error) { |
| 230 | + console.error('Error fetching topics:', error); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // Function to handle user typing and show suggestions |
| 235 | + function handleInput(input) { |
| 236 | + const userInput = input.toLowerCase(); |
| 237 | + suggestionsList.innerHTML = ''; // Clear previous suggestions |
| 238 | + |
| 239 | + if (userInput) { |
| 240 | + const filteredTopics = topics.filter(topic => topic.toLowerCase().includes(userInput)); |
| 241 | + |
| 242 | + // Show suggestions list only if there are filtered topics |
| 243 | + if (filteredTopics.length > 0) { |
| 244 | + filteredTopics.forEach(topic => { |
| 245 | + const suggestionItem = document.createElement('li'); |
| 246 | + suggestionItem.classList.add('suggestion-item'); |
| 247 | + suggestionItem.textContent = topic; |
| 248 | + |
| 249 | + // Handle click on suggestion |
| 250 | + suggestionItem.addEventListener('click', function() { |
| 251 | + if (directLine) { |
| 252 | + // Send the selected suggestion as a message to the bot |
| 253 | + directLine.postActivity({ |
| 254 | + from: { id: 'user1' }, // User ID |
| 255 | + type: 'message', |
| 256 | + text: topic // Send the selected topic |
| 257 | + }).subscribe( |
| 258 | + id => console.log('Message sent with ID:', id), |
| 259 | + error => console.error('Error sending message:', error) |
| 260 | + ); |
| 261 | + |
| 262 | + suggestionsList.style.display = 'none'; // Hide suggestions after selection |
| 263 | + } else { |
| 264 | + console.error('DirectLine is not initialized'); |
| 265 | + } |
| 266 | + }); |
| 267 | + |
| 268 | + suggestionsList.appendChild(suggestionItem); |
| 269 | + }); |
| 270 | + |
| 271 | + suggestionsList.style.display = 'block'; // Show suggestions list |
| 272 | + } else { |
| 273 | + suggestionsList.style.display = 'none'; // Hide if no suggestions |
| 274 | + } |
| 275 | + } else { |
| 276 | + suggestionsList.style.display = 'none'; // Hide if input is cleared |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + // Open bot on chat bubble click |
| 281 | + document.getElementById('chatBubble').addEventListener('click', async function() { |
| 282 | + const botContainer = document.getElementById('botContainer'); |
| 283 | + botContainer.style.display = 'flex'; // Show the bot container |
| 284 | + this.style.display = 'none'; // Hide chat bubble |
| 285 | + |
| 286 | + await fetchTopics(); // Fetch topics when the chat bubble is clicked |
| 287 | + initializeDirectLine(); // Initialize Direct Line after fetching topics |
| 288 | + }); |
| 289 | + |
| 290 | + // Close bot on 'X' button click |
| 291 | + document.getElementById('closeBot').addEventListener('click', function() { |
| 292 | + document.getElementById('botContainer').style.display = 'none'; // Hide bot container |
| 293 | + document.getElementById('chatBubble').style.display = 'block'; // Show chat bubble |
| 294 | + }); |
| 295 | + |
| 296 | + // Refresh page on refresh button click |
| 297 | + document.getElementById('refreshBot').addEventListener('click', function() { |
| 298 | + location.reload(); // Refresh the page |
| 299 | + }); |
| 300 | + |
| 301 | + // Display suggestions based on user typing in the web chat |
| 302 | + document.getElementById('webchat').addEventListener('input', function(event) { |
| 303 | + const userInput = event.target.value; |
| 304 | + handleInput(userInput); // Call handleInput function to show suggestions |
| 305 | + }); |
| 306 | + </script> |
| 307 | +</body> |
| 308 | +</html> |
0 commit comments