/
TxtLocal.pm
271 lines (194 loc) · 6.66 KB
/
TxtLocal.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package Net::SMS::TxtLocal;
use Carp;
use LWP::UserAgent;
use Moose;
use namespace::autoclean;
use JSON;
use 5.006;
our $VERSION = '0.02';
=head1 NAME
Net::SMS::TxtLocal - Send SMS messages using txtlocal.co.uk
=head1 SYNOPSIS
my $txtlocal = Net::SMS::TxtLocal->new(
uname => 'you@domain.com',
pword => 'secret',
from => 'Your Name', # optional
);
# get the current balance
my $balance = $txtlocal->get_credit_balance();
# send a message
$txtlocal->send_message(
{
message => "This is a test message from Net::SMS::TxtLocal",
to => [ '447890123456' ],
}
);
=head1 DESCRIPTION
This module is a super simple interface to the TxtLocal API which lets you send
SMS messages from your code.
=head1 ABOUT TXTLOCAL
TxtLocal is a UK based SMS service which lets you send messages to mobiles
worldwide. Their website is clean, their pricing reasonable and their API pretty
good too.
If you need to create an account and use this link some pennies might flow back
into the author's pockets: L<http://www.txtlocal.co.uk/?tlrx=52009>
If this shameless commercialism offends you use this link instead:
L<http://www.txtlocal.co.uk/>.
=cut
has uname => (
is => 'ro',
isa => 'Str',
required => 1
);
has pword => (
is => 'ro',
isa => 'Str',
required => 1
);
has from => (
is => 'ro',
isa => 'Str',
);
has host => (
is => 'ro',
isa => 'Str',
default => 'http://www.txtlocal.co.uk'
);
has ua => (
is => 'ro',
isa => 'Object',
default => sub { LWP::UserAgent->new },
lazy => 1,
);
=head1 METHODS
=head2 new
$txtlocal = Net::SMS::TxtLocal->new(
uname => 'you@domain.com',
pword => 'secret',
from => 'Your Name', # optional
);
Create a sender object. C<uname> and C<pword> are required and should be your
login to the TxtLocal system.
C<from> is optional and is added to any messages sent.
=cut
# no code here - new is provided by Moose
=head2 get_credit_balance
my $credit_balance = $txtlocal->get_credit_balance();
Get the credit balance for this account from TxtLocal. This will try to connect
to their servers and is a good way to test that the connection is available.
=cut
sub get_credit_balance {
my $self = shift;
my $credits = $self->_make_request(
{
path => '/getcredits.php',
query => {},
}
);
# clean up the repsonse and check it makes sense
$credits =~ s{ \s* (\d+) \s* }{$1}x;
croak "Bad credit value: '$credits' " if $credits =~ m{\D};
return $credits + 0;
}
=head2 send_message
$bool = $txtlocal->send_message(
{
message => 'the text of the message',
to => ['447890123456'],
}
);
Send a message to the numbers given in the array.
=cut
sub send_message {
my $self = shift;
my $args = shift;
my $message = $args->{message}
|| croak "required parameter 'message' missing";
my $to = $args->{to} || croak "required parameter 'to' missing";
my $response = $self->_make_request(
{
path => '/sendsmspost.php',
query => {
from => $self->from,
message => $message,
selectednums => join( ',', @$to ),
},
}
);
return 1;
}
=head1 PRIVATE METHODS
=head2 _make_request
my $response = $txtlocal->_make_request(
{
path => '/path/to/page.php',
query => { foo => 'bar' },
}
);
Make a POST request to the TxtLocal servers. The C<uname> and C<pword> will be
added to the request and content returned - either as a string or a
datastructure.
=cut
sub _make_request {
my $self = shift;
my $args = shift;
my $path = $args->{path} || croak "required parameter 'path' missing";
my $query = $args->{query} || croak "required parameter 'query' missing";
my $url = $self->host . $path;
my $post_query = {
uname => $self->uname,
pword => $self->pword,
json => 1,
%$query,
};
my $response = $self->ua->post( $url, $post_query );
croak "Error with request" unless $response->is_success;
my $content = $response->content;
# check to see if a string error has been returned.
croak "Invalid request - please check uname and pword"
if $content =~ m{ \A \s* invalid \s* \z }xi;
# return the response unless it looks like JSON
return $content unless $content =~ m{ \A \s* \{ \s* "\w }x;
# decode the JSON
my $data = decode_json($content);
# check for the error key and croak if it is there
croak "Error with request: '$data->{ERROR}'" if $data->{ERROR};
return $data;
}
=head1 BUGS
No bugs have been reported.
Please report any bugs or feature requests to
C<bug-net-sms-txtlocal@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 CONTRIBUTING
The repository for this module is hosted here:
L<http://github.com/evdb/Net-SMS-TxtLocal>
Please feel free to fork, make changes and send me a patch :)
=head1 AUTHOR
Edmund von der Burg C<< <evdb@cpan.org> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2010, Edmund von der Burg C<< <evdb@cpan.org> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
=cut
1;