levi
levi

Reputation: 25161

Twilio cycle through numbers until call is answered

I am interested in cycling through a list of numbers, dialing each number until one picks up. If we reach the end of the list, start again from beginning, N amount of times.

My script looks like this:

<Response>
    <Dial action="hangup.php" timeout="5">
        <Number url="greet.php">
            123-456-7890
        </Number>
    </Dial>
    <Dial action="hangup.php" timeout="5">
        <Number url="greet.php">
            223-456-7890
        </Number> 
    </Dial>
</Response>

Is there any way for the response to loop N times, or is the solution to simply hard code the dial elements N times within the Response block?

Upvotes: 0

Views: 405

Answers (1)

am1704
am1704

Reputation: 857

You can try using "<Redirect>" . But use it cautiously to make sure you don't end up in infinite loop ( as always the case with any loops)

I have not tested this code (in NodeJS) below yet , but quickly put it down to give you an idea of how <Redirect> could be potentially used to achieve what you require

const maxRetries = 10;

app.get('/loopOnThisTwiml',
 function(i_Req,o_Res)
   {

      var counter = i_Req.query.loopCounter ;
      if(!counter)
         {
             counter = 0 ;
         }
      else
         {
            counter = counter+1;
          } 

       var ivrTwimlResp = new twilio.TwimlResponse();
       var thisTwimlUrl = "/loopOnThisTwiml?loopCounter=" + counter ; 

       ivrTwilRes.dial({callerId:'+1xxxxxxxxx',action:"hangup.php",method:"GET",timeout:"5"},
             function()
                 {
                      this.number('+11234567890',{url:"/greet.php",method:"GET"});
                 }
            )
            .dial({callerId:'+1xxxxxxxxx',action:"hangup.php",method:"GET",timeout:"5"},
                      function()
                          {
                               this.number('+12234567890',{url:"/greet.php",method:"GET"});
                          }
                     );   

       if(counter < maxRetries)
       {
          ivrTwilRes.redirect( { method : 'GET' } , thisTwimlUrl );
       }

       o_Res.set('Content-Type','text/xml');
       o_Res.send(ivrTwimlResp.toString());
   }
);

The code above generates the TwiML mentioned in your question and adds "<Redirect>" to the same TwiML until you reach the counter (maxRetries) ; maxRetries is defined as a global constant.

A side note : The TwiML in your question is good when you want to dial serially and have preference to one number over the other. In case you want to simultaneously ring multiple numbers and let anyone pick up the call , you could also have a look at Simulring .

Upvotes: 2

Related Questions