Twilio - Carrier Lookup - twilio

How do I access the phone carrier with Twilio's carrier lookup?
Here is some of my sample code:
require_once('twilio/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "--------------";
$token = "------------------";
$client = new Lookups_Services_Twilio($sid, $token);
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
//How do I access the carrier here?
echo $number->phone_number;

Twilio developer evangelist here.
When you call for the carrier details, they are all returned as an object on the number called carrier. You can see this in the example response on the Twilio Lookup page. So, with your code:
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
echo $number->carrier->name;
echo $number->carrier->type;

With Twilio 5.x SDK, things changed very slightly:
use Twilio\Rest\Client as Twilio;
$client = new Twilio("sid", "token");
$response = $client->lookups->phoneNumbers("+15551234567")->fetch(["type" => "carrier"]);
echo $response->carrier["type"] . "\r\n";
echo $response->carrier["name"];
See https://www.twilio.com/docs/api/lookups for the complete current documentation.

When fetching "type" => "caller-name", the response can be unwound with:
echo $response->callerName["caller_name"];
camelCase, hyphen and dash all in one use case!

Related

RestException is not a valid phone number on Twilio trial account

I use twilio/sdk package, with the following code.
$sid = "AC3###############";
$token = "100################";
$from = $to = "+628********99";
$otp = rand(1111111, 9999999);
$client = new Twilio\Rest\Client($sid, $token);
$message = $client
->messages
->create($to, [
'from' => $to,
'body' => 'OTP code : ' . $otp
])
This is the error that I got
Twilio\Exceptions\RestException [HTTP 400] Unable to create record: The 'To' number +628********99 is not a valid phone number.
What did I do wrong ?
How can I use my trial account to send trial sms?
FYI the number I used is a verified number in Twilio and country origin is Indonesia +62.
The line $token = '100################; is missing it's closing single quote. Could that be the reason?
PS: It's hard to build SMS OTP right because of fraud schemes like SMS Traffic Pumping Fraud. Twilio Verify has built-in protection against these risks.

How to switch an ongoing Twilio phone call to a <Pay> connector

I would like to use the < Pay> connector optionally while in an ongoing phone call. I cannot find out how to do trigger a new resource during an ongoing phone call.
You can modify an "in progress" call by passing new TwiML (XML) to execute which could contain your "<Pay>".
You must provide
the ID of the call you want to modify (the "CallSid" "CAe1644a7eed5088b159577c5802d8be38")
and an URL where Twilio will find the instructions (the "Url" "http://demo.twilio.com/docs/voice.xml")
I don't know what language you're using but in PHP with Twilio's library the code would look someting like this:
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACc0966dd96e4d55d26ae72df4d6dc3494";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(array(
"method" => "POST",
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
print($call->to);
You can read more about this here
(https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress).

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 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 REST API How to get Sid

I am still new to Twilio. I am writing a PHP script which will connect to Twilio and parse some information about calls. I am using the code from this page:
https://www.twilio.com/docs/api/rest/call
Here is my code:
require_once '../twilio-library/Services/Twilio.php';
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo "->".$call->Sid."<- <br/>";
}
The browser just outputs many blanks. No Sid values. What am I doing wrong?
Twilio Developer Evangelist here.
Try doing the following to get call information.
<?php
require_once '../twilio-library/Services/Twilio.php';
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
$client = new Services_Twilio($sid, $token);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo $call->sid;
}
Notice that if you're using the latest version of the library, you don't need to specify a version on your request. Double check that the path ../twilio-library/Services/Twilio.php is correct for you though.
If you're only testing, you could move the file Twilio.php to the same directory where your code is and just import Twilio.php on it.
If you then decide to filter the logs by date, here's how you would do it.
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->getIterator(0, 50, array(
"Status" => "completed",
"StartTime>" => "2015-03-01",
"StartTime<" => "2015-05-10"
)) as $call
) {
echo $call->sid;
}
Let me know how it goes.

Resources