<play> No matter if it is Human or Machine Answered - twilio

I want Twilio to dial a service that needs to provide an extension before it can leave a message.
ifMachine seems to wait for the "beep" , however there is a welcome message as soon as the call is placed:
- Call Placed (Initiated + Answered)
- The recipient service plays : enter the extension of the mailbox you want to reach
- Twilio plays the digits of the extension
- Another message explanation message (about 20sec) is played
- Then the beep comes and twilio is suppose to play the mp3 to leave it in the mailbox.
I tried all day to get this to work.
Here is the php:
$call = $client->account->calls->create('+18777777777', '5555555555', 'http://domain.com/play.xml', array(
'Method' => 'GET',
'FallbackMethod' => 'GET',
'StatusCallbackMethod' => 'GET',
'IfMachine' => 'Continue',
'Record' => 'false',
));
Here is the xml:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play digits="ww1900"></Play>
<Pause length="25"/>
<Play>http://domain.com/18.mp3</Play>
</Response>
The call is placed, but lasts only 5 seconds, twilio does not continue.

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.

How can I capture a dtmf code on for a call sent out of twilio?

I send calls out by building an html file that contains twiml markup, and use the php lib to place the call to the outgoing number (see e.g.)
$tw_call = $twilio_client->calls->create(
"+1".$recipient['address'], "+1".$org['twilio_number'],
array(
'Url' => VOICE_CALL_LINK.'/'.$file, (this contains the SAY verbs and text)
'Timeout' => '30',
'StatusCallback' => CALLBACK_LINK.'/voice_call_handler.php',
'StatusCallbackEvent' => array('initiated', 'ringing', 'answered', 'completed')
)
I want to know if it is possible to record a dtmf code from the call recipient via the method I am using for placing the call?
Can an additional callback url be placed in the text file? If so how would I capture which call the was coming back? Would the call sid be available to the possible callback url within the text file?
Ok I must be missing something. I tried the following:
<Response>
<Pause length='1'/>
<Say voice='alice'>$intro</Say>
<Pause length='1'/>
<Say voice='alice'>$msg_body</Say>
<Pause length='1'/>
<Gather action='absolute html path' numDigits='1'>
<Say Please respond by selecting 1 for I can come. Select 2 for I cannot come.</Say>
</Gather>
</Response>";
I get back from Twilio "an application error has occurred". If I remove the Gather tags and the Say tag within the Gather tags, I receive a perfect call.
Same error occurs if I leave the tags and remove the action and path.
Can you gather responses on outbound calls? I ask because all twilio docs refer to inbound call.
Twilio developer evangelist here.
In order to capture DTMF tones from a call you can use the <Gather> TwiML verb. This would probably go in the file which contains your <Say> that you point to in the code above. <Say> can be nested within <Gather> in order to allow you to ask the user for input and start taking it as soon as they start typing.
The TwiML might look like this:
<Response>
<Gather action="/gather_result.php" numDigits="1">
<Say>Welcome to the free beer hotline, dial 1 for free beer, dial 2 for other beverages.</Say>
</Gather>
</Response>
Then, when the user dials the number (you can control how many numbers with the numDigits attribute) Twilio will make a request to the URL in the action attribute. Within that request will be a Digits parameter which will contain the numbers the user pressed. The call SID would also be among the parameters.
Let me know if that helps at all.
I had similar issue where Gather TwiML was not capturing the user dtmf input from a call sent out of twilio. For some reasons, it failed to capture my input digit. I did press 1#, but the voice message keep playing and repeating the same message. Sometimes it works and twilio able to get the digit that I inputted, but more than 80% of the times I tried, it failed to capture the inputted digit. Below is the TwiML in node js looks like:
var promise = new Parse.Promise();
twilioClient.calls.create({
to: phoneNumber,
from:'+6598124124',
url: hosturl + '/gather_user_dial',
body: callParam,
statusCallback: hosturl + '/callback_user',
statusCallbackMethod: 'POST',
statusCallbackEvent: ["completed", "busy", "no-answer", "canceled", "failed"]
}).then(function(call) {
if (res) res.success(call);
promise.resolve(call);
}, function(error) {
console.error('Call failed! Reason: ' + error.message);
if (res) res.error(error);
promise.reject(error);
});
app.post('/gather_user_dial', (request, response) => {
const twiml = new VoiceResponse();
const gather = twiml.gather({
numDigits: 1,
timeout: 5,
actionOnEmptyResult: true,
action: '/gather',
});
gather.say('You are receiving a call from company A because you press the emergency button. Press 1 if you are okay or Press 9 if you need help, followed by the pound sign.');
twiml.redirect('/gather_user_dial');
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/gather', (request, response) => {
const twiml = new VoiceResponse();
if (request.body.Digits) {
switch (request.body.Digits) {
case '1':
twiml.say('User has been notified!');
userPressOne(request.body.Called);
break;
case '9':
twiml.say('User has been notified!');
userPressNine(request.body.Called);
break;
default:
twiml.say("Sorry, I don't understand that choice.").pause();
twiml.redirect('/gather_user_dial');
break;
}
} else {
twiml.redirect('/gather_user_dial');
}
response.type('text/xml');
response.send(twiml.toString());
});

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>

Difference between CallTo/To and CallFrom/From in Twilio

When I make an outgoing call using Twilio API & TwiML App Twilio makes request to my server with following params:
'AccountSid' => '...',
'ApplicationSid' => '...',
'Caller' => 'client:2',
'CallStatus' => 'ringing',
'callFrom' => 'some_phone_number',
'Called' => '',
'To' => '',
'callTo' => 'some_phone_numeber',
'CallSid' => '...',
'From' => 'client:2',
'Direction' => 'inbound',
'ApiVersion' => '2010-04-01',
what's the difference between CallTo (which equals the actual phone number I'm calling to) and To (which is empty for some reason) ? Same for From and CallFrom. I couldn't find any info in the docs about callTo/callFrom. Plus, why To is empty?
Update
Some additional info. I make a call from twilio number to real Russian phone number. I actually hangup the call. There is no forwarding/redirecting. The Twiml app for outgoing calls is:
<Response>
<Dial callerId="{{ $callFrom }}"
record="record-from-ringing"
action="{{ $action }}">
<Number>{{ $callTo }}</Number>
</Dial>
</Response>
CallFrom and CallTo equals those which are passed with the request. So the data I mentioned above is what Twilio passes with the request, and then Twilio gets the TwiML instructions.
What I do not understand if that what's the difference between to/from? Are from/to fields used when dealing with client names, like for internal conversation or kind of...?
And another question is: when the call is finished and the action url called, twilio passes DialCallStatus and CallStatus, what's the difference there? I need to store call status so that I know if the client was busy and didn't answer, but DialCallStatus has different value from CallStatus.
Thanks

Pass Inbound call between twilio clients

i have several twilio client users
Sky - Kevin - Paul
A twilio registered number connects to callsky.xml by default / that code looking like.
<Response>
<Dial>
<Agent>sky</Agent>
</Dial>
</Response>
a call comes in and goes to sky, she then wants to pass that call over to Kevin or Paul.
how is this possible ?
tried:
$caid = $_REQUEST["CallSID"];
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->get($caid);
$url = 'http://thesite.com/twiml.xml';
$call->update(
'Url' => $url,
'Method' => "GET",
);
This is executed through Ajax, when sky presses a button with the destination users name on it.
<button onclick="divert('kevin')">Kevin</button>
Also: Oddly, the update command shows as a syntax error in my IDE.
Twilio evangelist here.
The update method takes an array, so that might be causing the syntax error:
https://www.twilio.com/docs/api/rest/change-call-state
I'd also recommened checking out the AppMonitor (https://www.twilio.com/user/account/developer-tools/app-monitor) to see if Twilio is logging and errors when trying to retreive the TwiML from your URL.
The URL you point the call at can return static XML, or you ca ndynamically generate it using the PHP helper library. Here is an example of using the <Dial> verb to dial a Client instance:
$response = new Services_Twilio_Twiml;
$dial = $response->dial(NULL, array(
'callerId' => '+14152223333'
));
$dial->client('client-id');
print $response;
This will output TwiML that looks like:
<Response>
<Dial callerId="+14152223333">
<Client>client-id</Client>
</Dial>
</Response>
Hope that helps.

Resources