diff --git a/main.js b/main.js index 99e165f..a670677 100644 --- a/main.js +++ b/main.js @@ -1,49 +1,52 @@ /** - * Surfly NIC integration - * - * Make sure the following variables are initialized based on your configuration before importing this script - * showLiveChatButton - * showVideoChatButton - * scaleDroneChannelId - * nicHomeURL - * nicBusNumber - * nicChatPOC - * clusterNiC - * videoSignalerURL - * chatSignalerURL - * surflySettings - * surflyWidgetKey - * surflyModalTitle - * surflyModalBody - * - * Example in HTML pages: - * - * - * - * ... - * - * - * ... - * - * - * ... - * ​ - * + Surfly NIC integration + + Make sure the following variables are initialized based on your configuration before importing this script + showLiveChatButton + showVideoChatButton + scaleDroneChannelId + + surflySettings + surflyWidgetKey + surflyModalTitle + surflyModalBody + + nicChatPOC + clusterNiC + # If AdminAPI is configured for your account, you don't need to set the following variables + nicHomeURL + nicBusNumber + videoSignalerURL + chatSignalerURL + + Example in HTML pages: + + + + ... + + + ... + + + ... + ​ + */ var nicHomeURL = nicHomeURL || "https://home-" + clusterNiC + ".nice-incontact.com"; @@ -164,28 +167,28 @@ function createVideochatButton() { } -function createSurflySession(contactId, inviteType) { +function createSurflySession(contactId, inviteType, signal) { var surflyMetadata = { "name": "Customer" }; - if (inviteType == 'cobrowse') { + if (inviteType === 'cobrowse') { var regularSession = Surfly.session({ block_until_agent_joins: false }); regularSession.on("session_started", function(session, event) { var surflySessionPin = session.pin; var surflyFollowerLink = session.followerLink; - signalContact(contactId, surflyFollowerLink, surflySessionPin, 'cobrowse'); + signal && signalContact(contactId, surflyFollowerLink, surflySessionPin, 'cobrowse'); var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; console.log('Session Pin: ' + surflySessionPin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); - updateStudioScript(contactId, 'cobrowse'); + signal && updateStudioScript(contactId, 'cobrowse'); showVideoChatButton && createVideochatButton(); }).startLeader(null, surflyMetadata); - } else if (inviteType == 'videochat') { + } else if (inviteType === 'videochat') { var regularSession = Surfly.session({ block_until_agent_joins: false, videochat_autostart: true, @@ -194,14 +197,14 @@ function createSurflySession(contactId, inviteType) { regularSession.on("session_started", function(session, event) { var surflySessionPin = session.pin; var surflyFollowerLink = session.followerLink; - signalContact(contactId, surflyFollowerLink, surflySessionPin, 'videochat'); + signal && signalContact(contactId, surflyFollowerLink, surflySessionPin, 'videochat'); var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; console.log('Session Pin: ' + surflySessionPin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); - updateStudioScript(contactId, 'videochat'); + signal && updateStudioScript(contactId, 'videochat'); showVideoChatButton && createVideochatButton(); }).startLeader(null, surflyMetadata); } @@ -238,6 +241,42 @@ function loadSurfly() { } else { console.log("Connection has been opened"); + + const adminAPIRoom = drone.subscribe('adminapi'); + adminAPIRoom.on('open', error => { + if (error) { + return console.error(error); + } else { + console.log("Admin API room has been opened"); + } + }); + + adminAPIRoom.on('message', message => { + const nic_contact_id = message.data.nic_contact_id; + if (message.data.unique_page_id === getOrCreateUniqueClientID()) { + Surfly.on('session_created', function(session, event) { + fetch( + "https://nic.surf.ly/signal", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + cluster_nic: clusterNiC, + nic_contact_id: nic_contact_id, + follower_link: session.followerLink, + pin: session.pin, + }), + } + ).catch(error => { + console.error("Error:", error); + }); + }); + createSurflySession(nic_contact_id, message.data.type, false); + } + }); + + const cobrowseRoom = drone.subscribe('cobrowse'); cobrowseRoom.on('open', error => { @@ -256,14 +295,14 @@ function loadSurfly() { member } = message; - if (data.type == 'ElevateToCobrowse' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'cobrowse'); + if (data.type === 'ElevateToCobrowse' && data.bu === nicBusNumber && data.uniquePageId === getOrCreateUniqueClientID()) { + createSurflySession(data.contactId, 'cobrowse', true); console.log('Co-browsing session requested'); console.log("Message ID: " + id); console.log("Timestamp: " + timestamp); console.log(data); - } else if (data.type == 'sessionended' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { + } else if (data.type === 'sessionended' && data.bu === nicBusNumber && data.uniquePageId === getOrCreateUniqueClientID()) { Surfly.listSessions().forEach(function(session) { session.end(); @@ -293,8 +332,8 @@ function loadSurfly() { member } = message; - if (data.type == 'ElevateToVideo' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'videochat'); + if (data.type === 'ElevateToVideo' && data.bu === nicBusNumber && data.uniquePageId === getOrCreateUniqueClientID()) { + createSurflySession(data.contactId, 'videochat', true); console.log('Videochat session requested'); console.log("Message ID: " + id); @@ -313,18 +352,6 @@ function loadSurfly() { console.log("User has been reconnected"); }); - if (localStorage.getItem(nicBusNumber + "-uniquePageId")) { - console.log("random: " + localStorage.getItem(nicBusNumber + "-uniquePageId")); - } else { - let randomString = Math.random().toString(36).substring(7); - let dateNow = Date.now(); - let uniquePageId = randomString + "-" + dateNow; - - console.log("random: " + uniquePageId); - - localStorage.setItem(nicBusNumber + "-uniquePageId", uniquePageId); - } - showLiveChatButton && initializeChatNiC(); } } @@ -332,13 +359,27 @@ function loadSurfly() { } +function getOrCreateUniqueClientID() { + const uniqueClientIDKey = "uniquePageId"; + let uniqueClientID = localStorage.getItem(uniqueClientIDKey); + if (!uniqueClientID) { + let randomString = Math.random().toString(36).substring(7); + let dateNow = Date.now(); + uniqueClientID = randomString + "-" + dateNow; + localStorage.setItem(uniqueClientIDKey, uniqueClientID); + } + + return uniqueClientID; +} + + function initializeChatNiC() { icPatronChat.init({ serverHost: nicHomeURL, bus_no: nicBusNumber, poc: nicChatPOC, - params: [localStorage.getItem(nicBusNumber + "-uniquePageId")] + params: [getOrCreateUniqueClientID()] }); console.log('Initializing NiC'); -} \ No newline at end of file +}