Widget da web com exemplo ao vivo
<html>
<head>
<title>Exemplo de eventos do Chatlayer</title>
<meta charset="UTF-8" />
</head>
<body>
<button onclick="initChatlayer()">Inicializar Chatlayer</button>
<button onclick="destroyChatlayer()">Destruir Chatlayer</button>
<br />
<br />
<button onclick="openChatlayer()">Abrir widget do Chatlayer</button>
<button onclick="closeChatlayer()">Fechar widget do Chatlayer</button>
<script>
/**
* Este exemplo mostra a destruição de um webwidget da Chatlayer
* ao acionar o plugin JSON Builder no fluxo.
*
* A caixa de diálogo da ação JSON Builder foi configurada da seguinte forma:
* https://minio.dev.chatlayer.ai/public/JSONBuilder.png
*
*/
let chatlayerInstance = null;
const initChatlayer = () => {
if (chatlayerInstance) return;
console.log("Inicializando o widget da Chatlayer ...");
chatlayerInstance = chatlayer({ lang: "en" });
// Os tipos de evento são "event", "open" e "close"
chatlayerInstance.on("open", event => {
console.log('ABERTO');
});
chatlayerInstance.on("close", event => {
console.log('FECHADO')
});
chatlayerInstance.on("event", event => {
// Observe o conteúdo do event aqui: a chave "action" com o valor "destroy"
// vem diretamente da ação configurada do JSON Builder.
const isChatlayerDestroyEvent = event.action === "destroy";
if (isChatlayerDestroyEvent) {
destroyChatlayer();
}
});
};
const destroyChatlayer = () => {
if (!chatlayerInstance) return;
console.log("Destruindo o widget da Chatlayer ...");
// Para destruir o widget, ele precisa ser aberto primeiro
chatlayerInstance.open();
chatlayerInstance.destroy();
chatlayerInstance = null;
};
const openChatlayer = () => {
if (!chatlayerInstance) {
console.log("Por favor, inicialize a Chatlayer primeiro!");
return;
}
chatlayerInstance.open();
};
const closeChatlayer = () => {
if (!chatlayerInstance) {
console.log("Por favor, inicialize a Chatlayer primeiro!");
return;
}
chatlayerInstance.close();
};
</script>
<!-- Observe a ausência da prop onload no seguinte script tag. -->
<!-- Para inicializar automaticamente o webwidget, você adicionaria a prop: -->
<!-- onload="initChatlayer()" -->
<script
src="https://chatbox.staging.chatlayer.ai/sdk/5ecbcf1514c3dc4942f05f96"
async
></script>
</body>
</html>
Atualizado
Isto foi útil?