document.addEventListener("DOMContentLoaded", function() {
// Adding an event listener to the chat link
var chatLink = document.getElementById("openChatLink");
if (chatLink) {
chatLink.addEventListener("click", function(event) {
event.preventDefault(); // Prevent the default link behavior
initializeChat();
// Call the function to initialize chat
});
}
function initializeChat() {
// Initialize the Botpress WebChat with the required parameters
window.botpressWebChat.init({
'botId': '...',
'hostUrl': 'https://cdn.botpress.cloud/webchat/v0',
'messagingUrl': 'https://messaging.botpress.cloud',
'clientId': '...',
/USE YOUR BOT Integration -> Webchat -> Configurable/
});
// Opens up the Chatbot by default
// This lets users start chatting with the Chatbot without needing to click any buttons or menus.
window.botpressWebChat.onEvent(
function (event) {
if (event.type === 'LIFECYCLE.LOADED') {
window.botpressWebChat.sendEvent({ type: 'show' })
}
// HIDE YOUR CUSTOM ICON WHEN THE CHAT IS OPEN
if (event.type === 'UI.OPENED') {
chatLink.classList.remove("loading");
chatLink.classList.add("hidden");
}
// SHOW YOUR CUSTOM ICON WHEN THE CHAT IS CLOSED
if (event.type === 'UI.CLOSED') {
chatLink.classList.remove("hidden");
chatLinkMessageNumber.classList.remove("hidden");
chatSpeechBubble.classList.remove("hidden");
}
}
},
['LIFECYCLE.LOADED', 'LIFECYCLE.READY', 'TRIGGER', 'UI.OPENED', 'UI.CLOSED']
);
}
});