Twilio 循环显示号码直到有人接听电话

Twilio cycle through numbers until call is answered

我有兴趣在号码列表中循环,拨打每个号码,直到有人接听为止。如果我们到达列表的末尾,从头开始,N 次。

我的脚本如下所示:

<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>

有没有办法让响应循环 N 次,或者是在 Response 块中简单地硬编码拨号元素 N 次的解决方案?

您可以尝试使用 "<Redirect>" 。但是请谨慎使用它以确保您不会陷入无限循环(任何循环都是如此)

我还没有在下面测试这段代码(在 NodeJS 中),但很快就把它写下来让你了解 <Redirect> 如何潜在地用于实现你需要的东西

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

上面的代码 生成您问题中提到的 TwiML 并将 "<Redirect>" 添加到同一 TwiML 直到您到达计数器 (maxRetries) ; maxRetries 被定义为一个全局常量。

旁注:当您想连续拨号并且优先选择一个号码时,您问题中的 TwiML 非常有用。如果您想同时拨打多个号码并让任何人接听电话,您还可以查看 Simulring .