0

We have a requirement to end a live chat automatically if an agent does not accept or decline the request within 5 minutes. Following this thread : Live agent chat idle session 10 mins , I have tried implementing the idea given in this thread. I have added a cometd listener to a vf page which gets the chat key related to a chat request on the agent's queue.I have added this vf page on a vf tab and placed it in service console. I am using sforce.console.chat.endChat(chatKey:String, (optional)callback:Function) to end the chat after a certain amount of time. Right now I am trying with 1 min delay.Here is my code:

<apex:page controller="SampleConsoleVFC" >
    
    <apex:includeScript value="/support/console/49.0/integration.js"/>
    <apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js" />
    <apex:includeScript value="{!URLFOR($Resource.cometd, 'cometd/cometd.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.cometd, 'jquery/jquery.cometd.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.cometd, 'json2.js')}"/>
    
    <script type="text/javascript">
         $.cometd.configure({
        url: window.location.protocol + '//' + window.location.hostname + '/cometd/41.0/',
        requestHeaders: {
            Authorization: 'OAuth {!$Api.Session_ID}'
        },
        appendMessageTypeToURL : false
    });
    
    console.log('before handshake');
    
    // disable websockets
    $.cometd.websocketEnabled = false; 
    
    $.cometd.handshake();
    $.cometd.subscribe('/topic/TimeCheck', function(message) {
    });
    console.log('before addListener');  
    
    $.cometd.addListener('/topic/TimeCheck', function(message) {
        var id = message.data.sobject.Id;
        console.log(id);
        var reqTime = message.data.sobject.RequestTime ;
        console.log(reqTime);
        Visualforce.remoting.Manager.invokeAction(
                         '{!$RemoteAction.SampleConsoleVFC.getChatKey}',
                         id,
                         function (result, event) {
                             if (event.status) {
                                 setTimeout(function(){ 
                                    console.log(result);
                                     sforce.console.chat.endChat(result, endSuccess); }, 60000);
                            
                             
                             } else if (event.type === 'exception') {
                                 alert('Exception...');
                             } else {
                                 alert(event.message);
                             }
                         },
                         { escape: true }
                     );
    });
    function endSuccess(result) {
            //Report whether ending the chat was succesful
            if (result.success == true) {
                alert('Ending the chat was successful');
            } else {
                alert('Ending the chat was not successful');
            }
        };
    </script>
</apex:page>

I have checked the logs, I am getting the chat key but after passing the chat key into the end chat function,I am getting the alert below:

Ending the chat was not successful

Could anyone please help me out with the possible reasons of this function not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.