Twilio for Perl

CI

A Perl library for the Twilio API. It sends messages, makes and answers calls, and reaches Twilio's other products the same way as Twilio's official libraries for PHP, Python and Node.js, so Twilio's own documentation applies to it.

Version 2 is a complete rewrite and shares no code with version 1.

Installation

cpanm Twilio

It needs Perl 5.16 or later. Two modules are optional: Cpanel::JSON::XS makes JSON faster, and Crypt::OpenSSL::RSA is needed only for Public Key Client Validation.

Quick start

Send an SMS:

use Twilio;

my $client = Twilio->new;    # reads TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN

my $message = $client->messages->create('+15558675310', {
  from => '+15017122661',
  body => 'Hello from Perl',
});
print $message->sid, "\n";

Make a call:

my $call = $client->calls->create('+15558675310', '+15017122661', {
  url => 'http://demo.twilio.com/docs/voice.xml',
});

Reply to an incoming SMS, from the webhook of your Twilio number (a CGI script here; Twilio::Manual also shows Mojolicious):

use CGI;
use Twilio::TwiML::MessagingResponse;

my $query    = CGI->new;
my $response = Twilio::TwiML::MessagingResponse->new;
$response->message('Thanks! You said: ' . $query->param('Body'));

print $query->header(-type => 'text/xml', -charset => 'UTF-8'), $response;

Twilio::Manual is the guide: it follows Twilio's messaging and voice quickstarts, and covers credentials, API keys, webhook validation, lists, errors, other products, and how to translate any example from Twilio's documentation.

What it covers

How it follows the official libraries

The library has the same structure as Twilio's official libraries, which are generated from the same specifications: a client, then a product and version, then a resource's list, a record's context, and the record itself.

$client->verify->v2->services($service_sid)->verifications->create('+15558675310', 'sms');

is, step by step, the same chain as in PHP, Python or Node.js. Where the libraries differ, it follows twilio-php: method names, the order of required values, paging, and the format of errors and TwiML. So a documentation example translates with a few rules: names in snake_case, required values first as plain arguments, optional values in a hash reference, and eval for errors. The manual lists them all, with examples.

How it is built

The 678 modules under Twilio::Rest are generated, as the official libraries' are. The generator in tools/ reads Twilio's OpenAPI specifications (twilio-oai, pinned to a release) and applies the rules of Twilio's own generator, deciding unclear cases the way twilio-php 8.14 does. Each class gets its documentation from the descriptions in the specifications. The TwiML classes are generated from the element and attribute names in twilio-php.

The rest is written by hand: HTTP, authentication, paging, errors, TwiML writing, tokens and validation. It is ported from twilio-php, with choices from twilio-python and twilio-node where those are better, such as keeping an OAuth token for each credential provider, as twilio-python does. Only requests that Twilio refused with HTTP 429 are ever sent again, so a message is never sent twice.

The generated code is committed, and an author test fails if it differs from what the generator writes. The generator is not part of the CPAN distribution; see tools/README.md.

How it is tested

Development

perl Makefile.PL
make test        # nmake on Windows with MSVC

Author checks are in xt/, and need the develop prerequisites listed in Makefile.PL:

prove -lr xt

The live tests need a Twilio account; xt/live/smoke.t explains its switches.

Issues and contributions

Bug reports and pull requests are welcome through GitHub issues. CONTRIBUTING.md explains how to work on the library.

Report security problems privately, by email, as SECURITY.md explains, not through issues.

License

MIT. This is an independent project. It is not written, endorsed or supported by Twilio Inc., and "Twilio" is a trademark of Twilio Inc.

Parts of it are based on Twilio's own MIT-licensed work, twilio-php and the twilio-oai specifications. As that license requires, LICENSE also includes Twilio's copyright notice for them, as credit.