line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SMS::Send::Twilio; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
15064
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
59
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
48
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
32
|
use 5.008_005; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
92
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
7
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
113
|
|
9
|
2
|
|
|
2
|
|
696
|
use JSON::PP; |
|
2
|
|
|
|
|
12806
|
|
|
2
|
|
|
|
|
135
|
|
10
|
2
|
|
|
2
|
|
1042
|
use WWW::Twilio::API; |
|
2
|
|
|
|
|
96069
|
|
|
2
|
|
|
|
|
66
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
996
|
use parent qw(SMS::Send::Driver); |
|
2
|
|
|
|
|
516
|
|
|
2
|
|
|
|
|
11
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
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 have a limit of 160 chars', |
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 have a limit of 160 chars', |
71
|
|
|
|
|
|
|
to => '+31645742418', |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub new { |
77
|
2
|
|
|
2
|
1
|
512
|
my $class = shift; |
78
|
2
|
|
|
|
|
5
|
my %params = @_; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# check required parameters |
81
|
2
|
|
|
|
|
3
|
for my $param (qw ( _accountsid _from _authtoken )) { |
82
|
4
|
100
|
|
|
|
197
|
exists $params{$param} |
83
|
|
|
|
|
|
|
or croak $class . "->new requires $param parameter\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
2
|
my $self = \%params; |
87
|
1
|
|
|
|
|
3
|
bless $self, $class; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Create twilio object |
90
|
1
|
50
|
|
|
|
8
|
$self->{twilio} = WWW::Twilio::API->new( |
91
|
|
|
|
|
|
|
AccountSid => $self->{_accountsid}, |
92
|
|
|
|
|
|
|
AuthToken => $self->{_authtoken}, |
93
|
|
|
|
|
|
|
) or die $class . "->new can't set up connection: $!\n"; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
20
|
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
|
0
|
|
|
|
|
|
my $response = $self->{twilio}->POST( |
107
|
|
|
|
|
|
|
'SMS/Messages.json', |
108
|
|
|
|
|
|
|
From => $self->{_from}, |
109
|
|
|
|
|
|
|
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
|
|
|
|
|
|
print STDERR "$result->{message}\n"; |
123
|
0
|
|
|
|
|
|
return 0; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
return 0; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Michiel Beijen Emichiel.beijen@gmail.comE |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 COPYRIGHT |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Copyright 2013- Michiel Beijen |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 LICENSE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
141
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 SEE ALSO |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
L |
146
|
|
|
|
|
|
|
L |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |