Twilio respond to keypress during voice call - twilio

I am new to Twilio and to programming in general.
I have a simple Arduino based App running on an ESP8266. Basically it rings an alarm on a scheduled basis, and requires the user to press a button to silence the beeping alarm.
If the alarm beeps 10 times, it then places a voice call from Twilio to a specific number and announces "the alarm is ringing".
Everything is working great to that point.
I would like the voice call to say "The alarm is ringing. Press 1 to silence the alarm."
I have no clue as to how to 'accept' the buttonpress . Everything that I can find seems to be about routing internally within Twilio (forwarding, voicemail, etc), but nothing about Twilio posting a response outside.
Any guidance? Any PHP sample code anywhere that does this kind of thing?

You'll want to use a gather in your TwiML
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather timeout="10" numDigits="1">
<Say>The alarm is ringing. Press 1 to silence the alarm.</Say>
</Gather>
</Response>
https://www.twilio.com/docs/api/twiml/gather#attributes
once the user presses a digit then your server will be hit with another request with the Digits param set, check if it equals 1

Related

Can you modify a Twilio call in-progress while it's still ringing?

I'm trying to simulate a typical phone behaviour, where a call comes in and "rings" until someone picks it up, however I'd like to problematically control this pickup action.
Initially, when the call comes in, I throw the following TwiML at the call:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="300"/>
<Reject reason="busy"/>
</Response>
This delays answering the call by up to 5 minutes while the app waits for user input. The caller hears ringing during this time. If there's no input within 5 minutes, the app will disconnect the call as busy. Once the user or program has decided what to do with the call, I'd like to be able to send a new TwiML instructions.
However, when I use the method described here to modify the call, I am given an error by Twilio that the call can't be modified as it's not in-progress.
Is there any way to send new instructions towards the call at this point to have it answered and redirected during the Pause?
You cannot modify the call until you answer it (in-progress), as the error message suggests. You must wait for the <Pause length="300"/> to complete, as it is the first verb in the returned TwiML and the call will remained unanswered until it completes and a TwiML Verb which answers the call is reached.
TwiML™ Voice: Pause

Twilio: how to warn users before call ends aggressively with time limited calls?

So here what i have, when a user A calls my Twilio number, the call is forwarded towards another user B number through a Twiml response which dials user B number. I have done a time limit for any calls which is 5 minutes, but i would like to prevent the two users when there is only 1 minute left that time is nearly up and avoid cutting out aggressively the call.
Any one has an idea how to do that ?
Twilio developer evangelist here.
You could have the users actually call into a <Conference> (generating a call to user B when user A calls and enters the conference) and then start a timer for 4 minutes on your server. When the timer is done you can then check if the conference is still live (by calling the REST API and checking the conference status) and if it is dial one more caller into the conference.
That caller could be a recorded message that you play with <Play> or a message to be read out with text to speech using <Say> that lets the callers know that there's only one minute left.
For example:
<Response>
<Say voice="alice">Warning. There is only one minute of calling time left.</Say>
<Hangup/>
</Response>
Update
This is now better done with announcements that can be played into conferences. Check out the blog post here with details on how to announce messages into a conference.

Recording multiple user answers in Twilio call

I am building an Interactive Voice Assistant using Twilio. My goal is to record parts of the conversation, process the recorded audio and
This is the answer to the /voice webhook (the one will receive Twilio's call)
<Response>
<Play>./welcome</Play>
<Record maxLength="10" action="/processing" recordingStatusCallback="/getRecording"></Record>
</Response>
Processing the audio and providing an answer may take a long time, so I added a Pause at the end of /processing:
<Response>
<Play>./ok</Play>
<Pause length="10"></Pause>
</Response>
This is the answer when finished with /getRecording
<Response>
<Play>./answer</Play>
<Record maxLength="10" action="/processing" recordingStatusCallback="/getRecording "></Record>
</Response>
/welcome, /ok and /answer lead to corresponding audio files.
So I was able to execute all steps, I can check in my logs that /getRecording is actually executed to the end and the twiml sent back again, but the /answer after /getRecording is never executed by Twilio (and the call just ends).
Do you have any guidance for it? Does Twilio accept multiple recordings on the same call?
Note: For some reason, if instead of using the 'recordingStatusCallback' I use /getrecording as 'action' it does work... but then we wouldn't be sure the recording we are using is really generated, right?
Thank you for your help!
Twilio developer evangelist here.
Your /getRecording endpoint is called, however that is an asynchronous webhook in the context of the call. Returning TwiML to the recordingStatusCallback will not affect the current call.
Instead, you should use the REST API to modify the call by redirecting it to the next TwiML you want to execute based on the response to the recording file.
Hopefully the recording does take less than the 10 second pause that you are using, but you may want to add a loop into your /processing endpoint so that the call won't hang up on your caller. You can do this by redirecting the caller back to the start again:
<Response>
<Play>./ok</Play>
<Pause length="10"></Pause>
<Redirect>./processing</Redirect>
</Response>
Then, when you get the recording callback you redirect your caller out of this loop.
Let me know if that helps at all.

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.

Twilio: respond to incoming call by ringing forever

When a Twilio number receives an incoming voice call, what TwiML can I use so that the phone continues ringing indefinitely?
In certain circumstances, I want to pretend that the phone is ringing but there's nobody around to answer it and there's no voicemail configured. I thought the Reject verb could help. It appears to support only a busy signal or a "number disconnected" message.
One can use the Pause verb to have Twilio delay pickup. By specifying a sufficiently long delay, the call effectively rings "forever". For example,
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Pause length="600"/>
<Hangup />
</Response>
Probably the easiest way to do this is going to be to get an mp3 recording of a ringing phone, one that is very long (you can find them by googling) and setup twilio to <Play> the mp3 when someone calls it - I've done it and its very convincing.
Doing this will give you the extra benefit of letting you track who calls that number and how long they let it ring (of course you get billed for this).

Resources