| 1 | package SMS::Send::RedOxygen; | ||||||||||||
| 2 | |||||||||||||
| 3 | =pod | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | SMS::Send::RedOxygen - SMS::Send driver for RedOxygen.com RedSMS | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||
| 10 | |||||||||||||
| 11 | # Create a RedOxygen sender. For now, only AccountID+Password authentication | ||||||||||||
| 12 | # is supported; anonymous IP address based sending won't work. | ||||||||||||
| 13 | # | ||||||||||||
| 14 | my $send = SMS::Send->new( | ||||||||||||
| 15 | 'RedOxygen', | ||||||||||||
| 16 | _accountid => 'RedOxygenAccountID', | ||||||||||||
| 17 | _password => 'RedOxygenPassword', | ||||||||||||
| 18 | _email => 'RegisteredEmail' | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | # Send a message | ||||||||||||
| 22 | $send->send_sms( | ||||||||||||
| 23 | text => 'Hi there', | ||||||||||||
| 24 | to => '+61 (4) 1234 5678', | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | |||||||||||||
| 27 | # An exception is thrown if the web API replies with a HTTP status | ||||||||||||
| 28 | # code other than a 2xx OK or if the reply body error code is anything | ||||||||||||
| 29 | # except 0000. The error can be found in the exception text. | ||||||||||||
| 30 | |||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||
| 32 | |||||||||||||
| 33 | This SMS::Send driver bridges the SMS::Send API to RedOxygen's web API for SMS | ||||||||||||
| 34 | sending. RedOxygen uses custom message formats and response codes and can't just | ||||||||||||
| 35 | be used via simple JSON POST calls. | ||||||||||||
| 36 | |||||||||||||
| 37 | To use this driver you must have a RedOxygen account, either a trial or full account. | ||||||||||||
| 38 | RedOxygen's rates are not flat across all regions on most accounts though such accounts | ||||||||||||
| 39 | may be negoated. | ||||||||||||
| 40 | |||||||||||||
| 41 | This driver requires numbers to be in full international format. | ||||||||||||
| 42 | |||||||||||||
| 43 | LWP::UserAgent must be available for this module to function. This is typically | ||||||||||||
| 44 | packaged as libwww-perl on many systems, and can also be installed from CPAN. | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 LICENSE | ||||||||||||
| 47 | |||||||||||||
| 48 | The same as for Perl itself | ||||||||||||
| 49 | |||||||||||||
| 50 | =cut | ||||||||||||
| 51 | |||||||||||||
| 52 | 2 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | |||||||||||||
| 55 | 2 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 2 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 2 | $VERSION = '1.06'; | |||||||||||
| 58 | 2 | @ISA = 'SMS::Send::Driver'; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 2 | ▶ | use SMS::Send::Driver (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 2 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | |||||||||||||
| 64 | |||||||||||||
| 65 | |||||||||||||
| 66 | |||||||||||||
| 67 | |||||||||||||
| 68 | ##################################################################### | ||||||||||||
| 69 | # Constructor | ||||||||||||
| 70 | |||||||||||||
| 71 | 4 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 72 | 4 | my $class = shift; | |||||||||||
| 73 | 4 | my %args = @_; | |||||||||||
| 74 | |||||||||||||
| 75 | # Create the object | ||||||||||||
| 76 | 4 | ▶ | my $accid = $args{'_accountid'} || Carp::croak("The _accountid parameter must be set to your RedOxygen account ID code"); | ||||||||||
|
Condition100%2/2
$args{'_accountid'} || Carp::croak('The _accountid parameter must be set to your RedOxygen account ID code')
MC/DC100%1/1
$args{'_accountid'} || Carp::croak('The _accountid parameter must be set to your RedOxygen account ID code')
$args{'_accountid'}
Truth table100%2/2
A
A= $args{'_accountid'}
| |||||||||||||
| 77 | 3 | ▶ | my $pw = $args{'_password'} || Carp::croak("The _password parameter must be set to your RedOxygen account password"); | ||||||||||
|
Condition100%2/2
$args{'_password'} || Carp::croak('The _password parameter must be set to your RedOxygen account password')
MC/DC100%1/1
$args{'_password'} || Carp::croak('The _password parameter must be set to your RedOxygen account password')
$args{'_password'}
Truth table100%2/2
A
A= $args{'_password'}
| |||||||||||||
| 78 | 2 | ▶ | my $email = $args{'_email'} || Carp::croak("The _email parameter must be set to the email address associated with your RedOxygen account"); | ||||||||||
|
Condition100%2/2
$args{'_email'} || Carp::croak('The _email parameter must be set to the email address associated with your RedOxygen account')
MC/DC100%1/1
$args{'_email'} || Carp::croak('The _email parameter must be set to the email address associated with your RedOxygen account')
$args{'_email'}
Truth table100%2/2
A
A= $args{'_email'}
| |||||||||||||
| 79 | 1 | ▶ | my $url = $args{'_url'} || 'http://www.redoxygen.net/sms.dll?Action=SendSMS'; | ||||||||||
|
Condition50%1/2
$args{'_url'} || 'http://www.redoxygen.net/sms.dll?Action=SendSMS'
MC/DC0%0/1
$args{'_url'} || 'http://www.redoxygen.net/sms.dll?Action=SendSMS'
$args{'_url'}
Truth table50%1/2
A
A= $args{'_url'}
| |||||||||||||
| 80 | 1 | my $self = bless { | |||||||||||
| 81 | 'accountid' => $accid, | ||||||||||||
| 82 | 'password' => $pw, | ||||||||||||
| 83 | 'email' => $email, | ||||||||||||
| 84 | 'url' => $url | ||||||||||||
| 85 | }, $class; | ||||||||||||
| 86 | |||||||||||||
| 87 | 1 | $self; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | # Reference: http://www.redoxygen.com/developers/perl/ | ||||||||||||
| 91 | # | ||||||||||||
| 92 | # This is the "simple" HTTP Post format that uses url-encoded bodies. It supports | ||||||||||||
| 93 | # one message per HTTP request. Parameters are passed as a hash. | ||||||||||||
| 94 | sub send_sms | ||||||||||||
| 95 | 0 | ▶ | { | ||||||||||
|
Subroutine
send_sms: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||
| 97 | |||||||||||||
| 98 | 0 | my $browser = LWP::UserAgent->new; | |||||||||||
| 99 | 0 | my %args = @_; | |||||||||||
| 100 | |||||||||||||
| 101 | # RedOxygen doesn't check for errors very well and tends to return success | ||||||||||||
| 102 | # when it hasn't done anything. Catch obvious problems. | ||||||||||||
| 103 | 0 | ▶ | if (!$args{text}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 0 | Carp::croak("No message supplied, need 'text' parameter"); | |||||||||||
| 105 | } | ||||||||||||
| 106 | 0 | ▶ | if (!$args{to}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | Carp::croak("No recipient specified, need 'to' parameter"); | |||||||||||
| 108 | } | ||||||||||||
| 109 | # RedOxygen doesn't like a leading +, spaces, parens, etc, so strip them | ||||||||||||
| 110 | 0 | $args{to} =~ s/[^0-9]//g; | |||||||||||
| 111 | |||||||||||||
| 112 | # Note: We don't test for password. It's optional, as you can use IP Address | ||||||||||||
| 113 | # based authentication instead. | ||||||||||||
| 114 | |||||||||||||
| 115 | # You might expect this to be a hash, but RedOxygen cares about the *order* | ||||||||||||
| 116 | # in which the parameters appear in the request. We must specify them precisely | ||||||||||||
| 117 | # as given below. | ||||||||||||
| 118 | my $request = [ | ||||||||||||
| 119 | 'AccountID' => $self->{accountid}, | ||||||||||||
| 120 | 'Email' => $self->{email}, | ||||||||||||
| 121 | 'Password' => $self->{password} || '', | ||||||||||||
| 122 | 'Recipient' => $args{to}, | ||||||||||||
| 123 | 'Message' => $args{text} | ||||||||||||
| 124 | 0 | ▶ | ]; | ||||||||||
|
Condition0%0/2
$self->{'password'} || ''
MC/DC0%0/1
$self->{'password'} || ''
$self->{'password'}
Truth table0%0/2
A
A= $self->{'password'}
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | my $response = $browser->post($self->{url}, $request); | |||||||||||
| 127 | 0 | ▶ | Carp::croak("HTTP error POSTing SMS to $self->{url}\n" . $response->status_line) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | unless $response->is_success(); | ||||||||||||
| 129 | |||||||||||||
| 130 | # The RedOxygen API is pretty damn ugly; instead of returning HTTP codes, it returns | ||||||||||||
| 131 | # numeric error codes in the request body as the first 4 bytes. Hopefully all the time. | ||||||||||||
| 132 | # Extract the error code and return it as well as the full message. | ||||||||||||
| 133 | # | ||||||||||||
| 134 | 0 | my $redstatus = substr($response->content,0,4); | |||||||||||
| 135 | 0 | ▶ | if (int($redstatus) != 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | 0 | Carp::croak("RedOxygen API returned error: " . $response->content); | |||||||||||
| 137 | } | ||||||||||||
| 138 | # RedOxygen doesn't give us final delivery confirmation but by this point we know | ||||||||||||
| 139 | # that submission worked. Probably. | ||||||||||||
| 140 | 0 | return 1; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | =pod | ||||||||||||
| 144 | |||||||||||||
| 145 | =head1 SUPPORT | ||||||||||||
| 146 | |||||||||||||
| 147 | Bugs should be reported via the CPAN bug tracker at | ||||||||||||
| 148 | |||||||||||||
| 149 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SMS-Send-RedOxygen> | ||||||||||||
| 150 | |||||||||||||
| 151 | For other issues, contact the author. | ||||||||||||
| 152 | |||||||||||||
| 153 | =head1 AUTHOR | ||||||||||||
| 154 | |||||||||||||
| 155 | Craig Ringer E<lt>craig@2ndquadrant.comE<gt> using SMS::Send by Adam Kennedy. | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 COPYRIGHT | ||||||||||||
| 158 | |||||||||||||
| 159 | Copyright 2012 Craig Ringer | ||||||||||||
| 160 | |||||||||||||
| 161 | This program is free software; you can redistribute | ||||||||||||
| 162 | it and/or modify it under the same terms as Perl itself. | ||||||||||||
| 163 | |||||||||||||
| 164 | The full text of the license can be found in the | ||||||||||||
| 165 | LICENSE file included with this module. | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||
| 168 | |||||||||||||
| 169 | 2 | 1; | |||||||||||