Created
July 27, 2011 08:54
-
-
Save shardiwal/1108954 to your computer and use it in GitHub Desktop.
API to send sms via Way2SMS.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Way2sms; | |
my $message = Way2sms->new( | |
username => 'Your way2sms username', | |
password => 'Your way2sms password', | |
receiver_mobile_no => [ 'mobile no 1', 'mobile no 2' ], | |
message => "Wow this is working" | |
); | |
$message->send(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Way2sms; | |
use Moose; | |
use LWP::UserAgent; | |
use HTTP::Cookies; | |
has 'username' => ( | |
is => 'rw', | |
isa => 'Str', | |
required => 1 | |
); | |
has 'password' => ( | |
is => 'rw', | |
isa => 'Str', | |
required => 1 | |
); | |
has 'receiver_mobile_no' => ( | |
is => 'rw', | |
isa => 'ArrayRef', | |
required => 1 | |
); | |
has 'message' => ( | |
is => 'rw', | |
isa => 'Str', | |
required => 1 | |
); | |
has 'browser' => ( | |
is => 'ro', | |
isa => 'LWP::UserAgent', | |
default => sub { | |
my ( $self ) = @_; | |
my $browser = LWP::UserAgent->new; | |
$browser->cookie_jar( | |
HTTP::Cookies->new( file => "lwpcookies.txt", autosave => 1 ) | |
); | |
return $browser; | |
} | |
); | |
has 'siteconfig' => ( | |
is => 'ro', | |
isa => 'HashRef', | |
default => sub { | |
return { | |
login_url => 'http://site6.way2sms.com/auth.cl', | |
sms_url => 'http://site6.way2sms.com/FirstServletsms', | |
} | |
} | |
); | |
sub _update_user { | |
my ( $self, $message ) = @_; | |
print "$message \n"; | |
} | |
sub send { | |
my ( $self ) = @_; | |
my $browser = $self->browser; | |
my $login_request = $browser->request( | |
HTTP::Request->new( | |
POST => $self->_prepare_uri_as_string( | |
$self->siteconfig->{login_url}, | |
[ | |
username => $self->username, | |
password => $self->password, | |
Submit => 'Sign in' | |
] | |
) | |
) | |
); | |
my $response_code = $login_request->code; | |
$self->_update_user( "$response_code : Login" ); | |
if ( $response_code == &HTTP::Status::RC_MOVED_PERMANENTLY | |
or $response_code == &HTTP::Status::RC_MOVED_TEMPORARILY ) | |
{ | |
my $redirect_uri = $login_request->header('Location'); | |
if ( $redirect_uri ) { | |
my $request = $browser->request( | |
HTTP::Request->new( GET => $redirect_uri ) | |
); | |
$redirect_uri =~ /id=(.*)/ig; | |
if ( $1 ) { | |
$self->_update_user( "$1 : Client Session Id" ); | |
foreach my $receiver_mobile_no (@{$self->receiver_mobile_no}) { | |
$self->_send_message( $browser, $receiver_mobile_no ); | |
} | |
} | |
else { | |
$self->_update_user( "Login Failed" ); | |
} | |
} | |
else { | |
$self->_update_user( "Login Failed while redirection" ); | |
} | |
} | |
} | |
sub _prepare_uri_as_string { | |
my ( $self, $url, $params ) = @_; | |
my $uri = URI->new( $url ); | |
$uri->query_form(ref($params) eq "HASH" ? %$params : @$params); | |
return $uri->as_string; | |
} | |
sub _send_message { | |
my ( $self, $browser, $receiver_mobile_no ) = @_; | |
my $smsrequest = $browser->request( | |
HTTP::Request->new( POST => $self->_prepare_uri_as_string( | |
$self->siteconfig->{sms_url}, | |
[ | |
Action => 'sa65sdf656fdfd', | |
HiddenAction => 'instantsms', | |
MobNo => $receiver_mobile_no, | |
catnamedis => 'Birthday', | |
chkall => 'on', | |
gpwd => '*******', | |
guid => 'username', | |
textArea => $self->message, | |
ypwd => '*******', | |
yuid => 'username' | |
] ) | |
) | |
); | |
if ( $smsrequest->decoded_content =~ /submitted successfully/ ) { | |
$self->_update_user("Message sent to : $receiver_mobile_no"); | |
} | |
else { | |
$self->_update_user("Message sending failed to $receiver_mobile_no"); | |
} | |
} | |
no Moose; | |
__PACKAGE__->meta->make_immutable(); | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment