// Variable to keep track of whether or not the chat window is open. The chat bubble is only shown when the chat window is closed
let isChatOpen = false
window.botpressWebChat.onEvent(
function (event) {
if (event.type === 'LIFECYCLE.LOADED') {
window.botpressWebChat.sendEvent({ type: 'hide' })
}
else if (event.type === 'LIFECYCLE.READY') {
// Send a custom trigger to the chatbot after a 3 sec timer
setTimeout(() => {
window.botpressWebChat.sendPayload({
type: 'trigger',
payload: {},
})
console.log('Payload triggered 3 seconds after page fully loaded')
}, 3000);
} else if (event.type === 'TRIGGER') {
// This section is to catch events sent from the chatbot
if (!isChatOpen && event.value && event.value.msg) {
// Display the chat bubble if the chat window is closed and a valid message is received from the chatbot
document.getElementById('msgDiv').style = "display: block";
document.getElementById('msgDiv').innerText = event.value.msg;
}
} else if (event.type === 'UI.OPENED') {
// Hides the chat bubble if the chat window has been opened and updates the boolean variable
isChatOpen = true
document.getElementById('msgDiv').style = "display: none";
} else if (event.type === 'UI.CLOSED') {
// Updates the chat bubble if the chat window is closed. This allows the chat bubble to be displayed again
// if the chatbot sends a new event
isChatOpen = false
}
},
// The list of events that we want to handle
['LIFECYCLE.LOADED', 'LIFECYCLE.READY', 'TRIGGER', 'UI.OPENED', 'UI.CLOSED']
)