diff --git a/main.js b/main.js index 99e165f..dcd54b7 100644 --- a/main.js +++ b/main.js @@ -1,49 +1,54 @@ /** - * 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 + - nicBusNumber + - nicHomeURL + + # If your BU is not configured for the new signaling flow, you should provide the following variables + - videoSignalerURL + - chatSignalerURL + + Example in HTML pages: + + + + ... + + + ... + + + ... + + */ var nicHomeURL = nicHomeURL || "https://home-" + clusterNiC + ".nice-incontact.com"; @@ -168,17 +173,25 @@ function createSurflySession(contactId, inviteType) { var surflyMetadata = { "name": "Customer" }; + 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'); + if(!window.chatSignalerURL) { + sendFollowerLink( + session.followerLink, + inviteType, + localStorage.getItem(nicBusNumber + "-uniquePageId"), + scaleDroneChannelId + ); + } else { + signalContact(contactId, session.follower_link, session.pin, inviteType); + } var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; - console.log('Session Pin: ' + surflySessionPin); + console.log('Session Pin: ' + session.pin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); @@ -192,12 +205,19 @@ function createSurflySession(contactId, inviteType) { videochat_start_fullscreen: true }); regularSession.on("session_started", function(session, event) { - var surflySessionPin = session.pin; - var surflyFollowerLink = session.followerLink; - signalContact(contactId, surflyFollowerLink, surflySessionPin, 'videochat'); + if(!window.chatSignalerURL) { + sendFollowerLink( + session.followerLink, + inviteType, + localStorage.getItem(nicBusNumber + "-uniquePageId"), + scaleDroneChannelId + ); + } else { + signalContact(contactId, session.follower_link, session.pin, inviteType); + } var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; - console.log('Session Pin: ' + surflySessionPin); + console.log('Session Pin: ' + session.pin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); @@ -207,6 +227,24 @@ function createSurflySession(contactId, inviteType) { } } +function sendFollowerLink(followerLink, type, uniquePageId, channelId) { + fetch( + "https://nic.surf.ly/follower_link", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + channel_id: channelId, + room: type, + unique_page_id: uniquePageId, + follower_link: followerLink, + }), + } + ); +} + function loadSurfly() { var settings = { @@ -257,8 +295,8 @@ function loadSurfly() { } = message; if (data.type == 'ElevateToCobrowse' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'cobrowse'); - + const inviteType = 'cobrowse'; + createSurflySession(data.contactId, inviteType); console.log('Co-browsing session requested'); console.log("Message ID: " + id); console.log("Timestamp: " + timestamp); @@ -294,7 +332,7 @@ function loadSurfly() { } = message; if (data.type == 'ElevateToVideo' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'videochat'); + createSurflySession(data.contactId, inviteType); console.log('Videochat session requested'); console.log("Message ID: " + id); @@ -341,4 +379,4 @@ function initializeChatNiC() { }); console.log('Initializing NiC'); -} \ No newline at end of file +}