Skip to content

Instantly share code, notes, and snippets.

@KixPanganiban
Last active July 7, 2022 01:04
Show Gist options
  • Save KixPanganiban/8816a5666b6f117bd582d6f152d79dd8 to your computer and use it in GitHub Desktop.
Save KixPanganiban/8816a5666b6f117bd582d6f152d79dd8 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Ada-Gorgias Sample</title>
<style>
.show-button {
display: block !important;
}
#chat-button {
display: none;
}
#ada-button-frame {
display: none;
}
</style>
</head>
<body>
<h1>Custom handoff from Ada to Gorgias sample</h1>
<p>Remember to update your Ada bot handle and add your live chat platform's embed script before testing.</p>
<!-- The following sample scripts should be placed just before the closing body tag or loaded after the main live chat elements are available. -->
<script>
(function () {
let gorgiasButton, adaButton;
function setLastChatOpened(name) {
try {
sessionStorage.setItem("lastChatOpened", name);
} catch {}
}
function switchChatWidgets() {
GorgiasChat.on("widget:closed", function (data) {
try {
gorgiasButton.classList.remove("show-button");
adaButton.classList.add("show-button");
} catch {}
setLastChatOpened("ada");
});
try {
gorgiasButton.classList.add("show-button");
adaButton.classList.remove("show-button");
} catch {}
}
function checkLastChatOpened() {
let lastChatOpened;
try {
lastChatOpened = sessionStorage.getItem("lastChatOpened");
} catch {
lastChatOpened = 'ada'
}
if (lastChatOpened === "gorgias") {
switchChatWidgets();
} else {
GorgiasChat.close();
try {
adaButton.classList.add("show-button");
} catch {}
}
}
function formatMessage(msg) {
const msgArray = msg.split(/(?=SUMMARY: |USER: |BOT: )/);
// If the chat transcript setting is reversed in the builder Dashboard (i.e. sorted from newest to oldest), the slice() params may need to be updated.
if (msg.length >= 4000) {
return `LAST FEW CHAT INTERACTIONS:\n\n${msgArray.slice(-6).join("")}`;
}
return msg;
}
window.adaSettings = {
adaReadyCallback: () => {
adaButton = document.querySelector("#ada-button-frame");
GorgiasChat.init().then(function () {
gorgiasButton = document.querySelector("#chat-button");
checkLastChatOpened();
});
},
eventCallbacks: {
custom_handoff: (event) => {
if (window.GorgiasChat && GorgiasChat.hasOwnProperty("captureUserEmail")) {
// The following 2 lines are optional. They are needed to set the email we save from Ada in Gorgias Chat
const userEmail = event.user_data.global.email;
GorgiasChat.captureUserEmail(userEmail);
switchChatWidgets();
GorgiasChat.open();
setLastChatOpened("gorgias");
const chatTranscript = event.chatter_transcript;
const message = formatMessage(chatTranscript);
GorgiasChat.sendMessage(message);
adaEmbed.toggle();
}
},
},
};
})();
</script>
<script
id="__ada"
data-handle="ada-example"
src="https://static.ada.support/embed2.js"
></script>
<!-- Replace this line with your Gorgias Chat Widget Javascript snippet -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment