我正在使用Twilio从浏览器拨打电话。当我点击通话时,我收到此错误Uncaught twilio.exception wrong number of segments
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
Twilio.Device.setup('@token');
Twilio.Device.ready(function (device) {
$("#log").text("Client '@clientName' is ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
$("#log").text("Successfully established call");
});
Twilio.Device.disconnect(function (conn) {
$("#log").text("Call ended");
});
Twilio.Device.incoming(function (conn) {
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
function call() {
// get the phone number or client to connect the call to
params = { "PhoneNumber": $("#number").val() };
Twilio.Device.connect(params);
}
function hangup() {
Twilio.Device.disconnectAll();
}
</script>
<div class="hero-unit" style="background-color: white; margin-left: 650px;">
<div class="container">
<h3>Call us and watch us answer!</h3>
<p style="margin-left: -65px;">Use Twilio to call our office and watch us answer on camera!</p>
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
<input type="text" id="number" name="number"
placeholder="Enter a phone number or client to call" />
</div>
</div>
在我背后的代码中:
public string TwilioToken
{
get
{
string accountSid = "*************";
string authToken = "*************";
string applicationSid = "************";
string clientName = "*****";
if (Request["client"] != null)
{
clientName = Request["client"];
}
var capability = new TwilioCapability(accountSid, authToken);
capability.AllowClientOutgoing(applicationSid);
capability.AllowClientIncoming(clientName);
return capability.GenerateToken();
}
}
答案 0 :(得分:1)
Twilio传道者在这里。
我无法告诉您如何获取要在HTML中呈现的TwilioToken属性的值。在这一行的JavaScript中:
Twilio.Device.setup('@token');
@token应该被代码隐藏中生成的值替换。既然你提到了代码隐藏,那么听起来你可能正在使用WebForms。为了在HTML中呈现TwilioToken属性的值,您需要使用类似这样的语法:
Twilio.Device.setup('<%= TwilioToken %>');
当您在浏览器中加载页面时,您会看到该语法被Twilio用于设置JavaScript库的长编码字符串替换。
希望有所帮助。
答案 1 :(得分:0)
示例代码显示如下行:
$capability = new Services_Twilio_Capability($TWILIO_SID, $TWILIO_TOKEN);
$capability->allowClientOutgoing('APabe7650f654fc34655fc81ae71caa3ff');
$TOKEN = $capability->generateToken();
后面跟着这个:
Twilio.Device.setup("<?php echo $TOKEN; ?>");
我收到了同样的错误,直到我意识到我拼错了$ TOKEN并将空字符串传递给Twilio.Device.setup。修好后,错误就消失了。