Twilio: Twilio.Device.connect() not hitting voice request URL

746 views Asked by At

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

There are 1 answers

4
Dokinoki On

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