<%@language = JScript %>

    <%

    /*
    Author: Gerard O'Rourke - Purplepi.ie
    Date: 02 September 2019
    Version 1.0
    /*

    /*
    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.
    */

    /*
	Location of File: 
	Example: D:\ECE\eService\templates\chat\businessHours\
	Example URL: http://ucce-ece-db-12.lab2.purplepi.ie/system/templates/chat/businessHours/chatStatus.asp?businessHoursId=5001&chatEntryId=1001
	*/	


    try { //Start of Global Try

// ******************************************************************************************** //
// ******************* You should review and change the BELOW variables  ********************** //
// ******************************************************************************************** //

        // Variables 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]'; //create a PCCE User Administrator dedicated for this role.
        var ccepass = 'myPa$$w0rd!';
        var debuglevel = 1; // 0 = disabled, 1 = minimum, 2 = all - recommend to set to 1			
        var SXH_OPTION_2_VALUE = 13056; // SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS -> 'SXH_OPTION_2_VALUE = 13056;' to ignore all SSL errors
        var resolveTimeout = 3000; // DNS Resolve Timeout (ms)
        var connectTimeout = 3000; // Value (ms) is applied to establishing a communication socket with the target server
        var sendTimeout = 3000; // The value applies to sending an individual packet of request data (if any) on the communication socket to the target server. (ms)
        var receiveTimeout = 3000; // The value applies to receiving a packet of response data from the target server (ms)

        // Default Values if unable to read valid values from BusinessHours Name
        var defaultProactiveChatTimer = '0'; // value here is set in ms, e.g. for a 10 second Proactive Chat, enter '10000'
        var defaultMinAgentCount = '0';
        var defaultMaxQueueDepth = '0';
        var defaultMaxWaitTime = '120';

// ******************************************************************************************** //
// ******************* You should review and change the ABOVE variables  ********************** //
// ******************************************************************************************** //



        // 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 = '" + myProactiveChatTimer + "';");

		Response.Write("\nconsole.log('chatStatus chatStatus: " + chatStatus + "');");
		Response.Write("\nconsole.log('chatStatus chatStatusReason: " + chatStatusReason + "');");
		Response.Write("\nconsole.log('chatStatus chatTimer: " + myProactiveChatTimer + "');");


// ******************************************************************************************** //
// ******************************** Functions ************************************************* //
// ******************************************************************************************** //

        function businessHoursAPI() {
            try {
                var srvXmlHttp
                srvXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0");
                srvXmlHttp.setOption(2, SXH_OPTION_2_VALUE); //Ignore SSL Certs
                srvXmlHttp.setTimeouts(resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
                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';
					myProactiveChatTimer = '0';
                    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];

                myProactiveChatTimer = myProactiveChatTimer*1000;	//Convert ChatTimer from seconds to milliseconds

                // 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';
				myProactiveChatTimer = '0';

                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, SXH_OPTION_2_VALUE); //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';
					myProactiveChatTimer = '0';
                    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';
					myProactiveChatTimer = '0';
                }

                if (parseInt(waitTime.text, 10) > myMaxWaitTime) {
                    chatStatus = 'busy';
                    chatStatusReason = 'MaxWaitTime';
					myProactiveChatTimer = '0';
                }

                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';
				myProactiveChatTimer = '0';
				
                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, SXH_OPTION_2_VALUE); //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';
					myProactiveChatTimer = '0';
                }

                if (debuglevel > 1) {
                    Response.Write("\nconsole.log('chatStatus agentCount: " + count.text + "')");
                }

                return chatStatus;
            } // Try
            catch (err) {
                chatStatus = 'closed';
                chatStatusReason = 'out of service';
				chatTimer = '0';
              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')");
}

%>
  • inprogress.txt
  • Last modified: 2019/09/04 22:24
  • by gerardorourke