useful perl sms script

zamrg

Senior Member
Joined
Oct 19, 2005
Messages
804
Reaction score
11
Location
Cape Town
I've been trying to work out a way for a website of mine to send sms notifications upon certain events occurring. This means sending out about 10 - 15 sms' p/day so a bulk sms option would be out of the question. I therefore wrote a perl script which uses vodacom4me to send out the sms'. My perl experience is non-existant and I would therefore appreciate any improvements or modifications. I pretty much used google to put this together cause I really don't have a clue when it comes to perl :)

The current useage of the script is v4mesms -username=072XXXXXX -password=XXXXXX -recipient=072XXXXXX -message="this is a test"

Code:
#!/usr/bin/perl

use strict;
use Getopt::Long;
use JSON;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;

use constant MAX_CHARS => 431;
use constant MAX_CHARS_PER_MESSAGE => MAX_CHARS / 3;

# define default values
my $USERNAME = '';
my $PASSWORD = '';
my $RECIPIENT = '';
my $MESSAGE = '';

# fetch and test command line parameters
GetOptions('username=s' => \$USERNAME, 'password=s' => \$PASSWORD, 'recipient=s' => \$RECIPIENT, 'message=s' => \$MESSAGE);
my $halt = 0;
if ($USERNAME =~ /^\s*$/ || length($USERNAME) != 10) {
	$halt = 1;
}
if ($PASSWORD =~ /^\s*$/) {
	$halt = 1;
}
if ($MESSAGE =~ /^\s*$/) {
	$halt = 1;
}
if ($halt) {
	print "usage: v4mesms -username=(your v4me username) -password=(your v4me password) -recipient=(cellphone number) -message=(text message to send)\n";
	exit;
}

# initiate script
my $result;
my $ua;
init();
# attempt to log in to vodacom4me
if (login($USERNAME, $PASSWORD)) {
	# send sms
	sendsms($RECIPIENT, $MESSAGE);
}
# return result as json response
print to_json($result, {pretty => 1});
exit;

sub init {
	# create json object which will return result of script
	$result = {
		SUCCESS => 'false'
	};
	# create a new useragent with a fake string and a temporary cookie file
	$ua = LWP::UserAgent->new;
	$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009020911 Ubuntu/8.10 (intrepid) Firefox/3.0.6 GTB5");
	$ua->cookie_jar( {} );
}

sub login {
	my $username = shift;
	my $password = shift;
	my $req = POST 'https://www.vodacom4me.co.za/vodacom4me-personal/login.do',
		[ 'V_LOGIN_NAME' => $username,
		  'V_PASSWORD' => $password
		];
	my $rsp = $ua->request($req);
	if ($rsp->is_success) {
		my $content = $rsp->as_string;
		if ($content =~ m/Your account could not be found on Vodacom4me/) {
			$result->{'ERROR'} = {
				CODE => '2',
				MESSAGE => "The account $username does not exist"
			};
		} elsif ($content =~ m/Login Failed\. The username or password that you have entered is invalid/) {
			$result->{'ERROR'} = {
				CODE => '3',
				MESSAGE => "The username or password which you specified is incorrect"
			};
		} else {
			return 1;
		}
	} else {
		$result->{'HTTP_ERROR'} = {
			CODE => $rsp->code(),
			MESSAGE => $rsp->message()
		};
	}
	return 0;
}

sub sendsms {
	my $recipient = shift;
	my $message = shift;
	# truncate message to maximum character limit
	$message = substr($message, 0, MAX_CHARS);
	# calculate characters left and number of messages to send based on the message length and the
	# maximum characters per msg constant
	my $msglength = length($message);
	my $nmessages;
	if ($msglength > MAX_CHARS_PER_MESSAGE * 2) {
		$nmessages = 3;
	} elsif ($msglength > MAX_CHARS_PER_MESSAGE) {
		$nmessages = 2;
	} else {
		$nmessages = 1;
	}
	my $charsleft = MAX_CHARS - $msglength;
	my $req = POST 'https://www.vodacom4me.co.za/vodacom4me-personal/sendSMS.do?operation=init&send=yes',
		[ 'picture' => 'null.gif',
		  'message.recipient' => $recipient,
		  'QuickList' => '',
		  'message.message' => $message,
		  'charsLeft' => $charsleft,
		  'numOfMsg' => $nmessages
		];
	$req->header('Referer' => 'https://www.vodacom4me.co.za/vodacom4me-personal/sendSMS.do?operation=init');
	my $rsp = $ua->request($req);
	if ($rsp->is_success) {
		my $content = $rsp->as_string;
		if ($content =~ m/The message ".+" will be sent to ".+" shortly/) {
			$result->{'SUCCESS'} = 'true';
			return 1;
		} else {
			$result->{'ERROR'} = {
				CODE => '4',
				MESSAGE => "An unexpected error occurred whilst trying to send the sms"
			};
		}		
	} else {
		$result->{'HTTP_ERROR'} = {
			CODE => $rsp->code(),
			MESSAGE => $rsp->message()
		};
	}
	return 0;
}

I'm looking for a way to get it to send to multiple recipients and also to send delayed sms'.
 
Top
Sign up to the MyBroadband newsletter
X