Twilio Make call and send digits multiple times using DTMF - twilio

Scenario: I want to dial a number where an IVR is setup. That has press 1 for English and press 2 for Spanish. Once a number is chosen then it asks for 6 digit key. How I can pass the next 6 digits using DTMF?
I had read the documentation thoroughly and searched on StackOverflow too but didn't got the relevant data.
What I tried is;
$twilio = new Client("AC8b2cc96be2a8dbc059f2908exxxxxxxx", "88db8eeb71124a3effd6c196xxxxxxxx");
$from = "+1289724xxxx";
$to = "+1289670xxxx";
$call = $twilio->calls
->create($to, // to
$from, // from
[
"method" => "GET",
"sendDigits" => "wwwwwwwwww1",
"sendDigits" => "wwwwwwwwwwwwwwwwww123456", // Whithout this line it's working for first digit press
"url" => "http://demo.twilio.com/docs/voice.xml"
]
);
print($call->sid);
I am trying it using PHP Rest API. https://www.twilio.com/docs/voice/make-calls#example-3
I had also read this question: How to send digits in twilio call

You can't send the same parameter twice, instead, you should combine that sendDigits parameter into one string, like this:
$call = $twilio->calls
->create($to, // to
$from, // from
[
"method" => "GET",
"sendDigits" => "wwwwwwwwww1wwwwwwwwwwwwwwwwww123456",
"url" => "http://demo.twilio.com/docs/voice.xml"
]
);
Then it will wait, press 1, wait some more then press 123456.

Related

Twilio outgoing call to recipient with call screening (Google voice)

I have a working service, where we do outgoing calls something like this.
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+14155551212", // to
"+14155551212", // from
["url" => "http://demo.twilio.com/docs/classic.mp3"]
);
print($call->sid);
The issue is when the recipient has call screening, the callback to the URL parameter is done as soon as the screening starts (with callbackstatus "in-progress"!), which means we have to add a pause in the response.
<Response>
<Pause length="10"/>
<Say>
This is an important message
</Say>
<Gather action="https://xxx" method="GET" timeout="15" speechTimeout="auto" numDigits="1" input="dtmf speech">
<Say>Please press 1 followed by the pound sign or say confirm to confirm your appointment</Say>
</Gather>
<Redirect method="GET">https://xxx</Redirect>
</Response>
Is there some way to bypass the screening or have the system not start the response in screening?
EDIT:
I've added the AMD options and it seems to be correctly somewhat working. Right now the only issue that remains is when calling in to Google Voice, when there's call screening, my added voice recording start reading before the recipient actually answers
$call = $client->calls->create(
$to, $from,
array(
"url" => $url,
"statusCallback" => $statusURL,
"statusCallbackMethod" => 'POST',
"machineDetection" => "DetectMessageEnd",
"machineDetectionTimeout" => 5
)
);
You are looking for answering machine detection (AMD).
You can make a call with AMD enabled by setting the MachineDetection parameter to Enable or DetectMessageEnd.
$call = $twilio->calls
->create("+14155551212", // to
"+14155551212", // from
[
"url" => "http://demo.twilio.com/docs/classic.mp3",
"machineDetection" => "Enabled"
]
);
When you use the MachineDetection parameter the request to your url will include an AnsweredBy parameter. From the docs:
Use Enable if you would like Twilio to return an AnsweredBy value as soon as it identifies the called party. This is useful if you would like to take a specific action — for example, connect to an agent, play a message) — for a human but hang up on a machine.
If you would like to leave a voicemail on an answering machine, specify DetectMessageEnd. In this case, Twilio will return an AnsweredBy immediately when a human is detected but for an answering machine, AnsweredBy is returned only once the end of the greeting is reached, usually indicated by a beep.
There is also an option for asynchronous AMD. With asynchronous detection your url is called immediately as if a normal call, but once Twilio has performed the detection an asynchronous webhook is made to a different URL and you can use the callback data to decide whether to update the call.

Record an outbound call in twillio

I am trying to record an outbound call using twillio php code :
Once i execute the code in browser, call will go to destination number [+919999999999].
Issue :
but once it recieved, some default voice will play and call will be disconnected....
Requirement :
But i want both pepoles [ source & destination ] should speak and want to record that conversation....
<?php
require_once '/var/www/html/ecom1/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "account_sid";
$token = "auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+919999999999",
"+918888888888",
array(
"record" => True,
"url" => "http://twimlets.com/forward?PhoneNumber=%2B918888888888&"
)
);
print($call->sid);
?>
I am using Trail account....
Append the parameter "Record=true" when making a POST request to tell Twilio to record an outgoing call via the REST API.
By default, the recording will be single-channel (mono). For dual-channel recordings (two legs of the call each in separate stereo channels), append the parameter "RecordingChannels=dual".
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = '<account_sid value>';
$auth_token = '<auth_token value>';
$client = new Client($account_sid, $auth_token);
$calls = $client->accounts("<account id>")
->calls->create("<Valid To number>", "<Valid From number>", array(
'Method' => "POST",
'Record' => "true",
'RecordingChannels' => "dual"
));
Please Note :
When attempt to initiate an outbound phone call, ensure the URL you specified to handle the call should be valid URL.
If you specified an Application Sid for your outbound phone call, the application must have a valid VoiceUrl or the call will fail.
Hope this information helps you !

How to add dynamic number in Twilio SMS API PHP

How can I add and Send SMS to number dynamically by PHP code.
I have add number from admin panel and send SMS to this static number. It works, but if I want to add number from HTML form. So how can I register number dynamically by PHP code.
$message = $client->messages->create(
'+919711969920', // Text this number
array(
'from' => '(205) 358-4782', // From a valid Twilio number
'body' => "Hello Sudhir"
)
);
Twilio developer evangelist here.
You can absolutely take input from an HTML form. Rather than hard coding the number into your PHP you would need to retrieve the number you want to send the message to from the incoming request parameters.
You could do this with:
$message = $client->messages->create(
$_REQUEST["to"],
array(
'from' => '(205) 358-4782', // From a valid Twilio number
'body' => "Hello Sudhir"
)
);
Make sure your form then has an input field with the name "to" and when you submit the form you will be able to get the parameter from the request with the code above.
<input type="tel" name="to" id="to" />
Let me know if that helps at all.

Unable to transfer twilio call to new number

We have small twilio application which we use to calls any customer number from website. Now we are trying to add transfer call functionality in our application.
But we are unable to transfer call using php api.
Here is what we are using :
We dial number from website, JS returns callid
We have another number on which we need to transfer call after few minutes
So we have "Transfer" button which makes ajax call to php file
which have following code :
<?php
$existing_call_sid = $_REQUEST['CallSid'];
$new_number = $_REQUEST['new_number'];
$call = $client->calls($existing_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST"
)
);
echo $call->to;
?>
transfer_xml_main.php contains :
<Response>
<Dial><?php echo $_GET['new_number'];?></Dial>
<Say>Please be on line we are transferring your call</Say>
</Response>
But when this request happens existing call is dropping and no call to new number.
also $call->to is blank
Am i doing anything wrong?
Update
After implementing answer of philnash i am getting following fatal error :
<b>Fatal error</b>: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to update record: No 'To' number is specified' in /twilo/twillo_php_master_new/Twilio/Version.php:85
Stack trace:
#0 /twilo/twillo_php_master_new/Twilio/Version.php(127): Twilio\Version->exception(Object(Twilio\Http\Response), 'Unable to updat...')
#1 /twilo/twillo_php_master_new/Twilio/Rest/Api/V2010/Account/CallContext.php(109): Twilio\Version->update('POST', '/Accounts/AC618...', Array, Array)
#2 /twilo/twilo_call_transfer.php(26): Twilio\Rest\Api\V2010\Account\CallContext->update(Array)
#3 {main}
thrown in <b>/twilo/twillo_php_master_new/Twilio/Version.php</b> on line <b>85</b><br />
However i am getting parent call id correctly and in $child_calls i am getting to and from correctly which is one who called first and one whom user is called. Still anything wrong ?
And yes we want exactly like you said :
1. User1 (agent) called one number (customer A) from twilio JS Client
2. Now User1 (agent) want to transfer call to another number which can be agent or some other number.
Also there is no errors in debugger
Twilio developer evangelist here.
I'm surprised that no call is happening. Are there any errors in your Twilio debugger?
I can see some issues though.
The CallSid you get from the JS when you dial out is the Sid of the dialling leg, the one owned by your agent. But, I'm guessing you want to transfer the person that is called to a new number. If that is the case then you need to get the Sid of the receiving leg of the call. The dialling Sid is the parent Sid for the calls, so you can look up the other leg by listing child legs, like this:
<?php
$parent_call_sid = $_REQUEST['CallSid'];
$child_calls = $this->client->calls->read(array("ParentCallSid" => $parent_call_sid));
$child_call_sid = $childCalls[0]->sid;
$new_number = $_REQUEST['new_number'];
$call = $client->calls($child_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST"
)
);
echo $call->to;
?>
Secondly, your TwiML you return is the wrong way round. If you want the message to come before you start dialling, then you need to put the <Say> first.
<Response>
<Say>Please be on line we are transferring your call</Say>
<Dial><?php echo $_GET['new_number'];?></Dial>
</Response>
When you do all of this, your original dialler will get cut off as the call is transferred. You may want this, though you may find that a warm transfer is a better experience. There is a good tutorial on how to perform a warm transfer using PHP and Laravel which might help.
Let me know if that helps at all.
In your PHP you use the POST method, but in your XML you use $_GET. You're not passing the variables.
<?php
$parent_call_sid = $_REQUEST['CallSid'];
$child_calls = $this->client->calls->read(array("ParentCallSid" => $parent_call_sid));
$child_call_sid = $childCalls[0]->sid;
$new_number = $_REQUEST['new_number'];
$call = $client->calls($child_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST" // <---NOTICE THE POST
)
);
echo $call->to;
?>
Change $_GET to $_POST
<Response>
<Dial><?php echo $_POST['new_number'];?></Dial>
<Say>Please be on line we are transferring your call</Say>
</Response>

Need dialer with call transfer after answer

I'm looking for a twimlet or php script for the Twilio API that would implement an outbound calling list function with optional transfer to recorded message -
Sales person Clicks number in a list to dial outbound call
dialed party answers
Sales person realizes it's a machine and clicks a link to transfer the call to a recorded message that will be left on the machine.
Or, if it's not a machine sales person proceeds with the call.
Is something like that available already canned or scripted?
Since the initial ask we've updated the click-to-call solution in a code complete tutorial.
In the last step, after a call is connected and Twilio asks for TwiML instructions you might consider adapting from the call screening tutorial to handle voicemail:
public function agentVoicemail(Request $request, $agentId)
{
$response = new Services_Twilio_Twiml;
$callStatus = $request->input('DialCallStatus');
if ($callStatus !== 'completed') {
$response->say(
'It appears that no agent is available. ' .
'Please leave a message after the beep',
['voice' => 'alice', 'language' => 'en-GB']
);
$response->record(
['maxLength' => '20',
'method' => 'GET',
'action' => route('hangup', [], false),
'transcribeCallback' => route(
'store-recording', ['agent' => $agentId], false
)
]
);
$response->say(
'No recording received. Goodbye',
['voice' => 'alice', 'language' => 'en-GB']
);
$response->hangup();
return $response;
}
return "Ok";
}

Resources