Created
September 11, 2017 05:25
-
-
Save realflash/b28276002c03a44345bd283fc9ea4d9e to your computer and use it in GitHub Desktop.
Perl interface to smsbroadcast.co.uk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use LWP::UserAgent; | |
use URI; | |
use Math::Round; | |
### Set these | |
my $auth_data = { 'username' => 'user', | |
'password' => 'password' }; | |
my $message_data = { 'from' => '07777777777', # Where the message should appear to have come from | |
'message' => 'test message', # What the message should be | |
'maxsplit' => 3, # Maximum number of message credits to use per destination | |
'delay' => 3, # How many minutes from now the message should be sent | |
}; | |
my $cost_per_message = 2.2; | |
my $to = ['07123456789', '07321654987']; | |
### Don't change this | |
my $uri = URI->new("https://api.smsbroadcast.co.uk/api-adv.php"); | |
my $api_doc = "https://www.smsbroadcast.co.uk/Advanced%20HTTP%20API.pdf"; | |
my $max_message_length = 765; | |
my $max_maxsplit = 5; | |
my $message_split_point = 153; | |
my $max_single_message_length = 160; | |
### Main | |
# Set up our browser object | |
my $ua = LWP::UserAgent->new; | |
$ua->env_proxy(); | |
$ua->timeout(30); | |
# Check our inputs are OK | |
unless(length($message_data->{'message'}) <= $max_message_length) | |
{ | |
print STDERR "Message too long. Maximum permitted length is $max_message_length. See $api_doc\n"; | |
exit 1; | |
} | |
unless($message_data->{'maxsplit'} <= $max_maxsplit) | |
{ | |
print STDERR "Maxsplit is too high. Maximum number of splits is $max_maxsplit, See $api_doc\n"; | |
exit 2; | |
} | |
# Work out how many messages we are going to send | |
print "Message length: ".length($message_data->{'message'})."\n"; | |
my $messages_per_destination; | |
if(length($message_data->{'message'}) <= 160) | |
{ | |
$messages_per_destination = 1; | |
} | |
else | |
{ | |
my $remaining_chars = length($message_data->{'message'}); | |
while($remaining_chars > 0) | |
{ | |
$messages_per_destination++; | |
$remaining_chars -= $message_split_point; | |
} | |
} | |
print "Sending $messages_per_destination per destination\n"; | |
unless($messages_per_destination <= $message_data->{'maxsplit'}) | |
{ | |
print STDERR "Message is too long for your maxsplit setting. This message would split into $messages_per_destination messages, but your maxsplit is set to ".$message_data->{'maxsplit'}."\n"; | |
exit 3; | |
} | |
my $total_messages = scalar(@$to) * $messages_per_destination; | |
print "Sending $total_messages in total, at a cost of ".$total_messages * $cost_per_message."p\n"; | |
# Work out if we have enough credits for the messages we want to send | |
my $balance; | |
my $call_data = { %$auth_data, ('action' => 'balance') }; # Merge the action params with the authentication params | |
my $response = $ua->post($uri, $call_data); # Call the SMS service | |
my ($result, $message) = split(/:/, $response->decoded_content); # Split out the colon-separated response | |
if($response->is_success && $result =~ /^OK$/) # If we got back an HTTP 200 and the result in the response data was 'OK', | |
{ # our message was sent | |
print "Balance: $message\n"; | |
$balance = $message; | |
} | |
else # Something went wrong | |
{ | |
print STDERR "Failed to get account balance\n"; | |
print STDERR "Response was: $result, Error: $message, HTTP result: ".$response->status_line; | |
exit 4; | |
} | |
if($balance < $total_messages) | |
{ | |
print STDERR "Insufficient message credits in account to send these messages\n"; | |
exit 5; | |
} | |
# Send the messages | |
my $results = []; # Store the results in here in case you want them later | |
foreach my $destination (@$to) | |
{ | |
$message_data->{'to'} = $destination; # Set the destination for this message in the params to the call | |
my $call_data = { %$auth_data, %$message_data }; # Merge the message params with the authentication params | |
my $response = $ua->post($uri, $call_data); # Call the SMS service | |
my ($result, $attempted_destination, $ref) = split(/:/, $response->decoded_content); # Split out the colon-separated response | |
chomp $ref; # Eliminate the bonus newline that gets put into ref | |
if($response->is_success && $result =~ /^OK$/) # If we got back an HTTP 200 and the result in the response data was 'OK', | |
{ # our message was sent | |
print "$destination: $result, reference $ref\n"; | |
push(@$results, { 'destination' => $destination, 'result' => 1, message => undef }); | |
next; | |
} | |
else # Probably didn't get sent | |
{ | |
my $msg = "$result, Error: $ref, HTTP result: ".$response->status_line; | |
print STDERR "$destination: $msg\n"; | |
push(@$results, { 'destination' => $destination, 'result' => 0, message => $msg }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment