呼叫连接或挂断上的 Twilio 获取呼叫详细信息
Posted
技术标签:
【中文标题】呼叫连接或挂断上的 Twilio 获取呼叫详细信息【英文标题】:Twilio on Call-Connect or Hangup get call details 【发布时间】:2019-07-14 12:54:26 【问题描述】:我正在使用带有 Twilio 的 ASP.NET MVC Web 调用
这是我的连接函数
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】:这就是我最终处理它的方式。
/* 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);
);
添加CallLog函数
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);
【讨论】:
以上是关于呼叫连接或挂断上的 Twilio 获取呼叫详细信息的主要内容,如果未能解决你的问题,请参考以下文章
执行 python 程序以从 twilio 服务器获取呼叫者参加电话会议时出错