代码之家  ›  专栏  ›  技术社区  ›  Marcus

使用Twilio函数为Twilio搜索组。(又名FindMe)

  •  0
  • Marcus  · 技术社区  · 9 年前

    我正试图与Twilio Twiml建立一个狩猎小组

    我必须为狩猎组中的每个数字设置不同的twimlbin吗?

    或者有没有办法将所有这些结合到一个Twimlbin中?

    Twimlbin 1:
    <Response>
        <Dial 
             action="http://www.ourapp.com/webhook;FailUrl=/Twimlbin 2" 
             timeout="10" 
             callerId="555-555-5555">
             NUMBER1
        </Dial>
    </Response>
    
    
    Twimlbin 2:
    <Response>
        <Dial 
             action="http://www.ourapp.com/webhook;FailUrl=/Twimlbin 3" 
             timeout="10" 
             callerId="555-555-5555">
             NUMBER2
        </Dial>
    </Response>
    
    ... Repeat N times for each agent ...
    

    谢谢:-)

    1 回复  |  直到 9 年前
        1
  •  1
  •   philnash    9 年前

    Twilio开发者布道者。

    TwiML容器非常适合TwiML的静态部分,但您的用例需要更多。

    Twilio Functions 允许您运行Node。Twilio基础设施中的js代码。 I've built and tested a version of this that works with Twilio Functions

    从数字数组开始:

    const numbers = [...];
    

    exports.handler = function(context, event, callback) {
      const response = new Twilio.twiml.VoiceResponse();
      if (event.DialCallStatus === "complete" || event.finished) {
        // Call was answered and completed or no one picked up
        response.hangup();
      } else {
    

    如果不是的话,我们就找出下一个要打的电话号码。如果URL中有下一个数字。如果确实将其保存到变量,则选择数组中的第一个数字:

        const numberToDial = event.nextNumber ? event.nextNumber : numbers[0];
    

        let url;
        const currentNumberIndex = numbers.indexOf(numberToDial);
        if (currentNumberIndex + 1 === numbers.length) {
          // no more numbers to call after this.
          url = "/hunt?finished=true";
        } else {
          const nextNumber = numbers[currentNumberIndex + 1];
          url = "/hunt?nextNumber=" + encodeURIComponent(nextNumber);
        }
    

    然后生成TwiML以拨打下一个号码,并将URL作为操作传递。您可以将自己的URL添加为statusCallbackUrl,以跟踪状态。

        const dial = response.dial({ action: url });
        dial.number({ statusCallback: "https://yourapp.com/statuscallback" }, numberToDial);
      }
    
      callback(null, response);
    }