var chatBoxIsShowed = false; async function initializeChatbot() { document.querySelector('.ChatBoxButon').classList.remove("hide"); document.querySelector('.ChatBoxButon').classList.add("show"); // Specifies style options to customize the Web Chat canvas. // Please visit https://microsoft.github.io/BotFramework-WebChat for customization samples. const styleOptions = { // Hide upload button. hideUploadButton: true, //backgroundColor: 'white', botAvatarInitials: 'BT', accent: '#00809d', botAvatarBackgroundColor: "#FFFFFF", botAvatarImage: '/documents/d/guest/favicon-ico' }; // Specifies the token endpoint URL. // To get this value, visit Copilot Studio > Settings > Channels > Mobile app page. const tokenEndpointURL = new URL('https://default1e870ba80c624525b25d8e489ea8b3.f8.environment.api.powerplatform.com/powervirtualagents/botsbyschema/cra87_ooredooCopilotFr/directline/token?api-version=2022-03-01-preview'); // Specifies the language the copilot and Web Chat should display in: // - (Recommended) To match the page language, set it to document.documentElement.lang // - To use current user language, set it to navigator.language with a fallback language // - To use another language, set it to supported Unicode locale // Setting page language is highly recommended. // When page language is set, browsers will use native font for the respective language. // const locale = document.documentElement.lang || 'en'; // Uses language specified in element and fallback to English (United States). const locale = 'fr'; // const locale = navigator.language || 'ja-JP'; // Uses user preferred language and fallback to Japanese. // const locale = 'zh-HAnt'; // Always use Chinese (Traditional). const apiVersion = tokenEndpointURL.searchParams.get('api-version'); const [directLineURL, token] = await Promise.all([ fetch(new URL('/powervirtualagents/regionalchannelsettings?api-version=' + apiVersion, tokenEndpointURL)) .then(response => { if (!response.ok) { throw new Error('Failed to retrieve regional channel settings.'); } return response.json(); }) .then(({ channelUrlsById: { directline } }) => directline), fetch(tokenEndpointURL) .then(response => { if (!response.ok) { throw new Error('Failed to retrieve Direct Line token.'); } return response.json(); }) .then(({ token }) => token) ]); // The "token" variable is the credentials for accessing the current conversation. // To maintain conversation across page navigation, save and reuse the token. // The token could have access to sensitive information about the user. // It must be treated like user password. const directLine = WebChat.createDirectLine({ domain: new URL('v3/directline', directLineURL), token }); // Sends "startConversation" event when the connection is established. const subscription = directLine.connectionStatus$.subscribe({ next(value) { if (value === 2) { directLine .postActivity({ localTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone, locale, name: 'startConversation', type: 'event' }) .subscribe(); // Only send the event once, unsubscribe after the event is sent. subscription.unsubscribe(); } } }); WebChat.renderWebChat({ directLine, locale, styleOptions }, document.getElementById('webchat')); const fullPath = window.location.pathname; // Split the URL into segments const segments = fullPath.split('/'); if (segments.length < 4) { let isPopup = getCookieByName("policy-popup"); if (isPopup) { const toggleChatBoxbtn2 = document.querySelector('.ChatBoxButon'); toggleChatBoxbtn2.click(); } } } // Your existing chat toggle function function toggleChatBox() { const chatBox = document.querySelector('.chatBox'); if (chatBoxIsShowed) { chatBox.style.display = 'none'; } else { chatBox.style.display = 'block'; } chatBoxIsShowed = !chatBoxIsShowed; } const toggleChatBoxbtn = document.querySelector('.ChatBoxButon'); const toggleChatBoxClosebtn = document.querySelector('.close-chat'); toggleChatBoxbtn.addEventListener("click", toggleChatBox); toggleChatBoxClosebtn.addEventListener("click", toggleChatBox); function getCookieByName(cname) { let name = cname + "="; let ca = document.cookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; }