line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SMS::Send::Twilio; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
51973
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
46
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
38
|
use 5.008_005; |
|
2
|
|
|
|
|
10
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
101
|
|
9
|
2
|
|
|
2
|
|
874
|
use JSON::PP; |
|
2
|
|
|
|
|
22975
|
|
|
2
|
|
|
|
|
143
|
|
10
|
2
|
|
|
2
|
|
715
|
use WWW::Twilio::API; |
|
2
|
|
|
|
|
69455
|
|
|
2
|
|
|
|
|
70
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
16
|
use parent qw(SMS::Send::Driver); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.20'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=encoding utf-8 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
SMS::Send::Twilio - SMS::Send backend for Twilio API |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use SMS::Send; |
25
|
|
|
|
|
|
|
# Create an object. There are three required values: |
26
|
|
|
|
|
|
|
my $sender = SMS::Send->new('Twilio', |
27
|
|
|
|
|
|
|
_accountsid => 'ACb657bdcb16f06893fd127e099c070eca', |
28
|
|
|
|
|
|
|
_authtoken => 'b857f7afe254fa86c689648447e04cff', |
29
|
|
|
|
|
|
|
_from => '+15005550006', |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Send a message to me |
33
|
|
|
|
|
|
|
my $sent = $sender->send_sms( |
34
|
|
|
|
|
|
|
text => 'Messages can be up to 1600 characters', |
35
|
|
|
|
|
|
|
to => '+31645742418', |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Did it send? |
39
|
|
|
|
|
|
|
if ( $sent ) { |
40
|
|
|
|
|
|
|
print "Sent test message\n"; |
41
|
|
|
|
|
|
|
} else { |
42
|
|
|
|
|
|
|
print "Test message failed\n"; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
SMS::Send::Twilio is an SMS::Send driver for the Twilio web service. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=pod |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head2 new |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Create a new sender using this driver |
54
|
|
|
|
|
|
|
my $sender = SMS::Send->new('Twilio', |
55
|
|
|
|
|
|
|
_accountsid => 'ACb657bdcb16f06893fd127e099c070eca', |
56
|
|
|
|
|
|
|
_authtoken => 'b857f7afe254fa86c689648447e04cff', |
57
|
|
|
|
|
|
|
_from => '+15005550006', |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
The C constructor takes three parameters, which should be passed |
61
|
|
|
|
|
|
|
through from the L constructor. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 send_sms |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
It's really easy; if it returns a true value, sending the message was OK. |
66
|
|
|
|
|
|
|
If not we'd see an error message on STDERR. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Send a message to me |
69
|
|
|
|
|
|
|
my $sent = $sender->send_sms( |
70
|
|
|
|
|
|
|
text => 'Messages can be up to 1600 characters', |
71
|
|
|
|
|
|
|
to => '+31645742418', |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub new { |
77
|
2
|
|
|
2
|
1
|
610
|
my $class = shift; |
78
|
2
|
|
|
|
|
5
|
my %params = @_; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# check required parameters |
81
|
2
|
|
|
|
|
4
|
for my $param (qw ( _accountsid _from _authtoken )) { |
82
|
4
|
100
|
|
|
|
184
|
exists $params{$param} |
83
|
|
|
|
|
|
|
or croak $class . "->new requires $param parameter"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
2
|
my $self = \%params; |
87
|
1
|
|
|
|
|
2
|
bless $self, $class; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Create twilio object |
90
|
|
|
|
|
|
|
$self->{twilio} = WWW::Twilio::API->new( |
91
|
|
|
|
|
|
|
AccountSid => $self->{_accountsid}, |
92
|
|
|
|
|
|
|
AuthToken => $self->{_authtoken}, |
93
|
1
|
50
|
|
|
|
7
|
) or croak $class . "->new can't set up connection: $!"; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
23
|
return $self; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub send_sms { |
99
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
100
|
0
|
|
|
|
|
|
my %params = @_; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Get the message and destination |
103
|
0
|
|
|
|
|
|
my $message = delete $params{text}; |
104
|
0
|
|
|
|
|
|
my $recipient = delete $params{to}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $response = $self->{twilio}->POST( |
107
|
|
|
|
|
|
|
'Messages.json', |
108
|
|
|
|
|
|
|
From => $self->{_from}, |
109
|
0
|
|
|
|
|
|
To => $recipient, |
110
|
|
|
|
|
|
|
Body => $message, |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
if ( $response->{code} == '201' ) { |
|
|
0
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $result = JSON::PP->new->utf8->decode( $response->{content} ); |
115
|
0
|
0
|
|
|
|
|
if ( $result->{sid} ) { |
116
|
0
|
|
|
|
|
|
return $result->{sid}; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
elsif ( $response->{code} == '400' ) { |
120
|
0
|
|
|
|
|
|
my $result = JSON::PP->new->utf8->decode( $response->{content} ); |
121
|
0
|
0
|
|
|
|
|
if ( $result->{message} ) { |
122
|
0
|
|
|
|
|
|
croak "$result->{message}"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
croak "Can't send SMS: $response->{code} $response->{message}"; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 AUTHOR |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Michiel Beijen Emichiel.beijen@gmail.comE |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 COPYRIGHT |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Copyright 2013-2015 Michiel Beijen |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 LICENSE |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
140
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L |
145
|
|
|
|
|
|
|
L |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |