simulring and collect digits from dialed number to accept call - twilio

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>
<Dial>
<Number>+1800XXXXXXX</Number>
<Number>+1912XXXXXXX</Number>
</Dial>
</Response>
The problem is I want to have one of the destination parties press a digit ( like "1" for example ) before twilio actually bridges the call to them.
I've looked at but that only seems to get digits from the caller, not the callee.

Twilio developer evangelist here.
To have the recipient of a call started by a <Number> perform an action, you need to provide a URL as the url attribute of the <Number>. When the call connects, Twilio will make a webhook request to that URL and you can return TwiML from the URL. Within that TwiML you can include a <Gather> to have the recipient enter a digit before you connect the call. See the documentation for the url attribute for <Number> for more detail.

Related

How to repeat values entered DTMF

I am trying to write some TwiML where the user will enter the phone number through keypad and I want to respond back and repeat the number they entered.
<Response>
<Gather input="speech dtmf" finishOnKey="#" timeout="4" numDigits="5" action="https://handler.twilio.com/twiml/EHb45a578e1b6a7a33187bb7e72f721dd1" method="GET">
<Say>Please enter your number</Say>
</Gather>
<Say>You entered: {Digits}</Say>
</Response>
Twilio developer evangelist here.
When you use <Gather> you set an action attribute to a URL. Once the <Gather> has recorded the user's response it will make a new HTTP request to that action URL and it will not continue the current TwiML. So, in your example, the last <Say> won't be used.
It looks like you have set your action URL to a TwiML Bin. With TwiML Bins you can do some template interpolation based on the incoming request parameters. The parameter containing the digits the user pressed will be Digits.
So, what you need is a new TwiML Bin that just has the second <Say> from your example and use that TwiML Bin as the action URL.
<Response>
<Say>You entered: {Digits}</Say>
</Response>
If you want to move beyond TwiML Bins, then you will need to write an application that can receive the webhook requests from Twilio, parse the response and read the Digits parameter (or the SpeechResult parameter, as you have "speech" in your input attribute too).

Twilio call forwarding one after another - how to disconnect if person picks up the call

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say> Connecting call to Pinto </Say>
<Dial record ="record-from-answer" timeout="10" hangupOnStar ="true">
<Number>XXXXXXXX</Number>
</Dial>
<Say> Pinto is not picking up the call, now connecting call to Management </Say>
<Dial record ="record-from-answer" timeout="10" hangupOnStar ="true">
<Number>XXXXXXXX</Number>
</Dial>
<Say> No one is picking up right now. Please text us at +12022171828 </Say>
</Response>
Above is the call forwarding flow for one after another.
What I'm looking here is,
If 1st user attended the call, then it should not initiate the call to 2nd number and also it should not say text message which is at last line
If 1st user disconnected, then redirect call to 2nd number - if 2nd number not picking up the call then it should say text message which is at last line
if 1st user disconnected, then redirect call to 2nd number - if 2nd number picked up the call then it should not say text message which is at last line
Also I want to Implement Transcribe & Transcribe call back by using TwiML.
So, please help me like how we need to do with that?
To carry out this flow, you cannot perform the entire thing in one TwiML response. Instead, you will need to provide the <Dial> element with URL in the action attribute so that once the first call completes, Twilio makes a webhook to find out what to do next.
So, your first TwiML response should look like:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say> Connecting call to Pinto </Say>
<Dial record="record-from-answer" timeout="10" hangupOnStar="true" action="/complete">
<Number>XXXXXXXX</Number>
</Dial>
</Response>
Above I've added action="/complete" to the <Dial> and this means we need an application to be able to respond to HTTP requests to /complete as well. We also need this to be an application and not a static response as we need to work out whether the call was answered or not and decide on what to do next and what TwiML to respond with.
We can do this because one of the parameters that Twilio sends to the action URL is DialCallStatus. This parameter can be "completed", "busy", "no-answer", "failed" or "canceled" and this tells you what happened in that first call.
So, in your case, you will want to check the DialCallStatus and if it is "completed" then you do not need to return the next <Say> and <Dial>. If, however, the DialCallStatus is one of the other statuses, you do want to return the next <Say> and <Dial>. That <Dial> should include another action URL that makes the same choice at the end of the second <Dial>.
I'm not sure what language you are building with, but a pseudo-code idea of this would look like this:
post '/complete' do
if params["DialCallStatus"] != "completed"
return "<Response><Hangup/></Response>"
else
return "<Response><Say>Pinto is not picking up the call, now connecting call to Management</Say><Dial ....></Response>"
end
end

Call an URL for get phone number to forward the Twilio call using TwiML

I'm trying to automate call forwarding using Twilio. when the user calls the Twilio number it will play some welcome message then it should call an external API, The API will return a phone number the Twilio should call the number. I'm trying to do with this TwiML. here is my TwiML bin document
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="1"/>
<Say voice="Polly.Joanna"> Welcome
</Say>
<Dial> HERE THE NUMBER SHOULD COME THAT API WILL RETURN
</Dial>
</Response>
for example https://getmynumber/samplenumber is the URL that will return a number. How can I achieve this with TwiML?
and is it possible to define a variable inside TwiML? because if i can save the number to that variable using the <Redirect> tag I can achieve this easily.
is it possible?
That'll be difficult with only TwiML Bin but you could try something like:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="1"/>
<Say voice="Polly.Joanna">Welcome</Say>
<Dial>{{Number}}</Dial>
</Response>
And then call it via: https://<your TwiML Bin URL>/...?Number=+123456789
This passes custom values into a TwiML Bin, here Number.
If you really need to call an API to get the number to dial you'll need some kind of endpoint to form the TwiML, e.g. a Twilio Function, or any other endpoint.
A version in Python could look like this:
from twilio.twiml.voice_response import VoiceResponse
response = VoiceResponse()
response.pause(length=1)
response.say('Welcome')
number = '+123456789' # Here you would do an API call
response.dial(number)
print(response)
The above basically creates the TwiML you have in your TwiML Bin but with Python. You would need to wrap this in an endpoint to be able to be called and returned. And of course you would need to add your logic to retrieve the number via the API.

How can I handle reaching voicemail using Twilio's <dial> verb

I know that on making a call Twilio can detect an answering machine, and react differently.
However if I use the <dial> verb, there's no obvious place to add this feature, even though it's essentially the same thing.
My intended flow is:
Customer enters their phone number
Twilio calls Customer and plays a voice message
Twilio dials an agent number, likely a mobile
If the Agent picks up, connect the customer to the agent
If the Agent busies the call or does not answer, call will likely go to Agent's voicemail.
Terminate call to Agent
Record voicemail from Customer
Email voicemail to Agent
From the official docs on the <Dial> verb (emphasis mine):
This is the simplest case for Dial. Twilio will dial 415-123-4567. If someone answers, Twilio will connect the caller to the called party. If the caller hangs up, the Twilio session ends. If the line is busy, if there is no answer, or if the called party hangs up, <Dial> exits and the <Say> verb is executed for the caller before the call flow ends.
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/simple_dial.xml -->
<Response>
<Dial>415-123-4567</Dial>
<Say>Goodbye</Say>
</Response>
Placing a <Record> verb after the <Say> verb sounds like what you are looking for. You can change the timeout from the default value of 30s like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial timeout="9001">415-123-4567</Dial>
<Say>Please leave a message</Say>
<Record action="/process_new_voicemail" />
</Response>
I am sure this is late, but hopefully it helps some one out. It sounds like you may just need to screen the call.
Basically, you can ask the "agent" that you dialed to accept the call and hang up if you do not receive input.
I am not sure what language you are using, but here is a great php/Laravel tutorial to explain:
https://www.twilio.com/docs/tutorials/walkthrough/ivr-screening/php/laravel
The key part is here:
$dialCommand = $response->dial(
['action' => route('agent-voicemail', ['agent' => $agent->id], false),
'method' => 'POST']
);
$dialCommand->number(
$numberToDial,
['url' => route('screen-call', [], false)]
);
Notice that the dial command uses the 'action' to specify a location that is sent a POST request should the call end i.e POST to /agent-voicemail.
Then, the number is dialed along with a 'url' parameter this is the location that will be requested after the agent has picked up but before connecting the two parties.
The /screen-call route then asks the agent to accept the call, if no input is received it hangs up and will make a POST request to the initial setup /agent-voicemail route.
This method will handle your scenario because if it goes to voicemail no input will be received and the call will end.

Gather after caller hangs up

Is it possible to play a recording and then gather digits from the receiver of a call after a call ends (the caller hangs up)?
Similar to: https://www.twilio.com/docs/api/twiml/gather just that it's the receiver and not the caller who ask and that it happens when the call ends.
Thanks!
Morten
Twilio Evangelist here. Thanks for the clarification.
You can do this, but you need to use a conference call. Imagine Alice is calling Jack. Normally, you would use TwiML like this:
<Response>
<Dial>
+15551234567
</Dial>
</Response>
What you need to do is dial Alice into a conference call:
<Response>
<Dial>
<Conference>Alice and Jack</Conference>
</Dial>
</Response>
Then you need to make an outbound API call to dial Jack into the conference. In Ruby this would be something like this:
require 'twilio-ruby'
client = Twilio::REST::Client.new "account-sid", "account-token"
cient.account.calls.create to: "+15551234567", from: "some-twilio-number", url: "Your app URL"
This will use the same TwiML as above to join Jack into the conference. You'll need to look at the details for a <Conference> to avoid/select holding music, and to prevent the conference ending when Alice hangs up. Something like this:
<Response>
<Dial>
<Conference endConferenceOnExit="false" beep="false" waitUrl="http://some-twilml-no-hold-music">
Alice and Jack
</Conference>
</Dial>
</Response>
Now, you will also need to use the callbacks for when the call ends (you should also set this on your Twilio Number in the Dashboard, or through the API.
Now, you can take the Call SID of whoever remains in the conference call (you'll get this when you receive the initial request for the TwiML), and use the REST API to modify that call, redirecting them to new TwiML:
require 'twilio-ruby'
client = Twilio::REST::Client.new "account-sid", "account-token"
call = client.account.calls.get "my-call-sid"
call.redirect_to url: "http://new-url-of-twiml", method: "POST"
This allows you to redirect the call to TwiML such as:
<Response>
<Gather action="http://some-url-to-send-digits-to">
<Play>http://some-mp3-or-wav-file</Play>
</Gather>
</Response>
Twilio will play the MP3/WAV file, while listening for any key (DTMF) tones the user enters. You'll need to configure your <Gather> depending on how many keys you want to listen for etc. But that's very straight forward.
Hope this helps!

Resources