为了通过 Twilio 从笔记本中调用电话号码,我创建了 ASP.NET-MVC 5.2 应用程序。
我可以调用一个号码并接听电话,但我不知道如何实现现场语音(能够通话)连接而不只是播放音乐。
我在 HomeController
中创建了一个 Action 方法:
public ActionResult Call(string to) {
client = new TwilioRestClient(Settings.AccountSid, Settings.AuthToken);
var result = client.InitiateOutboundCall(Settings.TwilioNumber, to, "http://twimlets.com/message?Message%5B0%5D=http://demo.kevinwhinnery.com/audio/zelda.mp3"); //it causes to play zelda theme when call is answered by callee
if (result.RestException != null) {
return new System.Web.Mvc.HttpStatusCodeResult(500, result.RestException.Message);
}
return Content("Call enroute!");
}
public ActionResult Index() {
return View();
}
此操作方法由 Ajax 调用调用。
当我从 Views\Home\Index.csthml
按下按钮时:
<form>
<p>Enter your mobile phone number:</p>
<input id="to" type="text"
placeholder="ex: +16518675309" />
<button>Send me a message</button>
</form>
调用下面的脚本,将电话号码从 <input id="to">
传递到 public ActionResult Call(string to)
中的操作方法 HomeController
:
$('form button').on('click', function(e) {
e.preventDefault();
// expect just a string of text back from the server
var url = '/call';
$.ajax(url, { //invokes call action method
method:'POST',
dataType:'text',
data:{
to:$('#to').val()//passes the number argument to the action method
},
success: function(data) {
showFlash(data);
},
error: function(jqxhr) {
alert('There was an error sending a request to the server');
}
})
});
这开始调用指定号码,即 48123456789
,其中 48
是国家代码。当被叫方接听电话时,将播放 zelda 主题。( http://twimlets.com/message?Message%5B0%5D=http://demo.kevinwhinnery.com/audio/zelda.mp3 )
相反,我想通过笔记本(它有内置麦克风)与我打电话的人交谈,然后让这个人回话。简而言之,我希望有现场语音。
问题:如何在ASP.NET-MVC 5.x中使用Twilio实现实时语音通话?
Settings.AccountSid
和 Settings.AuthToken
是我的凭据:
public static class Settings
{
public static string AccountSid { get { return "A###############0"; } }
public static string AuthToken { get { return "e###############0"; } }
public static string TwilioNumber { get { return "4########1"; } }
}
最佳答案
这里是 Twilio 布道者。
如果您想从浏览器调用电话,您需要考虑使用 Twilio Client for JavaScript:
https://www.twilio.com/docs/quickstart/csharp/client
这样您就可以从浏览器向 Twilio 发起 VoIP 调用。一旦调用到达 Twilio,您就可以将该调用与另一个 Twilio 客户端、SIP 端点或 PSTN 电话桥接:
https://www.twilio.com/docs/quickstart/csharp/client/outgoing-calls
希望对您有所帮助。
关于javascript - 如何使用 Twilio 调用实时语音电话而不是在接听电话时只播放 MP3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309884/