-
-
Save renanteixeira/c2d625a949618e7d8bcfa5edce2e8f67 to your computer and use it in GitHub Desktop.
Script Customizado WeniChat para o Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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