Twilio客户端CallSid在JavaScript中得到更改

时间:2018-11-23 19:27:33

标签: javascript twilio twilio-click-to-call

我已经使用Twilio客户端JavaScript SDK在Salesforce中实现了呼叫中心。我试图将通话记录信息保存在Salesforce中,并且使用connection.parameters.CallSid来在触发记录回叫时标识正确的记录。但是,有时我的客户端CallSid会自动更改为其他格式,因此录音回调函数无法找到Salesforce最终记录来使用RecordingUrl对其进行更新。有没有人曾经历过此过程或感谢任何指导。

下面是我的JavaScript代码。更具体地说,在startCall函数console.log中正确打印CallSid,但是当转到saveLog函数时,它具有不同的值和不同的格式,因此保存的记录具有错误的值

<script type="text/javascript">
    Twilio.Device.setup("{! token }");
    var callerSid;  //  hold the Twilio generated CallSid unique to this call
    var parentId;   //  hold the parent record being called in order to associate as the parent of task being logged for the call
    var newTaskId;  // hold the id of newly created task

    //  function to fire when click 2 dial executes
    var startCall = function (payload) {
        sforce.opencti.setSoftphonePanelVisibility({visible: true});  //pop up CTI softphone
        parentId = payload.recordId;    //  the record that the phone number being called belongs to
        var cleanednumber = cleanFormatting(payload.number);             
        params = {"PhoneNumber": cleanednumber};
        var connection = Twilio.Device.connect(params);
        callerSid = connection.parameters;  //  track the unique Twilio CallSid
        console.log('clk2dial : ', callerSid.CallSid); **// here it logs correcly as CAc57d05994cd69498e0353a5f4b07f2dc**
        setTimeout(function(){
            saveLog();  //  save the call information in a Task record
            }, 2000
        );
    };

    //OpenCTI!!
    sforce.opencti.enableClickToDial();
    sforce.opencti.onClickToDial({listener : startCall});   //  click 2 dial           

    function cleanFormatting(number) { 
        //changes a SFDC formatted US number, which would be 415-555-1212  into a twilio understanble number 4155551212      
        return number.replace(' ','').replace('-','').replace('(','').replace(')','').replace('+','');
    }

    //  save the call information in a Task record
    function saveLog() {
        var keyPrefix;
        var taskToSave;
        console.log('callerSid.CallSid : ', callerSid.CallSid); **// surprisingly here it logs as TJSce253eb4-c2a0-47f3-957f-8178e95162aa**
        if(parentId != null) {
            keyPrefix = parentId.slice(0,3);
        }
        if(keyPrefix != null && keyPrefix == '003') {
            taskToSave = {WhoId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};
        } else {
            taskToSave = {WhatId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};   
        }

        sforce.opencti.saveLog({value:taskToSave, callback: saveLogCallBack});        

    }

    //  call back function for saving the call information in a Task record
    var saveLogCallBack = function(response) {
        if(response.success) {
            newTaskId = response.returnValue.recordId;
            console.log('save success! : ', newTaskId);
        } else {
            console.error('Error saving : ', response.errors);   
        }
    }

    </script>

1 个答案:

答案 0 :(得分:0)

在解决此问题时回答我自己的问题。我为Twilio.Device.connect注册了一个函数,并在回调函数中检索了CallSid。除此之外,我还更新了click 2拨盘功能,如下所示。但是,我在Twilio文档中找不到这种方法,欢迎提出任何意见。

//  function to fire when click 2 dial executes
var startCall = function (payload) {
    sforce.opencti.setSoftphonePanelVisibility({visible: true});  //pop up CTI softphone
    parentId = payload.recordId;    //  the record that the phone number being called belongs to

    var cleanednumber = cleanFormatting(payload.number);             
    params = {"PhoneNumber": cleanednumber};
    Twilio.Device.connect(params);
};

//OpenCTI!!
sforce.opencti.enableClickToDial();
sforce.opencti.onClickToDial({listener : startCall});   //  click 2 dial        

// registered a function for Twilio Device connect
Twilio.Device.connect(function(response) {
    callSid = response.parameters;  //  track the unique Twilio CallSid
    // nothing change in save function so not posting again
    saveLog();  //  save the call information in a Task record
});