Twilio api play message before call hangup - twilio

I am hanging up a call like this(java):
Call.updater('somesid').setStatus(Call.UpdateStatus.COMPLETED).update();
I would like to play a message(not via some audio file, but via the twilio 'say' verb) before this call is ended.
How do I do it if its possible?

Twilio developer evangelist here.
Instead of just completing the call with the API request, you can redirect the call to some new TwiML instead. To do so, you need a new URL to send the call to and you update it like this:
Call.updater(callSid)
.setMethod(HttpMethod.POST)
.setUrl(URI.create(newUrl))
.update();
The new URL should then return TwiML that <Say>s the message you want and then hangs up with <Hangup/>. Like this:
<Response>
<Say voice="alice">Sorry, I have to hang up now.</Say>
<Hangup/>
</Response>

Related

Twilio - Taskrouter - Whisper during reservation - while ringing agent [duplicate]

I have two questions regarding TaskRouter.
How do I record a call when using TaskRouter?
How do I whisper to the worker when a call is bridged with caller?
Twilio developer evangelist here.
When you receive an assignment callback and you connect the caller with an agent using the call or dequeue instruction, you can send back further attributes in the response. To record the call, send the attribute record with the value record-from-answer to record the entire call.
To whisper to a worker you need to use the call instruction in your assignment callback. You can then supply a URL that will get called when the agent picks up the call before bridging the calls together which you can use as a whisper.
So, for example:
<?php
$reservationSid = $_REQUEST['ReservationSid'];
header('Content-Type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>You have a new call!</Say>
<Dial>
<Queue reservationSid="<?php echo $reservationSid ?>"></Queue>
</Dial>
</Response>
See the documentation for initiating a call in the assignment callback for more information.

Get response (TwiMl) Url to connect to conference call

The problem I'm trying to solve is having a Twilio phone number connect to a TwiMl, created within my PHP code, while making an outbound call with the end result having the caller and callee in the conference room together.
To set up the TwiMl I coded:
$response = '<Response>
<Play>%s</Play>
<Dial timeout=\'60\' callerId=\'%s\'>
<Conference startConferenceOnEnter="true" endConferenceOnExit="true">
conf-test
</Conference>
</Dial>
</Response>';
And to connect the callee to the call, I'm trying to use:
$client->account->calls->create(
+1XXXXXXXXXX, //To
+1XXXXXXXXXX, //From
array(
"url" => ???));
How do I get the URL from the above TwiMl into the url to connect further participants as well as the initial callee? Is this even possible, or am I forced to use a TwiMl bin and use that URL?
You can pass in TwiML using the below approach,
Pass TwiML with Call Initiation Requests
https://www.twilio.com/changelog/pass-twiml-call-initiation-requests

Twilio TaskRouter - Whisper and recording

I have two questions regarding TaskRouter.
How do I record a call when using TaskRouter?
How do I whisper to the worker when a call is bridged with caller?
Twilio developer evangelist here.
When you receive an assignment callback and you connect the caller with an agent using the call or dequeue instruction, you can send back further attributes in the response. To record the call, send the attribute record with the value record-from-answer to record the entire call.
To whisper to a worker you need to use the call instruction in your assignment callback. You can then supply a URL that will get called when the agent picks up the call before bridging the calls together which you can use as a whisper.
So, for example:
<?php
$reservationSid = $_REQUEST['ReservationSid'];
header('Content-Type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>You have a new call!</Say>
<Dial>
<Queue reservationSid="<?php echo $reservationSid ?>"></Queue>
</Dial>
</Response>
See the documentation for initiating a call in the assignment callback for more information.

Twilio, How to transfer a in-progress call to another number

How to transfer a in-progress call to another number.The concept that I m using is to use the update method when the call is in in-progress and dial the number that I wanted To connect and It is working but the connection with the first caller is breaking/
Code for the process of transferring call-
1.process for dialing call-
<Response>
<Dial callerId="callerid">
<Number statusCallbackEvent="initiated ringing answered completed" statusCallback="urltohadlestatus">user_number</Number>
</Dial>
</Response>
2. process to process to transfer the call-
I have used the update method to transfer the call.
function update_call1($CallSid, $admin_no) {
$rr = array(
"url" => "trurl?admin_no=".$admin_no,
"method" => "POST"
);
$call = $this->client->calls($CallSid)->update($rr);
return $call->to;
}
and used this TwiML
<Response>
<Dial>admin_number_call_to_be_transfered</Dial>
</Response>
what this does is transfer the call but when admin receives it,It disconnects the call.
And what I need like when jack make call to jenny and now jack want to transfer the call to jhonny and when call is transferred to jhonny, jack shound be disconnected from the call.
Twilio developer evangelist here.
You have two options here. Once the call is transferred away, the other caller will drop if it has nothing else to do. There are two ways you can achieve this.
You can either put the callers in a <Conference>. Then when the caller is transferred the other call remains in the conference room. There is a good tutorial on warm transfers using this technique, which might help.
Alternatively, if the side of the call that is dropping out right now is the one that generated the call from the Twilio REST API you can add more TwiML below the <Dial> verb to have the call continue. For example:
<Response>
<Dial>OTHER_NUMBER</Dial>
<Say loop="0">You are still on the call.</Say>
</Response>
Will just keep saying "You are still on the call" once the other end is transferred away.
You can also achieve this with the action attribute for <Dial>. Using the action attribute means that Twilio will make a webhook request to the URL you specify and use the TwiML from that response to carry on the call.

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