2

In my front end Javascript code, I call Twilio.Device.connect(), and it is not firing a request to my Voice Request URL. I am not sure what is going on here. I ensure that I setup my capability token before hand, and there are no errors, but it still doesn't work. Here is front end JS code.

            Twilio.Device.setup(resp.token);
            Twilio.Device.connect({autoDial: true});

            // respond to "connect" event

            Twilio.Device.connect(function (conn) {
                alert("Got here!");
            }

Also here is my code to generate the token.

public static void getToken()
{
    TwilioCapability t = new TwilioCapability(ACCOUNT_SID, AUTH_TOKEN);

    t.allowClientOutgoing(APP_SID);
    t.allowClientIncoming("test");

    try {
        throw new OKResponse(ImmutableMap.of("token", t.generateToken(3600)));
    } catch (DomainException e) {
        Logger.error(e, "Error generating twilio token: %s", e.getMessage());
    }
}
user2158382
  • 4,430
  • 12
  • 55
  • 97

1 Answers1

1

I had the same problem,

You need to call the function generateToken() after calling allowClientOutgoing() and allowClientIncoming() so that the object created by Services_Twilio_Capability() has the app link.

This works:

$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);
$strToken = $objToken->generateToken();

This does not:

$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$strToken = $objToken->generateToken();
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);

Also, it will not throw an error but your js will always show as "disconnected"

UPDATE

Here is an edit of my backend:

   /**
     * Create an instance of Services_Twilio_Capability();
     *
     * @return object
     */
    private function instantiateCapability(){
        if(is_null($this->objCapability))
            $this->objCapability = new \Services_Twilio_Capability(TWILIO_ID, TWILIO_KEY);
        return $this->objCapability;
    }

    /**
     * Generate a token
     *
     * @link http://twilio-php.readthedocs.org/en/latest/usage/token-generation.html
     * @param bool $boolOutgoing Allow outgoing connections
     * @param bool $boolIncoming Allow incoming connections
     * @return string
     */
    public function generateToken($boolOutgoing = true, $boolIncoming = true){
        $objCapability = $this->instantiateCapability();

        if($boolOutgoing) $objCapability->allowClientOutgoing(TWILIO_SID]);
        if($boolIncoming) $objCapability >allowClientIncoming($_SESSION[$GLOBALS['APP_NAME'] . 'ID']);

        $strToken = $objCapability->generateToken(TOKEN_DURATION);
        return json_encode(array('status' => 1, 'token' => $strToken));
    }

And here is the frontend (AJAX response callback):

function(result){
    if(result.status == 1) {
       //Load the twilio object
       Twilio.Device.setup(result.token);
    }
}
Megan Speir
  • 3,745
  • 1
  • 15
  • 25
Dokinoki
  • 171
  • 2
  • 10
  • I already do that. I should have also put in my code to generate the token – user2158382 Nov 24 '14 at 22:03
  • Your function getToken() returns a void value, I think it should return the a string (the token). My backend is in php, but I think in both cases we have to return a value that will be passed to the client via AJAX. – Dokinoki Nov 25 '14 at 01:08
  • my method does return a string and it can be verified when i look at the function response. After a succesful json to that method a string token is returned in `resp.token` – user2158382 Nov 25 '14 at 01:26
  • Are you using version 1.2 of the JS library? (https://static.twilio.com/libs/twiliojs/1.2/twilio.min.js), I had problems with 1.1. Also is the url in the app properly setup? Do you have mod_security or something similar? Twilio's servers do not send some types of headers which can make apache refuse the connection – Dokinoki Nov 25 '14 at 01:32