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