Skip to content

Instantly share code, notes, and snippets.

@renanteixeira
Last active March 5, 2024 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renanteixeira/c2d625a949618e7d8bcfa5edce2e8f67 to your computer and use it in GitHub Desktop.
Save renanteixeira/c2d625a949618e7d8bcfa5edce2e8f67 to your computer and use it in GitHub Desktop.
Script Customizado WeniChat para o Magento
<script type="text/javascript">
require([
"https://storage.googleapis.com/push-webchat/wwc-latest.js",
], function (WebChat) {
window.WebChat = WebChat;
document.WebChat = WebChat;
let wwc = document.createElement("div");
wwc.id = "webchat";
document.body.appendChild(wwc);
let title = document.title;
const config = {
"Encontre seu colchão": {
startFullScreen: true,
title: title,
},
"Encontre uma Loja": {
startFullScreen: true,
title: title,
},
"Loja Simmons": {
startFullScreen: false,
embedded: false,
title: title,
},
"default": {
startFullScreen: false,
title: "Simmons",
}
};
const payloads = {
"Encontre seu colchão": "Colchão ideal",
"Encontre uma Loja": "Encontrar uma loja",
"Loja Simmons": title.split(" - ")[1],
"default": "Olá"
};
let wcC = {
selector: "#webchat",
channelUuid: '14a29f67-9e1e-4122-8201-d4627acbb2fd',
host: 'https://flows.weni.ai',
socketUrl: "https://websocket.weni.ai",
inputTextFieldHint: "Digite sua mensagem...",
showFullScreenButton: true,
customizeWidget: {
"titleColor": "#fff",
"headerBackgroundColor": "#000",
"userMessageBubbleColor": "#000",
"quickRepliesBackgroundColor": "#001848",
"quickRepliesFontColor": "#fff",
"quickRepliesBorderColor": "#001848",
"launcherColor": "#000"
},
...config[title] || config["default"],
customMessageDelay: message => 1 * 1000
};
WebChat.default.init(wcC);
window.onload = function () {
let attempts = 0;
let maxAttempts = 20; // Stop after 20 attempts
let intervalId = setInterval(function () {
console.log("Checking if connected");
if (localStorage.chat_session) {
let session = JSON.parse(localStorage.chat_session);
if (session.params.connected) {
clearInterval(intervalId); // Stop checking once connected
if (!session.params.initialized && !session.params.initPayloadSent) {
console.log("Not initialized and initPayload not sent");
if ((title === "Encontre seu colchão") || (title === "Encontre uma Loja")) {
WebChat.toggle();
} else {
WebChat.open();
}
WebChat.send(payloads[title] || payloads["default"]);
} else if (session.params.initialized && !session.params.initPayloadSent) {
console.log("Initialized but initPayload not sent");
WebChat.send(payloads[title] || payloads["default"]);
if (!WebChat.isOpen()) {
WebChat.open();
}
} else {
console.log("Already initialized and initPayload sent");
WebChat.show();
}
}
}
attempts++;
if (attempts >= maxAttempts) {
clearInterval(intervalId); // Stop checking after max attempts
console.log("Stopped checking connection after max attempts");
}
}, 500); // Check every 500 milliseconds
};
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment