Perl Requires Chilkat v11.0.0+
Perl
List Twilio message history
See more Twilio Examples
List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.
Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.
Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
my $success = 0;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);
$http->BasicAuth(1);
# Load the owning Account SID and an API key from application configuration.
my $accountSid = 'YOUR_ACCOUNT_SID';
$http->Login('YOUR_API_KEY_SID');
$http->Password('YOUR_API_KEY_SECRET');
my $url = Chilkat2::StringBuilder->new;
$url->Append('https://api.twilio.com/2010-04-01/Accounts/');
$url->Append($accountSid);
$url->Append('/Messages.json');
$url->Append('?PageSize=50');
my $resp = Chilkat2::HttpResponse->new;
my $reply = Chilkat2::JsonObject->new;
my $total = 0;
my $more = 1;
while ($more) {
$success = $http->HttpNoBody('GET', $url->GetAsString(), $resp);
if ($resp->StatusCode == 0) {
print $http->LastErrorText, "\n";
exit;
}
my $parsed = $reply->Load($resp->BodyStr);
if ((!$success) || ($resp->StatusCode != 200)) {
print 'HTTP ', $resp->StatusCode, "\n";
if (($parsed) && ($reply->HasMember('code'))) {
print 'Twilio error ', $reply->IntOf('code'), ': ', $reply->StringOf('message'), "\n";
}
exit;
}
if (!$parsed) {
print "Expected a JSON response.\n";
exit;
}
if ($reply->JsonTypeOf('messages') != 4) {
print "Expected a messages array.\n";
exit;
}
my $count = $reply->SizeOfArray('messages');
my $i = 0;
while ($i < $count) {
$reply->I($i);
print $reply->StringOf('messages[i].sid'), ' ', $reply->StringOf('messages[i].direction'), ' ', $reply->StringOf('messages[i].status'), "\n";
$i = $i + 1;
}
$total = $total + $count;
# Twilio supplies a relative URI with the cursor and returns null on the last page.
if ($reply->IsNullOf('next_page_uri')) {
$more = 0;
} else {
if ($reply->JsonTypeOf('next_page_uri') != 1) {
print "Expected a next-page URI or null.\n";
exit;
}
$url->SetString('https://api.twilio.com');
$url->Append($reply->StringOf('next_page_uri'));
}
}
print 'Messages found: ', $total, "\n";