quick-musician-29561
01/10/2024, 8:39 AMquick-musician-29561
01/10/2024, 8:41 AMquick-musician-29561
01/10/2024, 8:42 AMquick-musician-29561
01/10/2024, 8:42 AMquick-musician-29561
01/10/2024, 8:45 AMquick-musician-29561
01/10/2024, 8:48 AMquick-musician-29561
01/10/2024, 9:07 AMjs
document.addEventListener('DOMContentLoaded', function() {
function initBotpressChat(config) {
const commonSettings = {
"composerPlaceholder": "Chat with bot",
"botConversationDescription": "This chatbot was built surprisingly fast with Botpress",
"hostUrl": "https://cdn.botpress.cloud/webchat/v1",
"messagingUrl": "https://messaging.botpress.cloud",
"lazySocket": true,
"themeName": "prism",
"frontendVersion": "v1",
"useSessionStorage": true,
"enableConversationDeletion": true,
"showPoweredBy": true,
"theme": "prism",
"themeColor": "#2563eb"
};
here you change the settings based on which chatbot has been chosen (botId, clientId, webhookId, stylesheet)
js
const settings = {
...commonSettings,
"botId": config === 'BLUE' ? "c956769e-929c-4a70-b3da-677e25003c34" : "e35fdc00-d48c-4c45-b4f4-5bf9c4fc91dc",
"clientId": config === 'BLUE' ? "c956769e-929c-4a70-b3da-677e25003c34" : "e35fdc00-d48c-4c45-b4f4-5bf9c4fc91dc",
"webhookId": config === 'BLUE' ? "c3f3b133-c3e0-4e59-9935-d816c0d9247a" : "70b174fb-405f-4e08-beb8-0cca4105012d",
"stylesheet": config === 'BLUE' ? "https://webchat-styler-css.botpress.app/prod/code/70692789-ba07-44f8-bdea-fcc4cd08ce7b/v72248/style.css" : "https://webchat-styler-css.botpress.app/prod/code/70692789-ba07-44f8-bdea-fcc4cd08ce7b/v4032/style.css"
};
window.botpressWebChat.init(settings);
}
quick-musician-29561
01/10/2024, 9:14 AMjs
function toggleChatConfig() {
const currentConfig = localStorage.getItem('chatConfig');
const newConfig = currentConfig === 'BLUE' ? 'RED' : 'BLUE';
localStorage.setItem('chatConfig', newConfig);
window.location.reload();
}
loadScript function loads the new Botpress config scripts for the chatbot user has chosen
js
function loadScript(src, onLoad) {
const script = document.createElement('script');
script.src = src;
script.onload = onLoad;
document.body.appendChild(script);
}
this part gets the correct config scripts from localStorage when button is clicked and gives them to the functions above
js
const chatConfig = localStorage.getItem('chatConfig') || 'BLUE';
loadScript("https://cdn.botpress.cloud/webchat/v1/inject.js", () => initBotpressChat(chatConfig));
document.getElementById('toggleChat').addEventListener('click', toggleChatConfig);
});
quick-musician-29561
01/10/2024, 9:18 AMhtml
<button id="toggleChat">Switch bot</button>
and also a script tag where you can place the JavaScript code (or a JS file like in this case)
html
<script src="script.js"></script>
quick-musician-29561
01/10/2024, 9:19 AMwide-grass-59382
01/11/2024, 1:51 AMbulky-gigabyte-59033
01/11/2024, 10:40 AMglamorous-guitar-39983
03/24/2024, 2:57 PMglamorous-guitar-39983
03/24/2024, 3:03 PMbrash-arm-25586
03/24/2024, 3:14 PM