Search code examples
twiliotwilio-functions

How to use a Twilio function to transfer a call to a phone number with an extension?


I'm trying to use a Twilio function to transfer a call to a phone number with an extension.

The Twilio function is called from a Twilio flow.

Right now, the call transfer to the phone number. However, the extension is never invoked.

I added some "w" characters for pause in the "sendDigits"...but it did not change anything.

Here is my Twilio flow

Here is my Twilio function widget with the parameter

Here is the code of the twilio function

exports.handler = function(context, event, callback) {
  // set-up the variables that this Function will use to forward a phone call using TwiML

  // REQUIRED - you must set this
  let phoneNumber = event.PhoneNumber || "NUMBER TO FORWARD TO";    
  // OPTIONAL
  let callerId =  event.CallerId || null;
  // OPTIONAL
  let timeout = event.Timeout || null;
  // OPTIONAL
  let allowedCallers = event.allowedCallers || [];

  // generate the TwiML to tell Twilio how to forward this call
  let twiml = new Twilio.twiml.VoiceResponse();

  let allowedThrough = true;
  if (allowedCallers.length > 0) {
    if (allowedCallers.indexOf(event.From) === -1) {
      allowedThrough = false;    
    }
  }

  let sendDigits = event.sendDigits;
  let dialParams = {};
  dialParams.sendDigits = sendDigits

  if (callerId) {
    dialParams.callerId = callerId;
  }
  if (timeout) {
    dialParams.timeout = timeout;
  }

  if (allowedThrough) {
    twiml.dial(dialParams, phoneNumber);
  }
  else {
    twiml.say('Sorry, you are calling from a restricted number. Good bye.');
  }

  // return the TwiML
  callback(null, twiml);
};

Any idea ?


Solution

  • The Dial verb doesn't have a sendDigits attribute, but the REST API does.

    You can use the Number noun with the Dial verb, https://www.twilio.com/docs/voice/twiml/number, and its associated URL parameter to reference a TwiML Bin with a Play, https://www.twilio.com/docs/voice/twiml/play, and its digits attribute, to play the DTMF after the dialed party answers the phone, but before the two parties communicate with one another.