Update script.js
Browse files
script.js
CHANGED
|
@@ -316,29 +316,31 @@ function sendMessage() {
|
|
| 316 |
const language = data.language || 'en';
|
| 317 |
|
| 318 |
// Extract mantra if present in the response
|
| 319 |
-
const
|
| 320 |
let formattedResponse = botResponse; // Initialize formatted response
|
| 321 |
let category = ""; // Initialize category variable
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
// Remove mantra from the botResponse to avoid displaying it twice
|
| 330 |
-
formattedResponse = botResponse.replace(/\[\[(.*?)\]\]/s, '').trim();
|
| 331 |
-
}
|
| 332 |
|
| 333 |
-
//
|
| 334 |
const responseDetails = formatBotResponse(formattedResponse);
|
| 335 |
formattedResponse = responseDetails.formattedResponse;
|
| 336 |
category = responseDetails.category;
|
| 337 |
|
| 338 |
-
// Append the
|
| 339 |
appendMessage(category, 'bot-message');
|
| 340 |
appendMessage(formattedResponse, 'bot-message');
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
// Speak the bot's response (Text-to-Speech)
|
| 343 |
speakText(formattedResponse, language);
|
| 344 |
|
|
@@ -380,19 +382,21 @@ function sendMessage() {
|
|
| 380 |
|
| 381 |
// Manta Block Function
|
| 382 |
|
| 383 |
-
// Function to display the mantra block with the mantra
|
| 384 |
-
function displayMantra(
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
| 396 |
}
|
| 397 |
|
| 398 |
// Function to decrease the repeat count
|
|
@@ -419,7 +423,7 @@ function speakMantra(mantraText, repeatCount) {
|
|
| 419 |
utterance.lang = 'sa-IN'; // Sanskrit
|
| 420 |
utterance.rate = 0.8; // Slow speech for mantras
|
| 421 |
utterance.pitch = 1.0; // Natural pitch
|
| 422 |
-
|
| 423 |
utterance.onend = function() {
|
| 424 |
if (repeatCount > 1) {
|
| 425 |
repeatCount--;
|
|
|
|
| 316 |
const language = data.language || 'en';
|
| 317 |
|
| 318 |
// Extract mantra if present in the response
|
| 319 |
+
const mantraMatches = [...botResponse.matchAll(/\[\[(.*?)\]\]/g)]; // Find all occurrences of [[mantra]]
|
| 320 |
let formattedResponse = botResponse; // Initialize formatted response
|
| 321 |
let category = ""; // Initialize category variable
|
| 322 |
+
let allMantras = []; // Array to hold all mantra texts
|
| 323 |
|
| 324 |
+
mantraMatches.forEach(match => {
|
| 325 |
+
allMantras.push(match[1].trim()); // Collect all mantra texts
|
| 326 |
+
// Remove each mantra from the botResponse to avoid displaying it twice
|
| 327 |
+
formattedResponse = formattedResponse.replace(match[0], '').trim();
|
| 328 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
+
// Append the remaining bot response without the mantras
|
| 331 |
const responseDetails = formatBotResponse(formattedResponse);
|
| 332 |
formattedResponse = responseDetails.formattedResponse;
|
| 333 |
category = responseDetails.category;
|
| 334 |
|
| 335 |
+
// Append the bot response
|
| 336 |
appendMessage(category, 'bot-message');
|
| 337 |
appendMessage(formattedResponse, 'bot-message');
|
| 338 |
|
| 339 |
+
// Display all mantras in the mantra block
|
| 340 |
+
if (allMantras.length > 0) {
|
| 341 |
+
displayMantra(allMantras); // Pass the array of mantras
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
// Speak the bot's response (Text-to-Speech)
|
| 345 |
speakText(formattedResponse, language);
|
| 346 |
|
|
|
|
| 382 |
|
| 383 |
// Manta Block Function
|
| 384 |
|
| 385 |
+
// Function to display the mantra block with the mantra texts
|
| 386 |
+
function displayMantra(mantraTexts) {
|
| 387 |
+
let mantraBlock = document.getElementById('mantra-block');
|
| 388 |
+
let mantraTextElem = document.getElementById('mantraText');
|
| 389 |
+
|
| 390 |
+
// Join all mantras into a single string separated by line breaks or any preferred format
|
| 391 |
+
mantraTextElem.textContent = mantraTexts.join('\n');
|
| 392 |
+
|
| 393 |
+
// Show the mantra block
|
| 394 |
+
mantraBlock.style.display = 'block';
|
| 395 |
|
| 396 |
+
// By default, play all mantras when shown
|
| 397 |
+
mantraTexts.forEach(mantra => {
|
| 398 |
+
speakMantra(mantra, 1); // Speak each mantra once
|
| 399 |
+
});
|
| 400 |
}
|
| 401 |
|
| 402 |
// Function to decrease the repeat count
|
|
|
|
| 423 |
utterance.lang = 'sa-IN'; // Sanskrit
|
| 424 |
utterance.rate = 0.8; // Slow speech for mantras
|
| 425 |
utterance.pitch = 1.0; // Natural pitch
|
| 426 |
+
|
| 427 |
utterance.onend = function() {
|
| 428 |
if (repeatCount > 1) {
|
| 429 |
repeatCount--;
|