0
votes

I can't get a browser to accept and answer an incoming call using Twilio JavaScript API.

Every time I call my Twilio number the call just hangs up and I don't see anything happen in my browser.

According to my JS I should get an alert popup when the call is connected/answered.

I've setup my Request URL correctly in my Twilio account and I've even checked the Twilio Debugger and don't see any error messages.

See code below for what I'm using in my browser app that should answer the incoming call.

FYI, I'm using PHP library to generate the Twilio Token. And I've double checked my Twilio API credentials - they're all correct (I've removed them in my code post below).

FYI2, I get the JS alert that the Twilio Device setup is ready.

FY3, I know the voice request URL is setup in Twilio correctly because if I change that code to Say Hello it says hello and then hangs up when I call my Twilio number.

<?php
###############
# Define Vars #
###############
include 'Services/Twilio/Capability.php';
$accountSid = 'A_xxxxxxxxxxxxxxxxxxx';
$authToken = 'f_xxxxxxxxxxxxxxxxxxx';
$appSid = 'AP_xxxxxxxxxxxxxxxxxxx';
$clientName = 'jack';

####################
# Get Twilio Token #
####################
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming($clientName);
$token = $capability->generateToken();
?>
<!DOCTYPE html>
<html>
<head>
<title>Twilio Incoming Call Test</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
<script>

Twilio.Device.setup('<?=$token?>');

Twilio.Device.ready(function(device) {
alert('ready!');
});

Twilio.Device.incoming(function(conn) {
conn.accept(incomingCall(data));
});

function incomingCall(data)
{
alert("Incomging Call:\n"+data);
}

Twilio.Device.error(function(conn) {
alert("Error:\n"+data);
});

</script>
</head>
<body>
<h1>Twilio Incoming Call Test</h1>
</body>
</html>

Here is my Voice Request URL code:

<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<Response>
<Dial>
<Client>jack</Client>
</Dial>
</Response>
1
conn.accept(incomingCall(data)); - what does var 'data' contains ? - Shanthakumar

1 Answers

1
votes

I figured out what the issue was. Pretty simple. The account sid and auth token where set to my sandbox/test credentials and when I changed this to live credentials it worked. Problem solved!