function cleanWordEncoding(text) {
if (!text) return '';
return text
.replace(/•|•|\u2022/g, '*') // bullet symbols
.replace(/–|—|–|—/g, '-') // dashes
.replace(/’|‘|’|‘/g, "'") // single quotes
.replace(/“|â€|“|”/g, '"') // double quotes
.replace(/[^\x00-\x7F]/g, ''); // remove other weird characters if needed
}
(function () {
function getUserId() {
let userId = localStorage.getItem('opulent_user_id');
if (!userId) {
userId = 'user_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
localStorage.setItem('opulent_user_id', userId);
}
return userId;
}
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function validatePhone(phone) {
const re = /^[0-9]{10,15}$/;
return re.test(phone);
}
function appendMessage(role, htmlContent) {
const chatBody = document.getElementById("chat-body");
const bubble = document.createElement("div");
bubble.style.margin = "10px 0";
bubble.style.padding = "12px 16px";
bubble.style.maxWidth = "80%";
bubble.style.borderRadius = "12px";
bubble.style.lineHeight = "1.5";
bubble.style.wordWrap = "break-word";
bubble.style.backgroundColor = role === "user" ? "#e0f7ff" : "#f0f0f0";
bubble.style.alignSelf = role === "user" ? "flex-end" : "flex-start";
bubble.style.color = "#111";
bubble.style.fontSize = "14px";
bubble.style.marginLeft = role === "user" ? "auto" : "0";
bubble.innerHTML = htmlContent;
chatBody.appendChild(bubble);
// Instead of scrolling to bottom, scroll to the new message if it's from bot
if (role === "bot") {
bubble.scrollIntoView({ behavior: "smooth", block: "start" });
}
}
function formatBotResponse(text) {
if (text.includes('* ')) {
let paragraphs = text.split(/\n\n|\r\n\r\n/);
let formattedText = '';
paragraphs.forEach((paragraph, index) => {
if (!paragraph.trim()) return;
if (!paragraph.trim().startsWith('*') && index === 0) {
formattedText += `
`;
}
else if (cleanWordEncoding(paragraph).includes('* ')) {
let cleanedParagraph = cleanWordEncoding(paragraph);
let lines = cleanedParagraph.split(/\n|\r\n/);
formattedText += '';
lines.forEach(line => {
if (line.trim().startsWith('*')) {
let content = line.trim().substring(1).trim();
content = content.replace(/\*\*\*([^*]+)\*\*\*/g, '$1');
content = content.replace(/\*\*([^*]+)\*\*/g, '$1');
content = content.replace(/\*([^*]+)\*/g, '$1');
formattedText += `- ${content}
`;
} else if (line.trim()) {
formattedText += `${line}
`;
}
});
formattedText += '
';
}
else {
formattedText += `${paragraph}
`;
}
});
return formattedText;
}
if (text.includes('•') || text.includes('')) {
return text;
}
let paragraphs = text.split(/\n\n|\r\n\r\n/);
let formattedText = '';
paragraphs.forEach((paragraph, index) => {
if (!paragraph.trim()) return;
if (paragraph.length < 80 && !paragraph.endsWith('.') && paragraphs.length > 1) {
formattedText += ``;
}
else if (paragraph.length < 40) {
formattedText += `${paragraph}
`;
}
else {
let sentences = paragraph.split(/(?<=\.|\?|\!) (?=[A-Z])/);
if (sentences.length > 1) {
formattedText += '';
sentences.forEach(sentence => {
if (sentence.trim()) {
formattedText += `- ${sentence.trim()}
`;
}
});
formattedText += '
';
} else {
formattedText += `${paragraph}
`;
}
}
});
return formattedText;
}
function sendChatMessage(message) {
const userId = getUserId(); // Retrieve the user ID from localStorage
const clientId = localStorage.getItem('client_id'); // Retrieve the client ID from localStorage (or wherever it's stored)
const chatBody = document.getElementById("chat-body");
appendMessage("user", message);
appendMessage("bot", `
`);
fetch("https://chat.pyot.co.in/api/askdocsopulent/", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
query: message,
user_id: userId,
client_id: clientId
})
})
.then(response => {
if (!response.ok) throw new Error("Network response was not ok");
return response.json();
})
.then(data => {
console.log("Success:", data);
chatBody.removeChild(chatBody.lastChild);
const isHtmlResponse = data.answer && (
data.answer.includes('