File Coverage

blib/lib/Net/SMS/160By2.pm
Criterion Covered Total %
statement 24 105 22.8
branch 0 28 0.0
condition 0 3 0.0
subroutine 8 14 57.1
pod 2 2 100.0
total 34 152 22.3


line stmt bran cond sub pod time code
1             package Net::SMS::160By2;
2            
3 1     1   23851 no warnings;
  1         2  
  1         26  
4 1     1   4 use strict;
  1         1  
  1         27  
5 1     1   894 use Data::Dumper;
  1         8815  
  1         60  
6             # Load this to handle exceptions nicely
7 1     1   8 use Carp;
  1         1  
  1         46  
8            
9             # Load this to make HTTP Requests
10 1     1   1043 use WWW::Mechanize;
  1         3779570  
  1         41  
11 1     1   1044 use HTML::TagParser;
  1         3551  
  1         29  
12 1     1   7 use URI;
  1         1  
  1         19  
13            
14             # Load this to uncompress the gzip content of http response.
15 1     1   6 use Compress::Zlib;
  1         1  
  1         1272  
16            
17             =head1 NAME
18            
19             Net::SMS::160By2 - Send SMS using your 160By2 account!
20            
21             =head1 VERSION
22            
23             Version 0.03
24            
25             =cut
26            
27             our $VERSION = '0.03';
28            
29             our $HOME_URL = 'http://160by2.com/Login';
30             our $SENDSMS_URL = 'http://160by2.com/FebSendSMS';
31             our $SENDSMS_SUBMIT_URL = 'http://160by2.com/Feb7SendSMS';
32            
33             =head1 SYNOPSIS
34            
35             This module provides a wrapper around 160By2.com to send an SMS to any mobile number in
36            
37             India, Kuwait, UAE, Saudi, Singapore, Philippines & Malaysia at present.
38            
39             you can use this as follows.
40            
41             use Net::SMS::160By2;
42            
43             my $obj = Net::SMS::160By2->new($username, $password);
44            
45             $obj->send_sms($msg, $to);
46            
47             # send additional params will print WWW::Mechanize detailed request and
48             # responses
49            
50             my $debug_obj = Net::SMS::160By2->new($username, $password, {debug => 1});
51            
52             $debug_obj->send_sms($msg, $to);
53            
54             Thats it!
55            
56             =head1 SUBROUTINES/METHODS
57            
58             =head2 new
59            
60             This is constructor method.
61            
62             input: username, password
63            
64             A new object will be created with username, password attributes.
65            
66             You can send additional params in a hash ref as 3rd parameter.
67            
68             at present only debug option is handled in additional params.
69            
70             output: Net::SMS::160By2 object
71            
72             =cut
73            
74             sub new {
75 0     0 1   my $class = shift;
76            
77             # read username and password
78 0           my $username = shift;
79 0           my $password = shift;
80 0           my $extra = shift;
81 0 0         $extra = {} unless ref($extra) eq 'HASH';
82            
83             # Throw error in case of no username or password
84 0 0         croak("No username provided") unless ($username);
85 0 0         croak("No password provided") unless ($password);
86            
87             # return blessed object
88 0           my $self = bless {
89             'username' => $username,
90             'password' => $password,
91             'mobile' => undef,
92             'message' => undef,
93             'query_form' => undef,
94 0           %{$extra}
95             }, $class;
96 0           return $self;
97             }
98            
99             =head2 send_sms
100            
101             This method is used to send an SMS to any mobile number.
102             input : message, to
103            
104             where message contains the information you want to send.
105             to is the recipient mobile number
106            
107             =cut
108            
109             sub send_sms {
110 0     0 1   my ($self, $msg, $to) = @_;
111 0 0 0       croak("Message or mobile number are missing") unless ($msg || $to);
112            
113             # trim spaces
114 0           $msg =~ s/^\s+|\s+$//;
115 0           $to =~ s/^\s+|\s+$//;
116            
117             # set message and mobile number
118 0           $self->{message} = $msg;
119 0           $self->{mobile} = $to;
120            
121             # create mechanize object
122 0           my $mech = WWW::Mechanize->new(autocheck => 1);
123 0 0         if ($self->{debug}) {
124 0     0     $mech->add_handler("request_send", sub { shift->dump; return });
  0            
  0            
125 0     0     $mech->add_handler("response_done", sub { shift->dump; return });
  0            
  0            
126             }
127 0           $mech->agent_alias( 'Windows Mozilla' );
128            
129             # Now connect to 160By2 Website login page
130 0           $mech->get($HOME_URL);
131            
132             # handle gzip content
133 0           my $response = $mech->response->content;
134 0 0         if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
135 0           $response = Compress::Zlib::memGunzip($response );
136 0           $mech->update_html( $response )
137             }
138            
139             # login to 160By2
140 0           my $status = $self->_login($mech);
141            
142 0 0         die "Login Failed" unless $status;
143            
144             # sendsms from 160by2
145 0           return $self->_send($mech);
146             }
147            
148             sub _login {
149 0     0     my ($self, $mech) = @_;
150            
151             # Get login form with htxt_UserName, txt_Passwd
152 0           $mech->form_with_fields('username', 'password');
153            
154             # set htxt_UserName, txt_Passwd
155 0           $mech->field('username', $self->{username});
156 0           $mech->field('password', $self->{password});
157            
158             # submit form
159 0           $mech->submit_form();
160            
161             # Verify login success/failed
162             # handle gzip content
163 0           my $response = $mech->response->content;
164 0 0         if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
165 0           $response = Compress::Zlib::memGunzip( $response );
166 0           $mech->update_html( $response )
167             }
168 0           my $home_uri = URI->new($mech->base());
169 0           my @q = $home_uri->query_form;
170 0           $self->{query_form} = \@q;
171 0           return $mech;
172             }
173            
174             sub _send {
175 0     0     my ($self, $mech) = @_;
176            
177             # Try to go to Home Page
178 0           my $sendsms_uri = URI->new($SENDSMS_URL);
179 0           $sendsms_uri->query_form(@{$self->{query_form}});
  0            
180            
181 0           my $sendsms_submit_uri = URI->new($SENDSMS_SUBMIT_URL);
182 0           $sendsms_submit_uri->query_form(@{$self->{query_form}});
  0            
183            
184             # Get content of SendSMS form page
185 0           $mech->get($sendsms_uri->as_string);
186            
187 0           my $response;
188 0 0         if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
189             # handle gzip content
190 0           $response = $mech->response->content;
191 0           $response = Compress::Zlib::memGunzip( $response );
192 0           $mech->update_html( $response );
193             }
194             # set form action
195 0           my $form = $mech->form_name('frm_sendsms');
196 0           $form->action($sendsms_submit_uri);
197            
198             # Use TagParser here, this will make our job easier
199 0           my $tp = HTML::TagParser->new($response);
200 0           my $sm_form = $tp->getElementById( "frm_sendsms" );
201 0           my $sm_form_tree = $sm_form->subTree();
202            
203             # User HTML::TagParser to recognize dynamically generated mobile number input, message textarea element id/names
204 0           my ($mob_elem) = $sm_form_tree->getElementsByAttribute('tabindex', "1");
205 0 0         if ($mob_elem) { # we have to consider tabindex=1 as mobile number input
206 0           $mech->field($mob_elem->getAttribute('id'), $self->{mobile});
207             }
208 0           my ($msg_elem) = $sm_form_tree->getElementsByAttribute('tabindex', "2");
209 0 0         if ($msg_elem) { # we have to consider tabindex=2 as message textarea
210 0           $mech->field($msg_elem->getAttribute('id'), $self->{message});
211             }
212            
213             # set additional params
214 0           my %params = @{$self->{query_form}};
  0            
215 0           $mech->field('hid_exists', "no");
216 0           $mech->field('feb2by2session', $params{id});
217             # submit form
218 0           $mech->submit();
219            
220            
221             # is URL call Success?
222 0 0         if ($mech->success()) {
223            
224             # Check sms sent successfully
225 0           my $response = $mech->response->content;
226 0 0         if($mech->response->header("Content-Encoding") eq "gzip") {
227 0           $response = Compress::Zlib::memGunzip($response) ;
228             }
229             # return 1(true) in case of success
230 0 0         return 1 if($response =~ m/Your message has been Sent/sig);
231             }
232             # return undef as failure
233 0           return;
234             }
235            
236             =head1 AUTHOR
237            
238             Mohan Prasad Gutta, C<< >>
239            
240             =head1 BUGS
241            
242             Please report any bugs or feature requests to C, or through
243             the web interface at L. I will be notified, and then you'll
244             automatically be notified of progress on your bug as I make changes.
245            
246            
247            
248            
249             =head1 SUPPORT
250            
251             You can find documentation for this module with the perldoc command.
252            
253             perldoc Net::SMS::160By2
254            
255            
256             You can also look for information at:
257            
258             =over 4
259            
260             =item * RT: CPAN's request tracker
261            
262             L
263            
264             =item * AnnoCPAN: Annotated CPAN documentation
265            
266             L
267            
268             =item * CPAN Ratings
269            
270             L
271            
272             =item * Search CPAN
273            
274             L
275            
276             =back
277            
278            
279             =head1 ACKNOWLEDGEMENTS
280            
281            
282             =head1 LICENSE AND COPYRIGHT
283            
284             Copyright 2010 Mohan Prasad Gutta.
285            
286             This program is free software; you can redistribute it and/or modify it
287             under the terms of either: the GNU General Public License as published
288             by the Free Software Foundation; or the Artistic License.
289            
290             See http://dev.perl.org/licenses/ for more information.
291            
292            
293             =cut
294            
295             1; # End of Net::SMS::160By2