Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
API to Send SMS from FullonSMS.com
#!/usr/bin/perl
use warnings;
use strict;
use Fullonsms::Session;
use Fullonsms::Message;
my $session = Fullonsms::Session->new(
username => 'your fullonsms username',
password => 'your fullonsms password',
);
$session->login;
my $message = Fullonsms::Message->new(
session => $session,
receiver_mobile_no => [ 'mobile no 1', ' mobile no 2' ],
message => "Wow this is working"
);
$message->send();
package Fullonsms::Message;
use Moose;
use Fullonsms::Util;
has 'receiver_mobile_no' => (
is => 'rw',
isa => 'ArrayRef',
required => 1
);
has 'message' => (
is => 'rw',
isa => 'Str',
required => 1
);
has 'session' => (
is => 'ro',
isa => 'Fullonsms::Session',
required => 1
);
has 'sms_url' => (
is => 'ro',
isa => 'Str',
default => sub {
my ($self) = @_;
return Fullonsms::Util::base_url . "/home.php";
}
);
sub send {
my ($self) = @_;
if ( $self->session ) {
foreach my $receiver_mobile_no ( @{ $self->receiver_mobile_no } ) {
$self->_send_message($receiver_mobile_no);
}
}
else {
Fullonsms::Util::inform_user(" Login failed ");
}
}
sub _send_message {
my ( $self, $receiver_mobile_no ) = @_;
my $browser = $self->session->browser;
my $smsrequest = $browser->request(
HTTP::Request->new(
POST => Fullonsms::Util::prepare_uri_as_string(
$self->sms_url,
[
CancelScript => $self->sms_url,
MobileNos => $receiver_mobile_no,
SelGroup => $receiver_mobile_no,
Message => $self->message,
Gender => 0,
FriendName => 'Your Friend Name',
ETemplatesId => '',
TabValue => 'contacts',
IntSubmit => 'Ok',
IntCancel => 'Cancel'
]
)
)
);
if ( $smsrequest->decoded_content =~ /MsgSent\.php/ ) {
Fullonsms::Util::inform_user("Message sent to : $receiver_mobile_no");
return 1;
}
else {
Fullonsms::Util::inform_user(
"Message sending failed to $receiver_mobile_no");
return 0;
}
}
no Moose;
__PACKAGE__->meta->make_immutable();
1;
package Fullonsms::Session;
use Moose;
use HTML::TagParser;
use LWP::UserAgent;
use Fullonsms::Util;
has 'username' => (
is => 'ro',
isa => 'Str',
required => 1
);
has 'password' => (
is => 'ro',
isa => 'Str',
required => 1
);
has 'login_url' => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => sub {
my ($self) = @_;
return Fullonsms::Util::base_url . "/CheckLogin.php";
}
);
has 'browser' => (
is => 'ro',
isa => 'LWP::UserAgent',
lazy => 1,
default => sub {
my ($self) = @_;
my $browser = LWP::UserAgent->new;
$browser->cookie_jar( {} );
return $browser;
}
);
sub login {
my ($self) = @_;
my $browser = $self->browser;
my $login_request = $browser->request(
HTTP::Request->new(
POST => Fullonsms::Util::prepare_uri_as_string(
$self->login_url,
[
MobileNoLogin => $self->username,
LoginPassword => $self->password,
x => 13,
y => 39
]
)
)
);
my $response_code = $login_request->code;
my $response_content = $login_request->decoded_content;
if ( $self->_is_logged_id($response_content) ) {
Fullonsms::Util::inform_user("$response_code : Login");
return 1;
}
else {
Fullonsms::Util::inform_user(" Login failed ");
return undef;
}
}
sub _is_logged_id {
my ( $self, $response_content ) = @_;
my $html = HTML::TagParser->new($response_content);
my @meta_tags = $html->getElementsByTagName("meta");
my $result_found = 0;
foreach my $meta_tag (@meta_tags) {
my $attr = $meta_tag->attributes;
foreach my $key ( sort keys %$attr ) {
if ( $attr->{'http-equiv'} eq 'Refresh'
and $attr->{'content'} =~ /home\.php\?Login=1/ )
{
$result_found = 1;
}
}
}
return $result_found;
}
no Moose;
__PACKAGE__->meta->make_immutable();
1;
package Fullonsms::Util;
use warnings;
use strict;
sub base_url {
return "http://www.fullonsms.com";
}
sub inform_user {
my ( $message ) = @_;
print "$message \n";
}
sub prepare_uri_as_string {
my ( $url, $params ) = @_;
my $uri = URI->new( $url );
$uri->query_form(ref($params) eq "HASH" ? %$params : @$params);
return $uri->as_string;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment