This is an old revision of the document!
In progress
<%@language=JScript%>
<%
/*
Author: Gerard O'Rourke
Date: 29 Auguest 2019
To Do: Timeouts, Error handling when Site returns incorrect data or no data, put code into functions.
Test 200 OK but with wrong URL (incorrect response - not able to parse)
Test with incorrect URL and non 200 OK response (404, 503 etc)
Test Timeout options
/*
/*
MIT License
Copyright (c) 2019 Gerard O'Rourke - purplepi.ie
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
try { //Start of Global Try
// CCE & ECE Server Details
var ccehostname = 'ucce-hds-a.lab2.purplepi.ie';
var ecehostname = 'ucce-ece-db-12.lab2.purplepi.ie';
var cceuser = '[email protected]';
var ccepass = 'myPa$$w0rd!';
// Default Values if unable to read valid values from BusinessHours Name
var defaultProactiveChatTimer = '0';
var defaultMinAgentCount = '0';
var defaultMaxQueueDepth = '0';
var defaultMaxWaitTime = '120';
var debuglevel = 0; // 0 = off, 1 = minimum, 2 = all
// Declare Global Variables used in both businessHours and ECE APIs
var myProactiveChatTimer
var myMaxQueueDepth
var myMaxWaitTime
var myMinAgentCount
// Query String - Retrieve dyanmic values for the Business Hours and ECE API IDs.
var myQuerySring = Request.QueryString;
var myChatEntryId = Request.QueryString("chatEntryId");
var myBusinessHoursId= Request.QueryString("businessHoursId");
// CCE & ECE URLs
var businessHoursUrl = 'https://' + ccehostname + '/unifiedconfig/config/businesshour/' + myBusinessHoursId;
var liveSessionStatusUrl = 'http://' + ecehostname + '/system/egain/chat/entrypoint/liveSessionStatus/' + myChatEntryId;
var capacityUrl = 'http://' + ecehostname + '/system/egain/chat/entrypoint/capacity/' + myChatEntryId;
// Initial JS variable values
var chatStatus = 'closed'; //Defaults value until overwritten
var chatStatusReason = 'out of service'; //Defaults value until overwritten
var chatTimer = '0'; //Defaults value until overwritten
Response.ContentType="application/javascript"; // Set Content Type
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus QueryString: " + Request.QueryString + "');");
}
chatstatus = businessHoursAPI() // BusinessHours API - returns 'open' or 'closed'
if (chatstatus == 'open'){
chatstatus = liveSessionStatusAPI() // ECE liveSessionStatus API - returns 'open' or 'busy' or if unable to read - 'closed' with chatStatusReason 'out of service'
}
if (chatstatus == 'open' && myMinAgentCount > 0){
chatstatus = capacityAPI() // ECE Capacity API - returns 'open' or 'busy' or if unable to read - 'closed' with chatStatusReason 'out of service'
}
Response.Write("\nvar chatStatus = '" + chatStatus + "';");
Response.Write("\nvar chatStatusReason = '" + chatStatusReason + "';");
Response.Write("\nvar chatTimer = '" + chatTimer + "';");
function businessHoursAPI(){
try {
var srvXmlHttp
srvXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0");
srvXmlHttp.setOption(2,13056); //Ignore SSL Certs
srvXmlHttp.open ("GET", businessHoursUrl, false, cceuser, ccepass);
srvXmlHttp.send();
var httpstatus = srvXmlHttp.status;
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus CCE API HTTP Status: " + httpstatus + "')");
}
if (httpstatus != 200){
chatStatus = 'closed';
chatStatusReason = 'out of service';
return chatStatus;
}
chatStatusReason = ''; //Clear 'out of service' value
var businessHours = srvXmlHttp.responseXML;
var businessHoursName = businessHours.selectSingleNode("//businessHour/name");
var runTimeStatus = businessHours.selectSingleNode("//businessHour/runTimeStatus");
var configuredStatus = businessHours.selectSingleNode("//businessHour/configuredStatus/status");
if (configuredStatus.text > 0) {
var reasonText = businessHours.selectSingleNode("//businessHour/configuredStatus/statusReason/reasonText");
}
//Parse Business Hours Name to retrieve the Proactive Chat Timer, Min Agent Count, Max Queue Depth & Max Wait Timeouts
var businessHoursNameArray = businessHoursName.text.split("_");
var myBusinessHoursName = businessHoursNameArray[0];
myProactiveChatTimer = businessHoursNameArray[1];
myMinAgentCount = businessHoursNameArray[2];
myMaxQueueDepth = businessHoursNameArray[3];
myMaxWaitTime = businessHoursNameArray[4];
// Check if Valid Values from BusinessHour - if not - overwrite with the default values.
if (isNaN(myProactiveChatTimer)){
myProactiveChatTimer = defaultProactiveChatTimer;
}
if (isNaN(myMinAgentCount)){
myMinAgentCount = defaultMinAgentCount;
}
if (isNaN(myMaxQueueDepth)){
myMaxQueueDepth = defaultMaxQueueDepth;
}
if (isNaN(myMaxWaitTime)){
myMaxWaitTime = defaultMaxWaitTime;
}
if ((runTimeStatus.text == 0 || runTimeStatus.text == 2) && configuredStatus.text > 0) {
chatStatusReason = reasonText.text;
}
if (runTimeStatus.text == 1 || runTimeStatus.text == 3){
chatStatus = 'open';
}
if (debuglevel > 1){
Response.Write("\nconsole.log('chatStatus BusinessHoursName: " + businessHoursName.text + "')");
if (configuredStatus.text > 0) {
Response.Write("\nconsole.log('chatStatus reasonText: " + reasonText.text + "')");
}
Response.Write("\nconsole.log('chatStatus myBusinessHoursName: " + myBusinessHoursName + "')");
Response.Write("\nconsole.log('chatStatus myProactiveChatTimer: " + myProactiveChatTimer + "')");
Response.Write("\nconsole.log('chatStatus myMinAgentCount: " + myMinAgentCount + "')");
Response.Write("\nconsole.log('chatStatus myMaxQueueDepth: " + myMaxQueueDepth + "')");
Response.Write("\nconsole.log('chatStatus myMaxWaitTime: " + myMaxWaitTime + "')");
Response.Write("\nconsole.log('chatStatus runTimeStatus: " + runTimeStatus.text + "')");
}
return chatStatus;
} //Try
catch(err) {
chatStatus = 'closed';
chatStatusReason = 'out of service';
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus Error BusinessHours API')");
}
return chatStatus;
} //Catch
} //Function
function liveSessionStatusAPI(){
try {
var srvXmlHttp
srvXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0");
srvXmlHttp.setOption(2,13056); //Ignore SSL Certs
srvXmlHttp.open ("GET", liveSessionStatusUrl, false);
srvXmlHttp.send();
var httpstatus = srvXmlHttp.status;
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus ECE LiveSession API HTTP Status: " + httpstatus + "')");
}
if (httpstatus != 200){
chatStatus = 'closed';
chatStatusReason = 'out of service';
return chatStatus;
}
var liveSessionStatus = srvXmlHttp.responseXML;
var waitTime = liveSessionStatus.selectSingleNode("//ns2:waitTime");
var queueDepth = liveSessionStatus.selectSingleNode("//ns2:queueDepth");
if (parseInt(queueDepth.text,10) > myMaxQueueDepth){
chatStatus = 'busy';
chatStatusReason = 'MaxQueueDepth';
}
if (parseInt(waitTime.text,10) > myMaxWaitTime){
chatStatus = 'busy';
chatStatusReason = 'MaxWaitTime';
}
if (debuglevel > 1){
Response.Write("\nconsole.log('chatStatus waitTime: " + waitTime.text + "')");
Response.Write("\nconsole.log('chatStatus queueDepth: " + queueDepth.text + "')");
}
return chatStatus;
} //Try
catch(err) {
chatStatus = 'closed';
chatStatusReason = 'out of service';
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus Error Live Session API')");
}
return chatStatus;
} //Catch
} //Function
function capacityAPI(){
try {
srvXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0");
srvXmlHttp.setOption(2,13056); //Ignore SSL Certs
srvXmlHttp.open ("GET", capacityUrl, false);
srvXmlHttp.send();
var httpstatus = srvXmlHttp.status;
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus ECE Capacity API HTTP Status: " + httpstatus + "')");
}
if (httpstatus != 200){
chatStatus = 'closed';
chatStatusReason = 'out of service';
return chatStatus;
}
var capacity = srvXmlHttp.responseXML;
var count = capacity.selectSingleNode("//ns2:count");
if (parseInt(count.text,10) < myMinAgentCount){
chatStatus = 'busy';
chatStatusReason = 'myMinAgentCount';
}
if (debuglevel > 1){
Response.Write("\nconsole.log('chatStatus agentCount: " + count.text + "')");
}
return chatStatus;
} // Try
catch(err) {
chatStatus = 'closed';
chatStatusReason = 'out of service';
if (debuglevel > 0){
Response.Write("\nconsole.log('chatStatus Error Capacity API')");
}
return chatStatus;
} //Catch
} //Function
} //End of Global Try
catch(err) {
chatStatus = 'closed';
chatStatusReason = 'out of service';
chatTimer=0;
Response.Write("\nvar chatStatus = '" + chatStatus + "';");
Response.Write("\nvar chatStatusReason = '" + chatStatusReason + "';");
Response.Write("\nvar chatTimer = '" + chatTimer + "';");
Response.Write("\nconsole.log('chatStatus Global Error')");
}
%>