Ronan
Ronan

Reputation: 327

Uncaught twilio.exception wrong number of segments

I'm using Twilio to make a call from a browser to the phone. When I click call I get this error 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>

In the code behind I have:

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();
        }
    }

Upvotes: 1

Views: 1482

Answers (2)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

I can't quite tell how you are getting the value of the TwilioToken property to render in your HTML. In this line of the JavaScript:

Twilio.Device.setup('@token');

@token should be being replaced by the value being generated in your code-behind. Since you mention code-behind it sounds like you might be using WebForms. In order to render the value of the TwilioToken property in your HTML you'd need to use something like this syntax:

Twilio.Device.setup('<%= TwilioToken %>');

Once you do that when you load the page in a browser you should see that syntax replaced by a long encoded string of characters that Twilio uses to setup the JavaScript library.

Hope that helps.

Upvotes: 1

user3491418
user3491418

Reputation: 1

The sample code shows lines like this:

$capability = new Services_Twilio_Capability($TWILIO_SID, $TWILIO_TOKEN);
$capability->allowClientOutgoing('APabe7650f654fc34655fc81ae71caa3ff');
$TOKEN = $capability->generateToken();

Followed by later on this:

Twilio.Device.setup("<?php echo $TOKEN; ?>");

I was getting this same error until I realized that I had misspelled $TOKEN and was passing a blank string to Twilio.Device.setup. After fixing it, the error went away.

Upvotes: 0

Related Questions