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).
Related
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.
I have the following code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/simple_gather.xml -->
<Response>
<Pause length="2"/> <Play>https://welcomehisheart.com/wp-content/uploads/2021/10/congress-invitation.mp3</Play>
<Pause length="1"/>
<Say>If you would no longer like to receive information about the Sacred Heart, press 2</Say>
<Gather/>
<Pause length="1"/>
</Response>
The TwiML URL is:
https://handler.twilio.com/twiml/EHe23193a659bfcf74b1061864aea9b224
The code works as expected. You can enter a selection during the phone call.
How to access the information gathered?
Thanks
Twilio developer evangelist here.
It looks like you are working with a TwiML Bin there, which is great for a static piece of TwiML like this first message. However, there are a couple of issues.
Firstly, you are not giving the user the proper time to enter their input. The <Gather> element is best used with the message nested within it, so that a user can press at any time. You can also set a timeout to better control how long they have to respond once the nested <Say> is complete. The default value of timeout is 5 seconds.
Secondly, if you are just waiting for the user to press a single digit, you can add the numDigits="1" attribute to the <Gather>. This will complete the <Gather> once the user presses a single digit.
Finally, and the subject of your question, you need to give the <Gather> a URL as the action attribute. Then, when a user presses a key, Twilio will make an HTTP request to that URL with the results of their input. You need to build an application that will handle that request and do something with the result of the key press.
So, you should update your TwiML to:
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/simple_gather.xml -->
<Response>
<Pause length="2"/>
<Play>https://welcomehisheart.com/wp-content/uploads/2021/10/congress-invitation.mp3</Play>
<Pause length="1"/>
<Gather numDigits="1" action="https://example.com/gather">
<Say>If you would no longer like to receive information about the Sacred Heart, press 2</Say>
</Gather>
</Response>
and you need to create an application that can receive an HTTP request, in this case at the URL example.com/gather, though you should provide your own URL here.
There are tutorials on how to gather user input in a phone call that will walk through this in more depth with code examples, that you should read next.
<?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
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.
So here's my issue:
I initiate a Twilio call via the API
When the call is picked up, Twilio reaches out to my Twiml server and gets a response containing the following (notice the "record" parameter of the Dial verb):
<Dial callerId="555-555-5555" record="record-from-ringing">
<Number statusBallbackEvent="completed" statusCallbackMethod="POST" statusCallback="https://myCallback.com">
555-555-5556
</Number>
</Dial>
The call gets placed and is recorded correctly, but the value of Call Sid for the recording is always the Sid of the parent call. This can be problematic if I use multiple Dial verbs during the course of one call.
Is there a way to figure out which child call initiated the recording?
To grab your child call SID, I'd recommend using an action URL in the verb that originates it. A request to this action URL would return the 'DialCallSid' parameter in Twilio's response:
https://www.twilio.com/docs/api/twiml/dial#attributes-action-parameters
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/dial_callstatus.xml -->
<Response>
<Dial action="/handleDialCallStatus.php" method="GET">
415-123-4567
</Dial>
<Say>I am unreachable</Say>
</Response>
You could then store or use that SID however you would like.
Please let me know if this helps at all.