Skip to content

Commit

Permalink
use Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
mirhmousavi committed May 3, 2024
1 parent 90333f7 commit 66ad2d5
Showing 1 changed file with 112 additions and 71 deletions.
183 changes: 112 additions & 71 deletions main.js
Original file line number Diff line number Diff line change
@@ -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:
*
* <html>
* <head>
* ...
* </head>
* <body>
* ...
* <script>
* var showLiveChatButton = true;
* var showVideoChatButton = true;
* var scaleDroneChannelId = 'fygLrCqVZUYQZL6';
* var nicHomeURL = 'https://home-b99.nice-incontact.com';
* var nicBusNumber = '1809119';
* var nicChatPOC = '1605d121-489c-4df4-83b1-334dbeb0a781u';
* var clusterNiC = 'b99';
* var videoSignalerURL = "https://home-b99.nice-incontact.com/inContact/Manage/Scripts/Spawn.aspx?scriptName=Surfly_Signaler&bus_no=1534130&scriptId=64719900&skill_no=1197136&Guid=602b102c74-c935-4539-9f80-0827ab18918";
* var chatSignalerURL = "https://home-b99.nice-incontact.com/inContact/Manage/Scripts/Spawn.aspx?scriptName=Surfly_Signaler&bus_no=1534130&scriptId=64719900&skill_no=1197136&Guid=602b102c74-c935-4539-9f80-0827ab18918";
* var surflySettings = { ... };
* // The following variables can be passed as part of the surflySettings object. we keep supporting them for backward compatibility
* var surflyWidgetKey = '134f5fd2ac8842c0a1cd6062818yd2ac';
* var surflyModalTitle = 'Start Videochat';
* var surflyModalBody = 'By clicking Accept, an agent will automatically join you in a videochat session. You will be prompted to enable/disable your camera and mute/unmute your microphone. You can access your audio settings using the gear icon. You can end the videochat at any time, by clicking ✕ in the menu or by closing this tab in your browser.';
* ​ </script>
* <script type="text/javascript" src="https://nic.surfly.com/surfly-nic-script.js"></script>
* ...
* ​</body>
* </html>
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:
<html>
<head>
...
</head>
<body>
...
<script>
var showLiveChatButton = true;
var showVideoChatButton = true;
var scaleDroneChannelId = 'fygLrCqVZUYQZL6';
var nicHomeURL = 'https://home-b99.nice-incontact.com';
var nicBusNumber = '1809119';
var nicChatPOC = '1605d121-489c-4df4-83b1-334dbeb0a781u';
var clusterNiC = 'b99';
var videoSignalerURL = "https://home-b99.nice-incontact.com/inContact/Manage/Scripts/Spawn.aspx?scriptName=Surfly_Signaler&bus_no=1534130&scriptId=64719900&skill_no=1197136&Guid=602b102c74-c935-4539-9f80-0827ab18918";
var chatSignalerURL = "https://home-b99.nice-incontact.com/inContact/Manage/Scripts/Spawn.aspx?scriptName=Surfly_Signaler&bus_no=1534130&scriptId=64719900&skill_no=1197136&Guid=602b102c74-c935-4539-9f80-0827ab18918";
var surflySettings = { ... };
// The following variables can be passed as part of the surflySettings object. we keep supporting them for backward compatibility
var surflyWidgetKey = '134f5fd2ac8842c0a1cd6062818yd2ac';
var surflyModalTitle = 'Start Videochat';
var surflyModalBody = 'By clicking Accept, an agent will automatically join you in a videochat session. You will be prompted to enable/disable your camera and mute/unmute your microphone. You can access your audio settings using the gear icon. You can end the videochat at any time, by clicking ✕ in the menu or by closing this tab in your browser.';
</script>
<script type="text/javascript" src="https://nic.surfly.com/surfly-nic-script.js"></script>
...
​</body>
</html>
*/

var nicHomeURL = nicHomeURL || "https://home-" + clusterNiC + ".nice-incontact.com";
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 => {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -313,32 +352,34 @@ 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();
}
}
});
}


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');
}
}

0 comments on commit 66ad2d5

Please sign in to comment.