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

filter: Statementstmt 33.9% 35 / 103 Branchbran 14.7% 10 / 68 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 30.0% 9 / 30 Podpod 100% 10 / 10 totaltotal 28.5% 64 / 224 CC 39 SCAR 44.0
CC
39
Cov
25%
send_sms
306.0
StatusCallback
12.0
ApplicationSid
12.0
CRAP 81.0
↑ 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.06';
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 AccountSid=AccountSid
20 AuthToken=AuthToken
21 MessagingServiceSid=String
22 ;From=+12025550123
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 =>'+17035550123');
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 =>'+17035550123');
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 =>'+17035550123', text=> 'Hello World!');
50 my $status = $sms->send_sms(to =>'+17035550123', 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., +16175550123 (E.164 format).
57  
58 to => "+17035550123"
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 0 ▶sub send_sms {
Subroutine send_sms: not called CC=17 SCAR=57.2
Pod documented
78 0 my $self = shift;
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 0 ▶sub _url {
Subroutine _url: not called CC=1 SCAR=6.9
118 0 my $self = shift;
119 0 my $url = URI->new(join '/', $self->url, 'Accounts', $self->AccountSid, 'Messages.json');
120 0 $url->userinfo(join ':', $self->AccountSid, $self->AuthToken);
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 url
129  
130 Returns the url for the Twilio versioned service.
131  
132 Default: https://api.twilio.com/2010-04-01
133  
134 =cut
135  
136 #see SMS::Send::Driver::WebService->url
137  
138 0 ▶sub _url_default {'https://api.twilio.com/2010-04-01'};
Subroutine _url_default: not called CC=1 SCAR=6.9
139 0 ▶sub _host_default {'api.twilio.com'};
Subroutine _host_default: not called CC=1 SCAR=6.9
140 0 ▶sub _protocol_default {'https'};
Subroutine _protocol_default: not called CC=1 SCAR=6.9
141 0 ▶sub _port_default {'443'};
Subroutine _port_default: not called CC=1 SCAR=6.9
142 0 ▶sub _script_name_default {'/2010-04-01'};
Subroutine _script_name_default: not called CC=1 SCAR=6.9
143  
144 =head2 AccountSid
145  
146 The "AccountSID" is passed on the URL and sent as the username for basic authentication credentials
147  
148 =cut
149  
150 1 ▶sub AccountSid {
Subroutine AccountSid: called CC=5 SCAR=20.2
Pod documented
151 1 my $self = shift;
152 1 ▶ $self->{'AccountSid'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
153 1 ▶ unless (defined $self->{'AccountSid'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"AccountSid"}) 0 1 1
154 0 ▶ $self->{'AccountSid'} = $self->cfg_property('AccountSid', $self->_AccountSid_default)
Condition0%0/3
$self->cfg_property('AccountSid', $self->_AccountSid_default) || $self->cfg_property('username', $self->_username_default)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cfg_property('AccountSid', $self->_AccountSid_default) || $self->cfg_property('username', $self->_username_default)
$self->cfg_property('AccountSid', $self->_AccountSid_default) $self->cfg_property('username', $self->_username_default)
Truth table0%0/3
A || B
A= $self->cfg_property('AccountSid', $self->_AccountSid_default)B= $self->cfg_property('username', $self->_username_default)
ABresult
1X1
011
000
155 || $self->cfg_property('username', $self->_username_default); #pre 0.04
156 }
157 1 ▶ die('Error: AccountSid property required') unless defined $self->{'AccountSid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"AccountSid"} 0 1 1
158 1 return $self->{'AccountSid'};
159 }
160  
161 0 ▶sub _AccountSid_default {undef};
Subroutine _AccountSid_default: not called CC=1 SCAR=6.9
162  
163 =head2 AuthToken
164  
165 The "AuthToken" sent as password for basic authentication credentials
166  
167 =cut
168  
169 1 ▶sub AuthToken {
Subroutine AuthToken: called CC=5 SCAR=20.2
Pod documented
170 1 my $self = shift;
171 1 ▶ $self->{'AuthToken'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
172 1 ▶ unless (defined $self->{'AuthToken'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"AuthToken"}) 0 1 1
173 0 ▶ $self->{'AuthToken'} = $self->cfg_property('AuthToken', $self->_AuthToken_default)
Condition0%0/3
$self->cfg_property('AuthToken', $self->_AuthToken_default) || $self->cfg_property('password', $self->_password_default)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cfg_property('AuthToken', $self->_AuthToken_default) || $self->cfg_property('password', $self->_password_default)
$self->cfg_property('AuthToken', $self->_AuthToken_default) $self->cfg_property('password', $self->_password_default)
Truth table0%0/3
A || B
A= $self->cfg_property('AuthToken', $self->_AuthToken_default)B= $self->cfg_property('password', $self->_password_default)
ABresult
1X1
011
000
174 || $self->cfg_property('password', $self->_password_default); #pre 0.04
175 }
176 1 ▶ die('Error: AuthToken property required') unless defined $self->{'AuthToken'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"AuthToken"} 0 1 1
177 1 return $self->{'AuthToken'};
178 }
179  
180 0 ▶sub _AuthToken_default {undef};
Subroutine _AuthToken_default: not called CC=1 SCAR=6.9
181  
182 =head2 From
183  
184 The "From" parameter passed in the posted form
185  
186 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.
187  
188 =cut
189  
190 1 ▶sub From {
Subroutine From: called CC=3 SCAR=11.4
Pod documented
191 1 my $self=shift;
192 1 ▶ $self->{'From'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
193 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
194 1 return $self->{'From'};
195 }
196  
197 0 ▶sub _From_default {undef};
Subroutine _From_default: not called CC=1 SCAR=6.9
198  
199 =head2 MessagingServiceSid
200  
201 The "MessagingServiceSid" parameter passed in the posted form
202  
203 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.
204  
205 =cut
206  
207 1 ▶sub MessagingServiceSid {
Subroutine MessagingServiceSid: called CC=3 SCAR=11.4
Pod documented
208 1 my $self=shift;
209 1 ▶ $self->{'MessagingServiceSid'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
210 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
211 1 return $self->{'MessagingServiceSid'};
212 }
213  
214 0 ▶sub _MessagingServiceSid_default {undef};
Subroutine _MessagingServiceSid_default: not called CC=1 SCAR=6.9
215  
216 =head2 StatusCallback
217  
218 The "StatusCallback" parameter passed in the posted form
219  
220 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).
221  
222 =cut
223  
224 0 ▶sub StatusCallback {
Subroutine StatusCallback: not called CC=3 SCAR=24.8
Pod documented
225 0 my $self=shift;
226 0 ▶ $self->{'StatusCallback'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
227 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
228 0 return $self->{'StatusCallback'};
229 }
230  
231 0 ▶sub _StatusCallback_default {undef};
Subroutine _StatusCallback_default: not called CC=1 SCAR=6.9
232  
233 =head2 ApplicationSid
234  
235 The "ApplicationSid" parameter passed in the posted form
236  
237 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.
238  
239 =cut
240  
241 0 ▶sub ApplicationSid {
Subroutine ApplicationSid: not called CC=3 SCAR=24.8
Pod documented
242 0 my $self=shift;
243 0 ▶ $self->{'ApplicationSid'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
244 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
245 0 return $self->{'ApplicationSid'};
246 }
247  
248 0 ▶sub _ApplicationSid_default {undef};
Subroutine _ApplicationSid_default: not called CC=1 SCAR=6.9
249  
250 =head2 MaxPrice
251  
252 The "MaxPrice" parameter passed in the posted form
253  
254 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.
255  
256 =cut
257  
258 0 ▶sub MaxPrice {
Subroutine MaxPrice: not called CC=3 SCAR=24.8
Pod documented
259 0 my $self=shift;
260 0 ▶ $self->{'MaxPrice'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
261 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
262 0 return $self->{'MaxPrice'};
263 }
264  
265 0 ▶sub _MaxPrice_default {undef};
Subroutine _MaxPrice_default: not called CC=1 SCAR=6.9
266  
267 =head2 ProvideFeedback
268  
269 The "ProvideFeedback" parameter passed in the posted form
270  
271 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.
272  
273 =cut
274  
275 0 ▶sub ProvideFeedback {
Subroutine ProvideFeedback: not called CC=3 SCAR=24.8
Pod documented
276 0 my $self=shift;
277 0 ▶ $self->{'ProvideFeedback'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
278 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
279 0 return $self->{'ProvideFeedback'};
280 }
281  
282 0 ▶sub _ProvideFeedback_default {undef};
Subroutine _ProvideFeedback_default: not called CC=1 SCAR=6.9
283  
284 =head2 ValidityPeriod
285  
286 The "ValidityPeriod" parameter passed in the posted form
287  
288 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.
289  
290 =cut
291  
292 0 ▶sub ValidityPeriod {
Subroutine ValidityPeriod: not called CC=3 SCAR=24.8
Pod documented
293 0 my $self=shift;
294 0 ▶ $self->{'ValidityPeriod'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
295 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
296 0 return $self->{'ValidityPeriod'};
297 }
298  
299 0 ▶sub _ValidityPeriod_default {undef};
Subroutine _ValidityPeriod_default: not called CC=1 SCAR=6.9
300  
301 =head1 SEE ALSO
302  
303 L<SMS::Send::Driver::WebService>, L<SMS::Send>, L<https://www.twilio.com/docs/api/messaging/send-messages>
304  
305 =head1 AUTHOR
306  
307 Michael R. Davis
308  
309 =head1 COPYRIGHT AND LICENSE
310  
311 MIT License
312  
313 Copyright (c) 2022 Michael R. Davis
314  
315 =cut
316  
317 4 1;
↑ Summary