blib/lib/SMS/Send/UK/123txt.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 79 CC 7 SCAR 21.8
CC
7
Cov
0%
clean_number
20.0
send_sms
12.0
new
6.0
CRAP 8.9
1 package SMS::Send::UK::123txt;
2  
3 use warnings;
4 use strict;
5 use Carp;
6  
7 =head1 NAME
8  
9 SMS::Send::UK::123txt
10  
11 =head1 VERSION
12  
13 Version 0.0.1
14  
15 =cut
16  
17 our $VERSION = '0.0.1';
18  
19 =head1 SYNOPSIS
20  
21 # Instantiate a sender object
22 my $sender = SMS::Send->new(
23 'UK::123txt',
24 _login => 'username',
25 _password => 'password',
26 _from => 'source_number',
27 );
28  
29 # Send a message
30 my $sent = $sender->send_sms(
31 text => 'This is my message',
32 to => '0000000000' # In the format: 447312345679
33 );
34  
35 =head1 DESCRIPTION
36  
37 L<SMS::Send::UK::123txt> is a L<SMS::Send> driver that provides
38 SMS message sending via the 123-txt service
39  
40 =head2 CAVEATS
41  
42 You need a 123-txt account in order to use this driver
43  
44 =head1 METHODS
45  
46 =cut
47  
48 use base 'SMS::Send::Driver';
49 use LWP::UserAgent();
50 use JSON qw(encode_json);
51 use URI::Escape qw(uri_escape);
52  
53 =head2 new
54  
55 # Instantiate a sender object
56 my $sender = SMS::Send->new(
57 'UK::123txt',
58 _login => 'username',
59 _password => 'password',
60 _from => 'source_number',
61 );
62  
63 The C<new> constructor accepts three parameters, all of which are required:
64  
65 The return value is a C<SMS::Send::UK::123txt> object.
66  
67 =over 4
68  
69 =item 'UK::123txt'
70  
71 The parameter identifying the driver name
72  
73 =item _login
74  
75 The C<_login> parameter as supplied by 123-txt
76  
77 =item _password
78  
79 The C<_password> parameter as supplied by 123-txt
80  
81 =item _from
82  
83 The C<_from> parameter as specified in your 123-txt
84 account as a source number or source id
85  
86 =back
87  
88 =cut
89  
90 sub new {
91 my $class = shift;
92 my %params = @_;
93  
94 # Ensure we've been passed the parameters we're expecting
95 my @expected = ( '_login', '_password', '_from' );
96 foreach my $expect(@expected) {
97 if (!exists $params{$expect}) {
98 croak join(', ', @expected) . ' parameters must be supplied';
99 }
100 }
101  
102 # Instantiate the object
103 my $self = bless {
104 user => $params{'_login'},
105 pass => $params{'_password'},
106 from => clean_number($params{'_from'}),
107 base_url => 'http://api.123-txt.com/Api123WCF.svc/rest/SendSms'
108 }, $class;
109  
110 return $self;
111 }
112  
113 =head2 clean_to
114  
115 # Examine a passed phone number and attempt to return the number
116 # in a format supported by the API,
117 # croak if a conversion is not possible
118 my $intl_number = clean_to($source_number);
119  
120 The C<clean_to> method accepts a single required parameter:
121  
122 The return value is the number in international format
123  
124 =over 4
125  
126 =item source_number
127  
128 A string containing the number to be cleaned
129  
130 =back
131  
132 =cut
133  
134 sub clean_number {
135 my $source_number = shift;
136  
137 # The number may already be in the format we want
138 if ($source_number =~ /^44/) {
139 return $source_number;
140 }
141 # Strip any leading +
142 elsif ($source_number =~ /^\+44/) {
143 $source_number =~ s/^\+//;
144 return $source_number;
145 }
146 # Replace a leading 0 with 44
147 elsif ($source_number =~ /^0/) {
148 $source_number =~ s/^0/44/;
149 return $source_number;
150 }
151 # We can't do anything with this number
152 else {
153 croak 'Unrecognised number format: ' . $source_number;
154 }
155 }
156  
157 =head2 send_sms
158  
159 # Send a message
160 my $sent = $sender->send_sms(
161 text => 'This is my message',
162 to => '0000000000' # In the format: 447312345679
163 );
164  
165 The C<send_sms> method accepts two parameters, both of which are required:
166  
167 The return value is a 0 or 1 representing false or true, indicating whether
168 the send was successful.
169  
170 =over 4
171  
172 =item text
173  
174 A string of text representing the message, limited to 160 characters
175  
176 =item to
177  
178 A numeric string representing the phone number of the recipient, in a valid
179 international format (i.e. country code followed by the number)
180  
181 =back
182  
183 =cut
184  
185 sub send_sms {
186 my $self = shift;
187 my %params = @_;
188  
189 # Ensure we've been passed the parameters we're expecting
190 my @expected = ('text', 'to');
191 foreach my $expect(@expected) {
192 if (!exists $params{$expect}) {
193 croak join(', ', @expected) . ' parameters must be supplied';
194 }
195 }
196  
197 my $ua = LWP::UserAgent->new;
198 my $to = clean_number($params{to});
199 my $text = $params{text};
200  
201 # Add our authorisation credentials
202 # Send the request
203 my $body = encode_json({
204 user => $self->{'user'},
205 pass => $self->{'pass'},
206 source => $self->{'from'},
207 destination => $to,
208 sms => uri_escape($text),
209 });
210  
211 my $response = $ua->post(
212 $self->{'base_url'},
213 'Content-Type' => 'application/json',
214 Content => $body
215 );
216  
217 # Check the send succeded
218 if (!$response->is_success()) {
219 croak('API request failed: ' . $response->status_line());
220 return 0;
221 }
222  
223 return 1;
224 }
225  
226 =head1 AUTHOR
227  
228 Andrew Isherwood C<< <andrew.isherwood at ptfs-europe.com> >>
229  
230 =head1 BUGS
231  
232 Please report any bugs or features to C<andrew.isherwood at ptfs-europe.com>
233  
234 =head1 SUPPORT
235  
236 You can find documentation for this module with the perldoc command.
237  
238 perldoc SMS::Send::UK::123txt
239  
240 =head1 COPYRIGHT & LICENSE
241  
242 Copyright (C) 2018 PTFS Europe L<https://www.ptfs-europe.com/>
243  
244 This library is free software; you can redistribute it and/or modify
245 it under the same terms as Perl itself
246
247 Additionally, you are again reminded that this software comes with
248 no warranty of any kind, including but not limited to the implied
249 warranty of merchantability.
250
251 ANY use may result in charges on your 123-txt bill, and you should
252 use this software with care. The author takes no responsibility for
253 any such charges accrued.
254  
255 =head1 ACKNOWLEDGEMENTS
256  
257 Many thanks to the authors of the following modules that served as
258 inspiration for this one:
259  
260 =over 4
261  
262 =item SMS::Send::US::TMobile
263  
264  
265 =item SMS::Send::US::Ipipi
266  
267  
268 =item SMS::Send::UK::Kapow
269  
270 =back
271  
272 =cut
273  
274 1; # End of SMS::Send::UK::123txt
275