Hunt Group for Twilio, using Twilio Functions. (aka FindMe )

前端 未结 1 1451
忘掉有多难
忘掉有多难 2020-12-12 06:19

I am trying to set up a hunt group with Twilio Twiml

Do I have to set up a different twimlbin for each number in the hunt group?

Or is there a way to join al

相关标签:
1条回答
  • 2020-12-12 07:14

    Twilio developer evangelist here.

    TwiML Bins are great for static bits of TwiML, but your use case needs a bit more than that.

    I recommend you check out Twilio Functions which allow you to run Node.js code in Twilio's infrastructure. I've built and tested a version of this that works with Twilio Functions.

    Here's an explanation of how it works:

    Start with an array of your numbers:

    const numbers = [...];
    

    Then, in the function, check if the callstatus is completed and hang up if it is.

    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 {
    

    If it's not, we work out the next number to call. If you have the next number in the URL. If you do save it to a variable, otherwise pick the first number in the array:

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

    Then, assign the next number to dial from the array.

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

    Then generate the TwiML to Dial the next number and pass the URL as the action. You can add your own URL as the statusCallbackUrl to keep a track of the statuses.

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

    I can't promise this will work, but I hope you see where I'm going with it. Let me know if it helps at all.

    0 讨论(0)
提交回复
热议问题