SMS.pm makes it easy to send messages to UK mobile phones
(messages are sent through Breathe.com). The applications
of SMS.pm, particularly when combined with LWP are endless.
I'm releasing this code here under the GPL and hoping that
I can get some help developing it so that it can support
more countries and have more features. My home node
has links to Vim-produced syntax-highlighted versions of the source,
if that's your thing.
#!/usr/bin/perl
#
# INFO:
#
# SMS.pm -- Send SMS to UK mobile phones through Breathe.com. (V 0.1
+)
#
# SMS.pm is the Perl-package cousin of my Fast SMS script. While Fas
+t SMS
# is intended as a stand-alone product, SMS.pm can be used in your P
+erl
# scripts easily.
#
# Ideas to try:
# - Use SMS.pm to alert you when your site or server is down.
# - Make your own 'mobile e-mail notification' system.
# - Send the latest news stories.
#
# There are many applications of this module, particulary when compi
+led
# with LWP.
#
# SYNOPSIS:
#
# use SMS;
# if (send_sms($number, $message)) { # Func exported
# # Message sent successfully
# } else {
# # Error occurred, error code (HTTP response code) stored in
# # $ERROR_CODE, error description found in $ERROR_NAME.
# }
#
# COPYRIGHT/LICENSING:
#
# (C) Nicholas Johnston, July 2000.
# sky@erupt.com
# http://sky.erupt.com/fast.html
#
# Distributed under the GPL.
#
# TODO:
# - Add compatability for other SMS services. Try to send with other
+ services
# if one is busy. I hope to get a new SMS hierarchy on CPAN, but I
+ need
# people to help out with development.
#
# All modules below are in the standard Perl distro:
use strict;
use LWP::UserAgent;
use vars qw(@ISA @EXPORT $ERROR_CODE $ERROR_MESSAGE);
# Export functions and variables:
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw($ERROR_CODE, $ERROR_MESSAGE, send_sms);
sub send_sms {
# Declares to keep 'strict' happy :-)
my ($phone_number, $message) = @_;
# Reject blanks
($phone_number and $message) or return undef;
# We need to trick Breathe.com into thinking that we came from its s
+ite.
# So we set the referer field to the appropriate URL! (the shame)
my $referer = "http://www.breathe.com/services/textmessaging.html?nu
+mber=".
quick_and_dirty_encoding($phone_number);
# Here's the URL to grab.
my $url = "http://www.breathe.com/services/textmessaging.html?number
+=".
quick_and_dirty_encoding($phone_number)."\&message=".
quick_and_dirty_encoding($message).
"\&charleft=146%2F146\&submit.x=5\&submit.y=14";
# Create a new useragent object and pretend to be IE 5 (::shudder::
+--
# suddenly I feel so dirty!)
my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
my $req = new HTTP::Request 'GET' => $url;
$req->header('Accept' => 'text/html');
$req->header('Referer' => $referer); # Send fake referer
my $res = $ua->request($req);
# If there were no HTTP or other connection issues, we can get hold
# of the page content...
my $result = $res->content if $res->is_success;
# Strings returned by Breathe.com:
# - Success: Your message has been sent and will arrive shortly
# - Server busy: busy
# - Duplicate: That message has already been sent in the last few
+ minutes.
if ($result =~ /Your message has been sent and will arrive shortly/i
+) {
return 1;
} elsif ($result =~ /That message has already been sent in the last
+few/i) {
$ERROR_MESSAGE = "Duplicate message.";
return undef;
} elsif ($result =~ /busy/i) {
$ERROR_MESSAGE = "Breathe's servers are busy.";
return undef;
} else {
$ERROR_CODE = $res->code;
$ERROR_MESSAGE = $res->message;
return undef;
}
}
#
# Does exactly what it says on the tin. Replaces spaces with +
# signs. Should also encode quotes and slashes correctly, someday!
#
sub quick_and_dirty_encoding {
my $encoded = $_[0];
$encoded =~ s/ /+/sg;
return $encoded;
}