Dial a Number Twilio Number to Trigger TWILM Bin - twilio

We are attempting to create a workflow that will ultimately connect a lead from a contact form, with a business owner.
The workflow is as follows:
1) Lead fills in contact form
2) Using Stamplay integration with Unbounce, the lead receives a text asking them if they wanted to be contacted "Now", or "Later".
Let's go with lead says "Now"
3) Lead says "Now", which will access a webhook URL to decide on what to do next.
In this particular case, saying "Now", will trigger a TWIML bin to dial the business owner. If the business owner doesn't pick up/busy, then we send a text to the lead asking them to send a follow-up text with a 'name' and 'date/time'.
4) The lead replies with a text with this information, and then both the business owner and lead receive separate notifications about the appointment.
I have been able to successfully go through this whole workflow when a user directly dials the Twilio number (not programatically with keywords yet, which is where I need help).
When a call comes in -> TWIML bin
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="4"/>
<Say>Please hold, while I connect your call.</Say>
<Pause length="4"/>
<Dial timeout="10"> business owner number </Dial>
<Pause length="4"/>
<Sms>I am currently unavailable. If you'd like me to get in touch, pls reply back with your name, and a time that would work best for you. Thanks, Adam</Sms>
</Response>
When a SMS is received -> webhook URL
<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'xyz';
$token = 'xyz';
$client = new Client($sid, $token);
$number = $_POST['From'];
$body = $_POST['Body'];
//Sends a message to the owner
$sms = $client->account->messages->create(
// Cell of owner
'12345',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Lead's reply sent to owner asNotification
'body' => "Hi <name>. You have a new lead. The information for this lead is: $body. You can contact them at $number"
)
);
//Sends a message to the lead
$sms = $client->account->messages->create(
// Cell of Lead
$number,
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Notification Message sent to Lead
'body' => "This is a confirmation that I have received your information, and will be in contact with you soon. Thanks, <name>."
)
);
Where I am encountering issues is having the lead text "Now", to trigger a phone call between the business owner and lead.
This is the code that I have been attempting to use, except that I have been receiving 11200- HTTP retrieval failure non-stop. I have also attempted to use $client->account->calls->create, as that's what I used to successfully send messages.
// Read TwiML at this URL when a call connects (attempt to connect to owner)
$call = $client->calls->create(
'lead-number', // Call this number
'78900', // From a valid Twilio number
array(
'url' => TWIML Bin of Interest
)
);
Anyone have any idea what I could do?

Check out the example of creating a dynamic response:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Twiml;
$response = new Twiml;
$body = $_REQUEST['Body'];
if( $body == 'hello' ){
$response->message('Hi!');
}else if( $body == 'bye' ){
$response->message('Goodbye');
}
print $response;
In your case you'll modify for if "now" is in the $body you can create the call which your code looks fine for.
$call = $client->calls->create(
"+1415XXXXXXX", "+1415XXXXXXX",
array("url" => "link_to_twiml_bin")
);
In regards to the 11200 HTTP retrieval error, take a look at the possible solutions here especially:
Make sure your web server allows HTTP POST requests to static
resources (if the URL refers to .xml or .html files)

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 to make a customable message in Twilio Voice call

i've successfully make a voice call with twilio like this:
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => "http://demo.twilio.com/docs/voice.xml")
);
the above code will call to the $toNumber with twilio demo message, when i try to use TwiML to create the dynamic message like this:
$response = new TwiML();
$response->say('Votre otp est 2234', ['voice' => 'woman', 'language' => 'fr']);
$call = $client->calls->create(
$toNumber, $fromNumber,
$response
);
i got an error like this:
[HTTP 400] Unable to create record: Url parameter is required. For
more information, see http://www.twilio.com/docs/api/twiml
Twilio developer evangelist here.
You can't send the TwiML to Twilio when you make a call like that. You need to send a URL that Twilio will send an HTTP request to when the call connects (like in the original example). So, to fix your code you should replace the demo URL with your own URL.
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => $YOUR_URL_HERE)
);
That URL can be your own application, or something like a TwiML Bin or Twilio Function. Check out this article to see how to respond with TwiML to a request from Twilio.
I notice that you are building something for one time passwords too. You might be interested in checking out the Authy API that can implement and deliver OTP codes via calls, SMS and app.

How to record-from-answer-dual with Twilio's object oriented interface?

I understand how to enable the 'record-from-answer-dual' with the XML style command set, but I'm not finding any way to accomplish the same thing with the more object-oriented style code, such as:
<?php
require_once 'twilio-php-master/Twilio/autoload.php';
$response = new Twilio\Twiml();
$sayMsg = 'Attention! Attention! The network operations
center has opened a ticket concerning an ATMS failure in the Eastern
region. The ticket number is ECHO,1,5,7,4. I repeat, the ticket number is
ECHO,1,5,7,4. Thank you.';
$response->record();
$response->say($sayMsg, array('voice' => 'alice'));
$response->hangup();
echo $response;
I've tried adding it to the new line, and the record line as an array-style entry, similar to enabling the Alice voice. No dice.
I want to record the entire call, from answer, including the message spoken by Twilio.
Thanks for any information anyone can provide!
Twilio developer evangelist here.
<Record> is used to record messages from a call, not to record the TwiML that follows. It's more useful if you are building a messaging or voicemail system for voice.
Given that your message sounds like some kind of announcement, I am guessing that you are generating this call from the REST API. In that case, you can use the Record parameter when you place the call and the entire call will be recorded. In PHP, that would be something like this:
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
$to, $from,
array(
"url" => $url,
"record" => true
)
);
Check out the documentation on the parameters you can use when making a call, including Record here.
Let me know if that helps at all.
Update from Jeffrey's comment
This is the Perl version, using the unofficial Twilio Perl module:
use WWW::Twilio::API;
my ($twilaccountsid, $twilauthtoken, $fromnum, $tonum, $twiml_uri) = #_;
my $twilio = WWW::Twilio::API->new(AccountSid => $twilaccountsid, AuthToken => $twilauthtoken);
my $response = $twilio->POST( 'Calls', From => $fromnum, To => $tonum, Record => 'true', Url => $twiml_uri);
return $response->{content};

TWILIO - why the actual response returned does not match with documented

I have integrated Twilio and it works fine. Now I want to capture all the intermediate message statuses. I referred to Sending Messages.
My code looks like -
require __DIR__ . '/vendor/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = '****************';
$token = '*****************';
$client = new Client($sid, $token);
// send message
$message = $client->messages->create(
// the number you'd like to send the message to
'+1xxxxxxxxx',
array(
'from' => '+1xxxxxxxx',
'body' => 'Test web hook message '.date('h:i'),
'statusCallback' => "https://xxxxxx/xxxx.php",
)
);
But the output/response returned to statusCallback is different as -
"{\"SmsSid\":\"SM72478c1ea61f467dbc33338123c0ad0\",\"SmsStatus\":\"sent\",\"MessageStatus\":\"sent\",\"To\":\"+1xxxxxxxx\",\"MessageSid\":\"SM72478c1ea612222dbc3b7858123c0ad0\",\"AccountSid\":\"ACb655a10c1c2222e4af158c5395d64beb\",\"From\":\"+1xxxxxxx\",\"ApiVersion\":\"2010-04-01\"}"
But I need the response as it is defined at Sending Messages
EDIT
If checked at Sending Messages, we can see the fields returned in the output are - account_sid, api_version, body, num_segments, num_media, date_created, date_sent, date_updated, direction, error_code, error_message, from, price, sid, status, to and uri.
But I receive fields as - SmsSid, SmsStatus, MessageStatus, To, MessageSid, AccountSid, From and ApiVersion.
For me, the fields - num_segments, date_sent, direction, error_code, error_message are important which I am not receiving. Do I need to use another API of TWILIO to retrieve this information ?
Why am I getting different response ?
Twilio developer evangelist here.
When you send a message and set a statusCallback URL the sending messages documentation says:
Twilio will POST the MessageSid along with the other standard request parameters as well as MessageStatus and ErrorCode.
The standard request parameters are:
MessageSid
SmsSid
AccountSid
MessagingServiceSid
From
To
Body
NumMedia
as well as some others specifically about media or geographic data based on the two numbers.
If you need to find out those other attributes of the message, you will need to look up the message using the REST API.
Let me know if that helps at all.
What does your code for your callback url script look like?
What you have is just an escaped JSON string, so to match what you see on the documentation you just have to do this:
$json = '{\"SmsSid\":\"SM72478c1ea61f467dbc33338123c0ad0\",\"SmsStatus\":\"sent\",\"MessageStatus\":\"sent\",\"To\":\"+1xxxxxxxx\",\"MessageSid\":\"SM72478c1ea612222dbc3b7858123c0ad0\",\"AccountSid\":\"ACb655a10c1c2222e4af158c5395d64beb\",\"From\":\"+1xxxxxxx\",\"ApiVersion\":\"2010-04-01\"}';
echo stripslashes($json);

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>

Resources