I have very basic knowledge with programming in Node.JS but I see that Twilio Functions are powerful and I have managed to achieve 50% of what I want to do even with my basic knowledge.
What I am looking to do is forward calls to a number that I purchased with Twilio, to mobile number in Brazil. I managed to do this part using your template “Call Forward” (I attach the code below). Nonetheless, the second part I cannot understand how I should attack the problem. I would like that if the call is not answered in 10 seconds that it should be forward to another number.
My doubts are:
- I imagine that I must use the 'timeout' attribute to set the limit in seconds that <Dial> waits for the called party to answer the call. Is that correct?
- Where must I place this code of the identification of the 'timeout' of 10 seconds? Can I place it in the same function as the first call redirection, if so how is this “hook” triggered? Or should I create a second function, and if so how can I call this second function when the ‘timeout’ of 10 seconds it complete?
=======
/**
* Call Forward Template
*
* This Function will forward a call to another phone number. If the call isn't answered or the line is busy,
* the call is optionally forwarded to a specified URL. You can optionally restrict which calling phones
* will be forwarded.
*/
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 || "+5511********";
// OPTIONAL
let callerId = event.CallerId || null;
// OPTIONAL
let timeout = event.Timeout || null;
// OPTIONAL
let allowedCallers = event.allowedCallers || [];
// new
let callStatus = event.CallStatus || '';
let answeredBy = event.AnsweredBy || '';
// 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 dialParams = {};
if (callerId) {
dialParams.callerId = callerId
}
if (timeout) {
dialParams.callerId = timeout
}
if (allowedThrough) {
twiml.dial(dialParams, phoneNumber);
console.log('Dialing:');
}
else {
twiml.say('Sorry, you are calling from a restricted number. Good bye.');
}
if (callStatus === 'in-progress') {
console.log('AnsweredBy:' + answeredBy);
}
// return the TwiML
callback(null, twiml);
};
/* * Example: Strictly Linear Phone Tree * This example shows how you might iterate through a list of numbers. * Each will be tried for 10 seconds before moving on to the next. */exports.handler = function(context, event, callback) { // REQUIRED - URL to hangup Function let hangUpAction = ""; // REQUIRED - URL to this Function let phoneTreeUrl = ""; // Timeout - The amount of time we want to dial before giving up let timeout = event.Timeout || 10; // Attempt - The position in the list we are currently at let attempt = event.Attempt || 0; // Phone Numbers - The list of numbers to cycle through let phoneNumbers = [ "+5511********" ];
// generate the TwiML to tell Twilio how to forward this call let twiml = new Twilio.twiml.VoiceResponse(); // If we have exhaused all numbers end the attempts if (attempt > phoneNumbers.length) { twiml.say("Sorry, we could not find someone to take your call. Please try again later"); callback(null, twiml); } // Build the state for our Twiml response let target = phoneNumbers[attempt]; let nextAttempt = attempt + 1; let nextAttemptUrl = phoneTreeUrl + "?Attempt=" + nextAttempt; twiml.dial(target, { timeout: timeout, action: hangUpAction }); twiml.redirect(nextAttemptUrl); callback(null, twiml);};/* * Example: Simplified Simulring. * This will dial all of the phone numbers specified in the phoneNumbers array at one. * The first number ot answer the call will be connected. */exports.handler = function(context, event, callback) { // Phone Numbers - The list of numbers to cycle through let phoneNumbers = [ "+5511********" ];
// generate the TwiML to tell Twilio how to forward this call let twiml = new Twilio.twiml.VoiceResponse(); let dial = twiml.dial(); for (var phoneNumber in phoneNumbers) { dial.number(phoneNumber); } callback(null, twiml);};Thank you Chris, that has helped me immensely.
Just two more quick questions if I may?
1) 1) How can I tell if a call to a particular number on the “twiml.dial(target, { timeout: timeout, action: hangUpAction });” was answered? And if it was answered, I will update a value of a certain pre-declared variable, which I will use later on in the function?
2) 2) Is it possible that I can obtain the caller´s number and pass it on to another function I created? This other function I have linked to the “CALL STATUS CHANGES” and when the call ends I need the callers number to send an SMS with some information; I am not sure if the is the correct an recommended procedure.
Thank you in advance,
Pedro
/* * Example: Strictly Linear Phone Tree * This example shows how you might iterate through a list of numbers. * Each will be tried for 10 seconds before moving on to the next. */exports.handler = function(context, event, callback) { // REQUIRED - URL to hangup Function //let hangUpAction = "https://rhetorical-pain-6593.twil.io/CM2"; let hangUpAction = "https://rhetorical-pain-6593.twil.io/RecordSales"; // REQUIRED - URL to this Function let phoneTreeUrl = "https://rhetorical-pain-6593.twil.io/CM1"; // Timeout - The amount of time we want to dial before giving up let timeout = event.Timeout || 10; // Attempt - The position in the list we are currently at let attempt = event.Attempt || 0; // Phone Numbers - The list of numbers to cycle through //" let phoneNumbers = [ "+111...", "+222..." ];
// generate the TwiML to tell Twilio how to forward this call let twiml = new Twilio.twiml.VoiceResponse(); // If we have exhaused all numbers end the attempts if (attempt > phoneNumbers.length) { twiml.say("Sorry, we could not find someone to take your call. Please try again later"); callback(null, twiml); } // Build the state for our Twiml response let target = phoneNumbers[attempt]; let nextAttempt = attempt + 1; let nextAttemptUrl = phoneTreeUrl + "?Attempt=" + nextAttempt; twiml.dial(target, { timeout: timeout, action: hangUpAction }); twiml.redirect(nextAttemptUrl); callback(null, twiml);};