Twilio API: Transferring connection to a forwarded number? - twilio

Im placing an outbound call, and based on the automated message from whom Twilio is calling, it calls my phone with the connection.
Here is the script that places the phone call.
import os
from twilio.rest import Client
account_sid = "xxxx"
auth_token = "xxxx"
client = Client(account_sid, auth_token)
call = client.calls.create( url='http://myhost.com/rec.php',to='+1234',from_='+9876')
print(call.sid)
The phone call is placed, and the action script sends a TwiML gather response. Here's rec.php
echo '<Response>
<Gather input="speech"
partialResultCallback="http://myhost.com/partial.php"
action="http://myhost.com/finalresult.php">
</Gather>
</Response>
I've got the partial page logging the text. But when I forward a call, my phone rings for one second, then disconnects. There's no errors in the debugger either.
Here's partial.php
if(contains("To continue in English", $_REQUEST['UnstableSpeechResult'])){
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Dial>+myPhoneNumber</Dial>
</Response>";
}
here is final result, which i think is called when the phone call is complete? im not sure.
if(contains("To continue in English", $_POST['SpeechResult'])){
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Dial>+myPhoneNumber</Dial>
</Response>";
}

According to the documentation, partialResultCallback doesn't process TwiML.
The webhooks Twilio makes to your partialResultCallback are
asynchronous. They do not accept any TwiML in response. If you want to
take more actions based on this partial result, you need to use the
REST API to modify the call.
Not sure how your phone is ringing given the above. Any more details you can provide will help troubleshoot the issue.

Related

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.

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.

Twilio initiate outbound call that connects agent phone before dialing target number

I want to create a help desk web page in which an agent can click a link to initiate an outbound call to a target number. I understand how to use the Web Client to make that happen, but for an agent who doesn't have bandwidth to support VoIP, I'd like Twilio to call the agent's phone number then dial the target number.
The experience would be much like using Google Voice with Google Chat/Hangout client -- Google Voice calls your number/client, then initiates a call to the target.
Also, if both agent and target phone numbers are domestic landlines, would this scenario incur 2X the per minute landline fees?
I'm not looking for code necessarily, but rather an answer based on Twilio APIs and Twiml concepts.
Twilio evangelist here.
Sounds like you are looking to create "Click to Call". Here is some code from our docs that shows how to do this:
https://www.twilio.com/docs/howto/click-to-call
The basics are:
Use the REST API to initiate an outbound call. When that call is answered Twilio is going to make an HTTP request to some URL that you told about in your initial REST request. That URL's job is to return TwiML that contains the <Dial> verb which tells Twilio to dial the second phone number and bridge the two call legs together.
For domestic US calls, the total cost is going to be 4 cents / minute. 2 cents per each leg, since each leg is considered outbound. See Example 4 on this page:
https://www.twilio.com/help/faq/voice/how-much-am-i-charged-for-call-forwarding
Hope that helps.
Simple/Direct Twilio Calls Agent->Call
original url: https://www.twilio.com/docs/quickstart/php/rest/call-request#call-end-callback
First file loaded from browser:
use Twilio\Rest\Client;
// Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "SID";
$AuthToken = "AuthTok";
// Step 3: Instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
"+12125551111",// connect this number(Agent)
// that you've purchased or verified with Twilio.
"+12135554646",// caller id for call
// Set the URL Twilio will request when the call is answered.
array("url" => "http://example.com/call_them.php")
);
echo "Started call: " . $call->sid;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
call_them.php:
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
//inside dial.. actual number you want to reach
?>
<Response>
<Dial>+18185556363</Dial>
</Response>
Thank you for the great answer, #user3229526, it worked like a charm.
In order to unhardcode the number to call, just append the number you wish to call as a URL parameter in the Twilio Requst URL
array("url" => "http://example.com/call_them.php?number=1234567890")
And edit call_them.php to accept that parameter
<Response>
<Dial>
<?php echo '+1'. $_GET['number']; ?> // +1 for country code
</Dial>
</Response>

Resources