2
votes

I've sign up to start using Twilio and I'm trying to setup the quickstart (https://www.twilio.com/docs/voice/client/javascript/quickstart) and it's almost working but incoming calls are not being received by:

Client code (used on browser after getTokenCapabilities):

  Twilio.Device.incoming(function (conn) {
    log('Incoming connection from ' + conn.parameters.From);
    var archEnemyPhoneNumber = '+12093373517';

    if (conn.parameters.From === archEnemyPhoneNumber) {
      conn.reject();
      log('It\'s your nemesis. Rejected call.');
    } else {
      // accept the incoming connection and start two-way audio
      conn.accept();
    }
  });

Code on Twilio Function for voice calls (consoles are always printed and else condition is never called:

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
    console.log('entrou aqui');
    if(event.To) {
        console.log('entrou ali');
      // Wrap the phone number or client name in the appropriate TwiML verb
      // if is a valid phone number
      const attr = isAValidPhoneNumber(event.To) ? 'number' : 'client';

      const dial = twiml.dial({
        callerId: context.CALLER_ID,
      });
      dial[attr]({}, event.To);
    } else {
      twiml.say('Thanks for calling!');
    }
    console.log('callback');
     callback(null, twiml);
};

/**
* Checks if the given value is valid as phone number
* @param {Number|String} number
* @return {Boolean}
*/
function isAValidPhoneNumber(number) {
  return /^[\d\+\-\(\) ]+$/.test(number);
}

I've include my phone number as Verified Caller ID, got a number from Twilio and create the functions using template Twilio Client Quickstart.

On Twilio Client Quickstart, i've paste TwiML SID as TWIML_APP_SID and tried to use my phone number and the number from Twilio as CALLER_ID.

Also I've changed VOICE URL on TwiML configuration and changed the VOICE URL on phone number from twilio configuration.

Any ideas on what is missing or what is wrong? When I open on browser http://127.0.0.1:8080/, It's possible to make calls but I don't receive any call on browser when I call to twilio number.

1
Have you used ngrok to make your local application available to the webhooks from Twilio? Are there any errors in your Twilio debugger?philnash
@philnash Not sure why should I use ngrok if i'm using twilio client quickstart. My webhooks (for token and voice) are something like rubcub-123.twil.io/xxxx. I've set this url on phone number and on TWiML app. My browser can retrieve token-capabilities and create a TwilioClient but incoming() is never called. When i make a call to number it says "this is a trial account, press any key to run your code" and it enters in a loop....when i go to functions and check my voice call function, i can see that the code is reached but my browser client never receive anything,user866364
@philnash and always event.To is a phone number. Why my phone call is not redirected to browser client?user866364

1 Answers

1
votes

In order to answer call, you need to have your token name identity in a tag in your VoiceResponse, here is an example.

exports.incomingVoiceResponse = function voiceResponse( to ) {
  // Create a TwiML voice response
  const twiml = new VoiceResponse();

    // Wrap the phone number or client name in the appropriate TwiML verb
    // if is a valid phone number
    const attr = isAValidPhoneNumber(to) ? 'client' : 'number';

    const dial = twiml.dial({
      callerId: to,
    });
    dial[attr]({}, 'jesus');
  console.log(twiml.toString())
  return twiml.toString();
};

See the 'jesus' client tag I have put ? Here is the token generator side :

exports.tokenGenerator = function tokenGenerator() {
  const identity = 'jesus';
  const capability = new ClientCapability({
    accountSid: config.accountSid,
    authToken: config.authToken,
  });

  capability.addScope(new ClientCapability.IncomingClientScope(identity));
  capability.addScope(new ClientCapability.OutgoingClientScope({
    applicationSid: config.twimlAppSid,
    clientName: identity,
  }));

  // Include identity and token in a JSON response
  return {
    identity: identity,
    token: capability.toJwt(),
  };
};

This works for me using the node quickstart as is and changing both of these functions.

However, don't forger to change the from 'number' to 'client' in function voiceResponse because it's an incoming call and not an outgoing.

  const attr = isAValidPhoneNumber(to) ? 'client' : 'number';

instead of

  const attr = isAValidPhoneNumber(to) ? 'number' : 'client';

Since the default nameGenerator from the client-quickstart-node from Twilio generates a random name, it isn't properly set when receiving incoming call.