-
Notifications
You must be signed in to change notification settings - Fork 0
/
160By2.pm
246 lines (161 loc) · 5.38 KB
/
160By2.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
package Net::SMS::160By2;
use warnings;
use strict;
use Data::Dumper;
# Load this to handle exceptions nicely
use Carp;
# Load this to make HTTP Requests
use WWW::Mechanize;
# Load this to uncompress the gzip content of http response.
use Compress::Zlib;
=head1 NAME
Net::SMS::160By2 - Send SMS using your 160By2 account!
=head1 VERSION
Version 0.01
=cut
our $VERSION = '0.01';
our $DEBUG = 1;
our $HOME_URL = 'http://www.160by2.com/index.aspx';
our $SENDSMS_URL = 'http://www.160by2.com/publicsms_sendsms.aspx';
=head1 SYNOPSIS
This module provides a wrapper around 160By2.com to send an SMS to any mobile number in
India, Kuwait, UAE, Saudi, Singapore, Philippines & Malaysia at present.
you can use this as follows.
use Net::SMS::160By2;
my $obj = Net::SMS::160By2->new($username, $password);
$obj->send_sms($msg, $to);
Thats it!
=head1 SUBROUTINES/METHODS
=head2 new
This is constructor method.
input: username, password
A new object will be created with username, password attributes.
output: Net::SMS::160By2 object
=cut
sub new {
my $class = shift;
# read username and password
my $username = shift;
my $password = shift;
# Throw error in case of no username or password
croak("No username provided") unless ($username);
croak("No password provided") unless ($password);
# return blessed object
my $self = bless {
'username' => $username,
'password' => $password,
'mobile' => undef,
'message' => undef
}, $class;
return $self;
}
=head2 send_sms
This method is used to send an SMS to any mobile number.
input : message, to
where message contains the information you want to send.
to is the recipient mobile number
=cut
sub send_sms {
my ($self, $msg, $to) = @_;
croak("Message or mobile number are missing") unless ($msg || $to);
# trim spaces
$msg =~ s/^\s+|\s+$//;
$to =~ s/^\s+|\s+$//;
# set message and mobile number
$self->{message} = $msg;
$self->{mobile} = $to;
# create mechanize object
my $mech = WWW::Mechanize->new(autocheck => 1);
# Now connect to 160By2 Website login page
$mech->get($HOME_URL);
# handle gzip content
my $response = $mech->response->content;
if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
$response = Compress::Zlib::memGunzip($response );
$mech->update_html( $response )
}
# login to 160By2
my $status = $self->_login($mech);
die "Login Failed" unless $status;
# sendsms from 160by2
return $self->_send($mech);
}
sub _login {
my ($self, $mech) = @_;
# Get login form with htxt_UserName, txt_Passwd
$mech->form_with_fields('htxt_UserName', 'txt_Passwd');
# set htxt_UserName, txt_Passwd
$mech->field('htxt_UserName', $self->{username});
$mech->field('txt_Passwd', $self->{password});
# submit form
$mech->submit_form();
# Verify login success/failed
# handle gzip content
my $response = $mech->response->content;
if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
$response = Compress::Zlib::memGunzip( $response );
$mech->update_html( $response )
}
return $mech;
}
sub _send {
my ($self, $mech) = @_;
# Get content of SendSMS form page
$mech->get($SENDSMS_URL);
if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
# handle gzip content
my $response = $mech->response->content;
$response = Compress::Zlib::memGunzip( $response );
$mech->update_html( $response );
}
# Get login form with htxt_UserName, txt_Passwd
my $form = $mech->form_with_fields('txt_mobileno', 'txt_send_sms', 'act_mnos');
# set htxt_UserName, txt_Passwd
$mech->field('txt_mobileno', $self->{mobile});
$mech->field('txt_send_sms', $self->{message});
my $mobile = $self->{mobile};
$mech->field('act_mnos', ($mobile =~ /^91/ && length($mobile) > 10 ? "$mobile," : "91$mobile,"));
# submit form
$mech->submit_form();
# is URL call Success?
if ($mech->success()) {
# Check sms sent successfully
my $response = $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip") {
$response = Compress::Zlib::memGunzip($response) ;
}
# return 1(true) in case of success
return 1 if($response =~ m/SMS Sent Successfully/sig);
}
# return undef as failure
return;
}
=head1 AUTHOR
Mohan Prasad Gutta, C<< <mohanprasadgutta at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-sms-160by2 at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-SMS-160By2>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::SMS::160By2
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-SMS-160By2>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-SMS-160By2>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Net-SMS-160By2>
=item * Search CPAN
L<http://search.cpan.org/dist/Net-SMS-160By2/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2010 Mohan Prasad Gutta.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
1; # End of Net::SMS::160By2