0

I would like to setup twilio to call person A if person A doesn't answer I want to call person B and so on.

From my understanding twilio will request the URL provided once the call is answered, either by machine or human (provided machine detection is enabled).

Currently I have it setup so that if an answering machine is detected it serves TWIML XML to hangup and if a person answers it serves the TWIML XML message. but I can't find a way in which to call the next person on the list.

`

client.calls.create({
  to: "+1" + numbers[i],
  from: process.env.TWILIO_NUMBER,
  url: "https://publically.accessable/url-of_mine",
  machineDetection: "Enabled",
  method: "GET"
})
.catch((err) => {
  console.log(err)
})

here is the function inside my publicly available URL

const params = event.queryStringParameters;
    if (params.AnsweredBy == "machine_start") {
        let xml = `
            <?xml version="1.0" encoding="UTF-8"?>
            <Response>
                <Hangup/>
            </Response>`
    
        return Response(xml, mimetype = 'text/xml')
    } else{
        let xml = `
            <?xml version="1.0" encoding="UTF-8"?>
            <Response>
                <Say voice="alice" loop='3'>Wildfire Alert. """ + memberCount + """  PURE members are within 15 miles of """ + fireName + """ fire. Please refer to Incident Monitor for further information.</Say>
            </Response>`
        return Response(xml, mimetype='text/xml')
    }

`

Brian McCall
  • 1,831
  • 1
  • 18
  • 33
  • Possible duplicate of [Hunt Group for Twilio, using Twilio Functions. (aka FindMe )](https://stackoverflow.com/questions/45305060/hunt-group-for-twilio-using-twilio-functions-aka-findme) – philnash Jul 27 '17 at 01:57
  • This looks similar to the question above, but it occurs to me that you might be doing this the other way around. Are you generating the call from the REST API? Can you share the code you have so far? – philnash Jul 27 '17 at 01:59
  • I am generating the call from the rest API. I edited my question to show what I have so far. – Brian McCall Jul 27 '17 at 20:43
  • @philnash I am doing it the other way around. I am not receiving a call I am sending a call. From what I can tell Twiml function are only for receiving calls or messages – Brian McCall Jul 27 '17 at 20:59

1 Answers1

0

Twilio developer evangelist here.

Apologies for my vote to close, I see that this is a different use case now.

To make a new call when the previous one isn't answered, or is answered by answer machine there are a couple of things you need to do.

First up, for the case where the call is not answered, fails, receives a busy signal or is otherwise cancelled, you need to set up a statusCallback for the call. statusCallback is a parameter that you set to another publicly accessibly url in the initial API call to make the phone call. By default, when you set a statusCallback Twilio will make a request to the URL when

client.calls.create({
  to: "+1" + numbers[i],
  from: process.env.TWILIO_NUMBER,
  url: "https://publicly.accessable/url-of_mine",
  machineDetection: "Enabled",
  method: "GET",
  statusCallback: "https://another.publicly.accessible/url-of_yours?currentIndex=" + i,
  statusCallbackMethod: "GET"
})

You might want to put some extra information into the URL, such as the current index (I've done that in my example above.

When you receive the request to the statusCallback URL you should check what the actual status of the call is. Basically, anything other than completed means the call did not get through and you need to call the next number in your list. This is where you can use that index again.

const params = event.queryStringParameters;
if (params.CallStatus != 'completed') {
  let i = parseInt(params.currentIndex, 10) + 1;
  client.calls.create({
    to: "+1" + numbers[i],
    from: process.env.TWILIO_NUMBER,
    url: "https://publicly.accessable/url-of_mine",
    machineDetection: "Enabled"
    method: "GET",
    statusCallback: "https://another.publicly.accessible/url-of_yours?currentIndex=" + i,
    statusCallbackMethod: "GET"
  })
}
return Response('')

I haven't been able to test, but my hunch is that when you return <Hangup/> to the answer machine detected call, that will be counted as a completed call too. The best way to deal with this is to start up a new call whilst you're responding with the <Hangup/>

const params = event.queryStringParameters;
if (params.AnsweredBy == "machine_start") {
    let xml = `
        <?xml version="1.0" encoding="UTF-8"?>
        <Response>
            <Hangup/>
        </Response>`

    client.calls.create( callParameters )

    return Response(xml, mimetype = 'text/xml')
} else{ .. } 

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thats similar to what I had when I wrote the initial code in python. But because of pythons synchronous nature it wasn't working as expected. This should do just fine in Node, Thank you. Will select as answer once I test it. – Brian McCall Jul 29 '17 at 02:28