Twilio在Call-Connect或Hangup上获取呼叫详细信息

时间:2019-02-20 18:30:02

标签: c# asp.net-mvc twilio

我正在通过Twilio使用ASP.NET MVC Web调用

这是我的Connect功能

function callCustomer(phoneNumber) {
    updateCallStatus("Calling " + phoneNumber + "...");
    phoneNumber = phoneNumber.replace(/ /g, '');
    var params = { To: phoneNumber };
    Twilio.Device.connect(params);
}

这是我的挂断功能

function hangUp() {
    Twilio.Device.disconnectAll();
}

这是我的TwiML Bin

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial callerId="++516xxx9999" record="record-from-answer">{{To}}</Dial>
</Response>

我正在使用Twilio客户端v1.6

//media.twiliocdn.com/sdk/js/client/v1.6/twilio.min.js

我想在连接到呼叫或挂断电话时收集每个呼叫的完整信息,例如呼叫时长,呼叫Sid,记录Sid,呼叫至等。然后,利用这些信息,我想在我的应用程序中实现播放录制的呼叫。

我相信一种处理方式是在“语音和传真”下设置“呼叫状态更改”并接收所有参数。

1 个答案:

答案 0 :(得分:0)

这就是我最终处理它的方式。

/* Callback for when a call ends */
Twilio.Device.disconnect(function (connection) {
    console.log(connection);
    // Disable the hangup button and enable the call buttons
    hangUpButton.prop("disabled", true);
    callCustomerButtons.prop("disabled", false);
    callSupportButton.prop("disabled", false);

    updateCallStatus("Ready");
    addCallLog(connection.parameters.CallSid);
});

addCallLog函数

function addCallLog(id) {
    var type = "";
    var entityId = Number($("#Id").val());
    $.ajax({
        url: "/Phone/AddCallLog?callId=" + id,
        type: "POST",
     contentType: "application/json;",
        success: function (data) {
            // Handle Success Event
        },
        error: function (data) {
           // Handle Error Event
        }
    });
}

控制器方法

[HttpPost]
        public ActionResult AddCallLog(string callId,string type,int entityId)
        {
            TwilioClient.Init(_callSetting.Twilio.AccountSid, _callSetting.Twilio.Authtoken);


            var records = CallResource.Read(parentCallSid: callId).ToList();
            if (records.Any())
            {

                var callResource= records[0];
                var parentRecord = CallResource.Fetch(pathSid: callId);
                if (callResource.Status.ToString().Equals("completed", StringComparison.OrdinalIgnoreCase))
                {
                    CallRecord callRecord = new CallRecord
                    {
                        EntityKey = entityId,
                        EntityType = type,
                        CallDateTimeUtc = callResource.DateCreated ?? DateTime.UtcNow,
                        CallSId = callResource.Sid,
                        ParentCallSId = callResource.ParentCallSid,
                        CalledById = _operatingUser.Id,
                        DurationInSeconds = parentRecord==null? Convert.ToDouble(callResource.Duration): Convert.ToDouble(parentRecord.Duration),
                        ToPhone = callResource.To,
                        CompanyId = _operatingUser.CompanyId
                    };
                     var callRecordResult=   _callRecordService.Add(callRecord);
                    var recording = RecordingResource.Read(callSid: callId).ToList();
                    if (!recording.Any()) return Json(true);
                    foreach (RecordingResource recordingResource in recording)
                    {
                        using (var client = new WebClient())
                        {
                            var url =
                                "https://api.twilio.com" + recordingResource.Uri.Replace(".json", ".mp3");
                            var content = client.DownloadData(url);

                            CallRecordMedia callRecordMedia = new CallRecordMedia
                            {
                                CallRecordId = callRecordResult.Id,
                                ContentType = "audio/mpeg",
                                RecordingSId = recordingResource.Sid,
                                RecordingCallSId = recordingResource.CallSid,
                                FileType = "mp3",
                                Data = content,
                                Price = Convert.ToDouble(recordingResource.Price),
                                PriceUnit = recordingResource.PriceUnit,
                                DurationInSeconds = Convert.ToDouble(recordingResource.Duration)
                            };
                            _callRecordService.AddCallRecording(callRecordMedia);
                        }
                    }
                }
            }
           return Json(true);
        }