Answered Notification for Outbound call from an App - TWILIO - twilio

For Outbound Calls From An App to a Number,
There are two legs - One between my browser and Twilio and the other between Twilio and the number to be dialled
Can I set a webhook to trigger, on each callstatus value change (especially when the Call Status is in-progress) of the second leg and How?
This is necessary so that I can start the timer and show hold button only after the customer picks up the phone
Any other idea to achieve this also Welcome. Thanks in advance.

Twilio developer evangelist here.
When you dial out from Twilio Client you can send parameters into the request, such as the number, then Twilio will send your application a webhook with those parameters to find out what to do next. To dial a number, you would use the <Dial> verb and nest a <Number> within it.
With a <Number> you can register for webhook callbacks when certain events take place. You do so by setting a statusCallback attribute to the URL that will receive the callback. By default the statusCallback URL will only receive the completed event when the call finishes, however you can register for more events using the statusCallbackEvent attribute.
The event you want to subscribe too is called "answered" and the TwiML you need is:
<Response>
<Dial>
<Number statusCallback="http://example.com/calls/callback" statusCallbackEvent="answered">
NUMBER TO DIAL
</Number>
</Dial>
</Response>
Let me know if that helps at all.

Related

play music to caller while simultaneous dial to multiple destinations

We've got a basic twiml set up that sends a call to multiple destinations, it looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play loop="10">https://xxx.xxx.xxx/assets/MoH.wav</Play>
<Dial>
<Number>+1800XXXXXXX</Number>
<Number>+1912XXXXXXX</Number>
</Dial>
</Response>
The problem with this is that the <Dial> doesn't happen until the <Play> finishes. We want to play music to the caller while waiting for the call to be answered by one of the destination parties.
We've tried <Enqueue> to play the music but it still doesn't dial simultaneously.
Twilio developer evangelist here.
You are right that TwiML expects to finish one verb before it starts the next, so in your example code it will play the <Play> all the way through before starting to dial the numbers.
If you want to play music to the caller while the dial happens, then you will need to use <Enqueue> to put them into a holding pattern while you dial the other numbers. However, you will not be able to use TwiML to dial the numbers, instead you will need to make the outbound calls using the REST API.
Once one of the outbound calls connects, you can then connect it with the original call by <Dial>ling in to the <Queue> that you placed the caller in. You will also want to end the other calls using the REST API.

Twilio Conference statusCallbackEvent="start speaker" not functioning

My Twilio server has 3 pages
/listener
Sets up a <dial> and <Conference statusCallback="/gather" statusCallbackEvent="speaker start"> and appends caller to said conference. In the conference, the events request should be sent on any user speaking and conference starting (starting is just used for debugging in case it's something wrong with speaking alone, but the starting request won't be sent as well).
/gather
Uses gather to listen to speech with action='/parse'
/parse
Parses the message.
Right now, when I set the webhook to /gather, I could see 2 POST requests to /gather and /parse no problem. But when I set it to /listener, only 1 POST request per call would appear and no subsequent requests would be sent on conference start or people speaking. Can anyone possibly tell me any example of statusCallbackEvent working with gather or provide any workarounds?
Twilio developer evangelist here.
It looks to me like you are trying to use speech detection on the ongoing conference call via events?
If that is the case, then I'm afraid that is not what statusCallbacks in <Conference> do. statusCallback events during calls are asynchronous callbacks, your response to them will not affect the ongoing call. Responding to a statusCallback event with TwiML will have no effect.
Subsequently, it's not possible to simultaneously continue a <Conference> and use <Gather> on the participants.

Diverting twilio call to voicemail if unanswered

I'd love some advice on my twilio setup for a problem I'm trying to solve.
Overview:
Each user in our system is provisioned a twilio phone number that they can hand out to anyone to contact them.
If personA contacts a user in our system (userB) via the provisioned twilio phone number, we'd like to connect them with userB if they are available. If userB is not available, we'd like to direct personA to voicemail. In other words, we want to make sure that we have control of the voicemail experience and the voicemail itself so that we can store it in our system, rather than having the voicemail be left on userB's device.
Current solution:
PersonA's incoming call gets added to a queue. At the same time, the system dials out userB.
UserB is asked to press 1 to accept the call. The reason for the explicit entry from UserB is to detect whether UserB is available to answer the call. (For example, if the call to UserB goes to their personal voicemail, the explicit digit entry will not happen telling us they are not available to answer.)
If UserB does not enter 1 in a specified amount of time, PersonA is directed to voicemail.
If UserB presses 1, call to UserB is modified (via twilio rest api) to dial the queue that PersonA is in to connect UserB and PersonA.
Problem with current solution:
In this solution, the control of when to divert personA's call to voicemail is controlled by the outcome of the call to UserB, which seems suboptimal. For instance, we may not be able to call UserB at all. In this case, personA would stay in the queue indefinitely.
What I'd like to happen in this case is to poll the queue personA is in to check the time in queue, and divert the call to voicemail if time in queue is greater than a threshold. However, it does not seem like it is possible to accurately know how long a call is unattended in a queue because:
The status of a call in a queue is in-progress even if the caller is listening to wait music. This is the same status as if PersonA's call had been answered.
If UserB dials into the queue, the call is only dequeued when the bridged parties disconnect, with no change in the call status of PersonA's call to indicate that they have been connected to UserB.
Questions
Is my understanding of why I cannot poll the call queue to divert calls to voicemail correct?
Should I instead be calling PersonA into a conference, and if UserB is available, connecting him/her to the conference that PersonA is in?
If I use a conference setup, what would be the easiest way to detect how long PersonA has been waiting in the conference so as to divert PersonA's call to voicemail in the event of UserB never joining the conference?
Twilio developer evangelist here.
I think you may have overcomplicated things a bit here with the queue. You can actually provide the message and gather within the original call without having to dial out yourself and eventually connect the two calls.
Here's how:
Your incoming call TwiML should look like this:
<Response>
<Dial action="/call_complete" timeout="30">
<Number url="/whisper">
ONWARD DIAL NUMBER
</Number>
</Dial>
</Response>
Giving the <Number> noun a URL will play the TwiML contents of that URL before the two calls are connected. You can use <Gather> in here to make sure the user has answered the call and not their own voicemail system:
/whisper
<Response>
<Gather numDigits="1" timeout="10" action="/gather_result">
<Say voice="alice">You are receiving a call, press any key to accept</Say>
</Gather>
<Hangup/>
</Response>
The /gather_result needs to work out whether a key was pressed or not. If it was pressed then we head through to the call, which we can do with an empty response as this gives control back to the original <Dial>. If no number was pressed we hangup this end, which causes the original <Dial> to complete and direct onto its action attribute. (I'm not sure what language you're working with but here is some Rubyish pseudo code)
/gather_result
<Response>
if params["Digits"] and params["Digits"].empty?
<Hangup/>
end
</Response>
/call_complete will then get called once the <Dial> action is over. If the status of the call at this point is "completed" or "answered" then the user has picked up the call and responded correctly to the whisper and we can hang up. If it's anything else, we redirect our call onto our voicemail recorder.
/call_complete
<Response>
if params["DialCallStatus"] == "completed" or params["DialCallStatus"] == "answered"
<Hangup/>
else
<Say voice="alice">The call could not be answered this time, please leave a message</Say>
<Record action="/record_complete" />
end
</Response>
Then finally your /record_complete action can do whatever you want with the recording URL and hang up the call.
/record_complete
<Response>
<Hangup/>
</Response>
This can all be achieved with Twimlets, as described in this blog post. Let me know if this helps at all.

Can I send digits to an incoming twilio call with twiml?

I have a twilio number that can process incoming calls with twiml. These incoming calls expect the recipient to press some digits after the call is connected.
If I were making an outgoing call, I could use the sendDigits attribute of a <Dial> tag. However, I can't figure out how to do this in response to an incoming call.
If I were receiving the call in a web client, I could use connection.sendDigits
Is there a way to do this in just twiml? Should I just play a recording file of various DTMF tones?
Edit: To clarify, I'm receiving calls from another automated system that expects additional numbers to be dialed after the call is connected.
Turns out the way to to do this is by using the <Play> twiml tag, which accepts a digits attribute.
<Play digits="94"></Play>

Twilio voicemail - Get a call back if caller hangs up before recording starts

Question
Implementing voicemail in Twilio. How can I get a call back if the caller hangs up before the recording starts?
More info
After the incoming call's <dial> times out, the call back URL responds with this:
<Response>
<Say>Please leave a message.</Say>
<Record playBeep="true" action="http://..." />
</Response>
The issue I seem to be having is that if the caller hangs up while the <Say> verb is executing, the <Record> verb never executes and thus the application never receives a call back.
Is it possible to receive a call back under this circumstance? If so, how do I make that happen?
Twilio evangelist here.
One idea might be to separate this TwiML response into two separate responses and track what the last step in your workflow you sent to the call was. First send the Say:
<Response>
<Say>Please leave a message.</Say>
<Redirect>http://example.com/record</Redirect>
</Response>
Then redirect to the Record:
<Response>
<Record playBeep="true" action="http://..." />
</Response>
To get notified when the call ends, set the StatusCallback attribute on your phone number. When Twilio makes the request to the StatusCallback URL, you can check to see what the last step that you sent to the user was and take the appropriate action.
Hope that helps.

Resources