Net::SMS::Optimus - Send SMS through www.optimus.pt


Net-SMS-Optimus documentation Contained in the Net-SMS-Optimus distribution.

Index


Code Index:

NAME

Top

Net::SMS::Optimus - Send SMS through www.optimus.pt

VERSION

Top

Version 0.05

SYNOPSIS

Top

This module exports just one function that is responsible for sending your sms through www.optimus.pt portal.

    use Net::SMS::Optimus;

    send_sms($username, $password, $number, $msg);

EXPORT

Top

send_sms

FUNCTIONS

Top

send_sms

This function does all the magic. It receives the following arguments:

username

The username to the portal

password

...

number

A string containing the destination number

message

A string (no longer than 152 chars) containing the message to be sent.

The operation uses various phases, doing several connections to the website, trying to mimic a real human SMS send. If anything goes wrong, it croaks and returns.

AUTHOR

Top

Ruben Fonseca, <root at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-net-sms-optimus at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-SMS-Optimus. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Net::SMS::Optimus

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Net-SMS-Optimus

* CPAN Ratings

http://cpanratings.perl.org/d/Net-SMS-Optimus

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-SMS-Optimus

* Search CPAN

http://search.cpan.org/dist/Net-SMS-Optimus

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Net-SMS-Optimus documentation Contained in the Net-SMS-Optimus distribution.
package Net::SMS::Optimus;

use warnings;
use strict;
use Carp;

use LWP::UserAgent;
use HTTP::Cookies;
use HTML::Form;
use URI;

our $VERSION = '0.05';
our (@ISA)    = qw/Exporter/;
our (@EXPORT) = qw/send_sms/;

sub send_sms {
    my ($username, $password, $number, $message) = @_;

    # Do some checks
    croak "Username required" unless $username;
    croak "Password required" unless $password;
    croak "Number required" unless $number;
    croak "You must specify a message, asshole" unless $message;
    croak "Your message should be no more than 152 chars" unless (length $message) <= 152;

    # Initialize the browser
    my $browser = LWP::UserAgent->new(
        requests_redirectable => ['GET', 'HEAD', 'POST']
    );
    $browser->cookie_jar( {} );
    $browser->env_proxy;
    
    # Fase 1: Login
    my $res = $browser->get('http://optimus.pt/particulares/omeuoptimus/');
    $res->is_success or die "Error reading from www.optimus.pt (Phase 1)\n";
    
    my @forms = HTML::Form->parse($res);
    @forms = grep $_->attr("id") eq "aspnetForm", @forms;
    die "No login form found (Phase 1)\n" unless @forms;
    my $form = shift @forms;
    
    $form->value('ctl00$MainContentPlaceHolder$UserAuth1$TxtUsername', $username);
    $form->value('ctl00$MainContentPlaceHolder$UserAuth1$TxtPassword', $password);
    
    $res = $browser->request($form->click);
    $res->is_success or die "Error submiting login form (Phase 1)\n";
    
    $res->content =~ /Logout/ or die "Check username and/or password (Phase 1)\n";
    
    # Fase 2: Obter a form de SMS    
    $res->content =~ /id\=\"totalFreeSms\"\>(\d+)\</;
    print "Free SMS: $1\n";
    
    @forms = HTML::Form->parse($res);
    @forms = grep $_->attr("id") eq "aspnetForm", @forms;
    die "No sms form found (Phase 2)\n" unless @forms;
    $form = shift @forms;
    
    # Fase 3: Preencher a form e enviar
    my $url = URI->new('http://optimus.pt/OMeuOptimus/OptimusOnlineAjaxCalls/SendSms.aspx');
    $url->query_form(
      To => $number,
      Text => $message,
      Type => 'normal'
    );
    $res = $browser->get($url);   
    $res->is_success or die "Error submiting SMS form (Phase 3)\n";
    
    $res->content =~ /Resultados\>\<Correctos/ or die "Error sending SMS (Phase 3)\n";
    print "SMS Sent :)\n";
}

1; # End of Net::SMS::Optimus