Hunt Group for Twilio, using Twilio Functions. (ak

2019-01-20 18:28发布

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 all this together into a single 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 ...

Thank you :-)

标签: twilio twiml
1条回答
可以哭但决不认输i
2楼-- · 2019-01-20 19:16

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.

查看更多
登录 后发表回答