blib/lib/SMS/Send/NANP/Twilio.pm

filter: Statementstmt 29.7% 25 / 84 Branchbran 7.1% 4 / 56 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 33.3% 7 / 21 Podpod 100% 8 / 8 totaltotal 25.5% 44 / 172 CC 31 SCAR 46.7
CC
31
Cov
20%
send_sms
306.0
StatusCallback
12.0
ApplicationSid
12.0
CRAP 106.3
↑ Summary
1 package SMS::Send::NANP::Twilio;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use JSON::XS qw{decode_json};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base qw{SMS::Send::Driver::WebService};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.04';
9  
10 =head1 NAME
11  
12 SMS::Send::NANP::Twilio - SMS::Send driver for Twilio
13  
14 =head1 SYNOPSIS
15  
16 Configure /etc/SMS-Send.ini
17  
18 [NANP::Twilio]
19 username=accountSid
20 password=authToken
21 MessagingServiceSid=String
22 ;From=+12025551212
23 ;StatusCallback=URL
24 ;ApplicationSid=String
25 ;MaxPrice=USD
26 ;ProvideFeedback=true|false
27 ;ValidityPeriod=14400
28  
29 use SMS::Send;
30 my $sms = SMS::Send->new('NANP::Twilio');
31 my $success = $sms->send_sms(text=> 'Hello World!', to =>'+17035551212');
32  
33 use SMS::Send::NANP::Twilio;
34 my $sms = SMS::Send::NANP::Twilio->new;
35 my $success = $sms->send_sms(text=> 'Hello World!', to =>'+17035551212');
36 my $json = $sms->{__content};
37 my $href = $sms->{__data};
38  
39 =head1 DESCRIPTION
40  
41 SMS::Send driver for Twilio
42  
43 =head1 METHODS
44  
45 =head2 send_sms
46  
47 Sends SMS Message via Twilio web service and returns 1 or 0. Method dies on critical error.
48  
49 my $status = $sms->send_sms(to =>'+17035551212', text=> 'Hello World!');
50 my $status = $sms->send_sms(to =>'+17035551212', text=> 'Image Attached', MediaUrl=>'https://...');
51  
52 =over
53  
54 =item to
55  
56 Passed as "To" in the posted form data. The destination phone number for SMS/MMS or a Channel user address for other 3rd party channels. Destination phone numbers should be formatted with a '+' and country code e.g., +16175551212 (E.164 format).
57  
58 to => "+17035551212"
59  
60 =item text
61  
62 Passed as "Body" in the posted form data. The text of the message you want to send, limited to 1600 characters.
63  
64 text => "My Message Body"
65  
66 =item MediaUrl
67  
68 The URL of the media you wish to send out with the message. gif, png, and jpeg content is currently supported and will be formatted correctly on the recipient's device. Other types are also accepted by the API. The media size limit is 5MB. If you wish to send more than one image in the message body, please provide multiple MediaUrls values in an array reference. You may include up to 10 MediaUrls per message.
69  
70 MediaUrl => "https://...."
71 MediaUrl => [$url1, $url2, ...]
72  
73 =back
74  
75 =cut
76  
77 sub send_sms {
78 0 ▶ my $self = shift;
Subroutine send_sms: not called CC=17 SCAR=57.2
Pod documented
79 0 my %argv = @_;
80  
81 0 ▶ my $to = $argv{'to'} or die('Error: to propoerty required');
Branch: 1 branch
BranchTrueFalseTotal
my $to = $argv{"to"} or die "Error: to propoerty required" 0 0 0
82 0 ▶ my $text = defined($argv{'text'}) ? $argv{'text'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $argv{'text'} ? : 0 0 0
83 0 my @form = (To => $to, Body => $text);
84  
85 0 ▶ my $MediaUrl = $argv{'MediaUrl'} || [];
Condition0%0/2
$argv{'MediaUrl'} || []
l!l
00
MC/DC0%0/1
$argv{'MediaUrl'} || []
$argv{'MediaUrl'}
Truth table0%0/2
A
A= $argv{'MediaUrl'}
Aresult
11
00
86 0 ▶ $MediaUrl = [$MediaUrl] unless ref($MediaUrl) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $MediaUrl eq "ARRAY" 0 0 0
87 0 ▶ die("Error: MediaUrl - You may include up to 10 MediaUrls per message.") if @$MediaUrl > 10;
Branch: 1 branch
BranchTrueFalseTotal
if @$MediaUrl > 10 0 0 0
88 0 push @form, MediaUrl => $_ foreach @$MediaUrl;
89  
90 0 my $status_response;
91 0 ▶ if ($self->From) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->From) 0 0 0
92 0 push @form, From => $self->From;
93 0 $status_response = 'queued'; #When you only specify the From parameter, Twilio will validate the phone
94 #numbers synchronously and return either a queued status or an error.
95 }
96 0 ▶ if ($self->MessagingServiceSid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->MessagingServiceSid) 0 0 0
97 0 push @form, MessagingServiceSid => $self->MessagingServiceSid;
98 0 $status_response = 'accepted'; #When specifying the MessagingServiceSid parameter, Twilio will first
99 #return an accepted status.
100 }
101 0 ▶ die('Error: Property "From" or "MessagingServiceSid" must be configured.') unless $status_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $status_response 0 0 0
102  
103 0 ▶ push @form, StatusCallback => $self->StatusCallback if $self->StatusCallback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->StatusCallback 0 0 0
104 0 ▶ push @form, ApplicationSid => $self->ApplicationSid if $self->ApplicationSid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ApplicationSid 0 0 0
105 0 ▶ push @form, MaxPrice => $self->MaxPrice if $self->MaxPrice;
Branch: 1 branch
BranchTrueFalseTotal
if $self->MaxPrice 0 0 0
106 0 ▶ push @form, ProvideFeedback => $self->ProvideFeedback if $self->ProvideFeedback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ProvideFeedback 0 0 0
107 0 ▶ push @form, ValidityPeriod => $self->ValidityPeriod if $self->ValidityPeriod;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ValidityPeriod 0 0 0
108  
109 0 my $response = $self->uat->post_form($self->_url, \@form); #isa HASH from HTTP::Tiny
110 0 ▶ die(sprintf("HTTP Error: %s %s", $response->{'status'}, $response->{'reason'})) unless $response->{'success'};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
111 0 $self->{'__content'} = $response->{'content'};
112 0 my $data = decode_json($response->{'content'});
113 0 $self->{'__data'} = $data;
114 0 ▶ return $data->{'status'} eq $status_response ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$data->{'status'} eq $status_response ? : 0 0 0
115 }
116  
117 sub _url {
118 0 ▶ my $self = shift;
Subroutine _url: not called CC=1 SCAR=6.9
119 0 my $url = URI->new(join '/', 'https://api.twilio.com/2010-04-01/Accounts', $self->username, 'Messages.json');
120 0 $url->userinfo(join ':', $self->username, $self->password);
121 0 return $url;
122 }
123  
124 =head1 PROPERTIES
125  
126 Properties may be stored in Current Directory, /etc/SMS-Send.ini or C:\Windows\SMS-Send.ini. See L<SMS::Send::Driver::WebService>->cfg_path
127  
128 =head2 username
129  
130 The "Account SID" is passed on the URL and sent as the username for basic authentication credentials
131  
132 =cut
133  
134 #see SMS::Send::Driver::WebService->username
135  
136 =head2 password
137  
138 The "Auth Token" sent as password for basic authentication credentials
139  
140 =cut
141  
142 #see SMS::Send::Driver::WebService->password
143  
144 =head2 From
145  
146 The "From" parameter passed in the posted form
147  
148 A Twilio phone number (in E.164 format), alphanumeric sender ID or a Channel Endpoint address enabled for the type of message you wish to send. Phone numbers or short codes purchased from Twilio work here. You cannot (for example) spoof messages from your own cell phone number.
149  
150 =cut
151  
152 sub From {
153 1 ▶ my $self=shift;
Subroutine From: called CC=3 SCAR=11.4
Pod documented
154 1 ▶ $self->{'From'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
155 1 ▶ $self->{'From'}=$self->cfg_property('From', $self->_From_default) unless defined $self->{'From'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"From"} 0 1 1
156 1 return $self->{'From'};
157 }
158  
159 0 ▶sub _From_default {undef};
Subroutine _From_default: not called CC=1 SCAR=6.9
160  
161 =head2 MessagingServiceSid
162  
163 The "MessagingServiceSid" parameter passed in the posted form
164  
165 The 34 character unique id of the Messaging Service you want to associate with this Message. Set this parameter to use the Messaging Service Settings and Copilot Features you have configured. When only this parameter is set, Twilio will use your enabled Copilot Features to select the from phone number for delivery.
166  
167 =cut
168  
169 sub MessagingServiceSid {
170 1 ▶ my $self=shift;
Subroutine MessagingServiceSid: called CC=3 SCAR=11.4
Pod documented
171 1 ▶ $self->{'MessagingServiceSid'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
172 1 ▶ $self->{'MessagingServiceSid'}=$self->cfg_property('MessagingServiceSid', $self->_MessagingServiceSid_default) unless defined $self->{'MessagingServiceSid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"MessagingServiceSid"} 0 1 1
173 1 return $self->{'MessagingServiceSid'};
174 }
175  
176 0 ▶sub _MessagingServiceSid_default {undef};
Subroutine _MessagingServiceSid_default: not called CC=1 SCAR=6.9
177  
178 =head2 StatusCallback
179  
180 The "StatusCallback" parameter passed in the posted form
181  
182 A URL where Twilio will POST each time your message status changes to one of the following: queued, failed, sent, delivered, or undelivered. Twilio will POST the MessageSid along with the other standard request parameters as well as MessageStatus and ErrorCode. If this parameter passed in addition to a MessagingServiceSid, Twilio will override the Status Callback URL of the Messaging Service. URLs must contain a valid hostname (underscores are not allowed).
183  
184 =cut
185  
186 sub StatusCallback {
187 0 ▶ my $self=shift;
Subroutine StatusCallback: not called CC=3 SCAR=24.8
Pod documented
188 0 ▶ $self->{'StatusCallback'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
189 0 ▶ $self->{'StatusCallback'}=$self->cfg_property('StatusCallback', $self->_StatusCallback_default) unless defined $self->{'StatusCallback'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"StatusCallback"} 0 0 0
190 0 return $self->{'StatusCallback'};
191 }
192  
193 0 ▶sub _StatusCallback_default {undef};
Subroutine _StatusCallback_default: not called CC=1 SCAR=6.9
194  
195 =head2 ApplicationSid
196  
197 The "ApplicationSid" parameter passed in the posted form
198  
199 Twilio will POST MessageSid as well as MessageStatus=sent or MessageStatus=failed to the URL in the MessageStatusCallback property of this Application. If the StatusCallback parameter above is also passed, the Application's MessageStatusCallback parameter will take precedence.
200  
201 =cut
202  
203 sub ApplicationSid {
204 0 ▶ my $self=shift;
Subroutine ApplicationSid: not called CC=3 SCAR=24.8
Pod documented
205 0 ▶ $self->{'ApplicationSid'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
206 0 ▶ $self->{'ApplicationSid'}=$self->cfg_property('ApplicationSid', $self->_ApplicationSid_default) unless defined $self->{'ApplicationSid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ApplicationSid"} 0 0 0
207 0 return $self->{'ApplicationSid'};
208 }
209  
210 0 ▶sub _ApplicationSid_default {undef};
Subroutine _ApplicationSid_default: not called CC=1 SCAR=6.9
211  
212 =head2 MaxPrice
213  
214 The "MaxPrice" parameter passed in the posted form
215  
216 The total maximum price up to the fourth decimal (0.0001) in US dollars acceptable for the message to be delivered. All messages regardless of the price point will be queued for delivery. A POST request will later be made to your Status Callback URL with a status change of 'Sent' or 'Failed'. When the price of the message is above this value the message will fail and not be sent. When MaxPrice is not set, all prices for the message is accepted.
217  
218 =cut
219  
220 sub MaxPrice {
221 0 ▶ my $self=shift;
Subroutine MaxPrice: not called CC=3 SCAR=24.8
Pod documented
222 0 ▶ $self->{'MaxPrice'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
223 0 ▶ $self->{'MaxPrice'}=$self->cfg_property('MaxPrice', $self->_MaxPrice_default) unless defined $self->{'MaxPrice'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"MaxPrice"} 0 0 0
224 0 return $self->{'MaxPrice'};
225 }
226  
227 0 ▶sub _MaxPrice_default {undef};
Subroutine _MaxPrice_default: not called CC=1 SCAR=6.9
228  
229 =head2 ProvideFeedback
230  
231 The "ProvideFeedback" parameter passed in the posted form
232  
233 Set this value to true if you are sending messages that have a trackable user action and you intend to confirm delivery of the message using the Message Feedback API. This parameter is set to false by default.
234  
235 =cut
236  
237 sub ProvideFeedback {
238 0 ▶ my $self=shift;
Subroutine ProvideFeedback: not called CC=3 SCAR=24.8
Pod documented
239 0 ▶ $self->{'ProvideFeedback'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
240 0 ▶ $self->{'ProvideFeedback'}=$self->cfg_property('ProvideFeedback', $self->_ProvideFeedback_default) unless defined $self->{'ProvideFeedback'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ProvideFeedback"} 0 0 0
241 0 return $self->{'ProvideFeedback'};
242 }
243  
244 0 ▶sub _ProvideFeedback_default {undef};
Subroutine _ProvideFeedback_default: not called CC=1 SCAR=6.9
245  
246 =head2 ValidityPeriod
247  
248 The "ValidityPeriod" parameter passed in the posted form
249  
250 The number of seconds that the message can remain in a Twilio queue. After exceeding this time limit, the message will fail and a POST request will later be made to your Status Callback URL. Valid values are between 1 and 14400 seconds (the default). Please note that Twilio cannot guarantee that a message will not be queued by the carrier after they accept the message. We do not recommend setting validity periods of less than 5 seconds.
251  
252 =cut
253  
254 sub ValidityPeriod {
255 0 ▶ my $self=shift;
Subroutine ValidityPeriod: not called CC=3 SCAR=24.8
Pod documented
256 0 ▶ $self->{'ValidityPeriod'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
257 0 ▶ $self->{'ValidityPeriod'}=$self->cfg_property('ValidityPeriod', $self->_ValidityPeriod_default) unless defined $self->{'ValidityPeriod'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ValidityPeriod"} 0 0 0
258 0 return $self->{'ValidityPeriod'};
259 }
260  
261 0 ▶sub _ValidityPeriod_default {undef};
Subroutine _ValidityPeriod_default: not called CC=1 SCAR=6.9
262  
263 =head1 SEE ALSO
264  
265 L<SMS::Send::Driver::WebService>, L<SMS::Send>, L<https://www.twilio.com/docs/api/messaging/send-messages>
266  
267 =head1 AUTHOR
268  
269 Michael R. Davis
270  
271 =head1 COPYRIGHT AND LICENSE
272  
273 Copyright (C) 2018 by Michael R. Davis
274  
275 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
276  
277 =cut
278  
279 4 1;
↑ Summary