Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sub get_token(\%) {
$hash->{token_expires} = $response->{expires_in} + time() - 60;

## Write the file to disk.
store $hash, dirname($0) . "/tokenstore.bin";
store $hash, dirname($0) . "/tokenstore.bin" or warn "Unable to write token store to disk. Proceeding anyway. $!";
}

return;
Expand Down
23 changes: 22 additions & 1 deletion send_sms.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@

my %config;

sub HELP_MESSAGE {
print $0 . ' - Perl implementation of the Telstra Messaging API v2
Options:
-n The number to send an SMS to formatted as: +61400000000
If not specified, prompted for input.

-m If specified, the message to send - otherwise prompted for input.

-f If specified, will change the From text on your message. Requires
account support within the API.
';
exit 0;
}

if ( -f dirname($0) . "/tokenstore.bin" ) {
%config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
}

## Check if we have any command line options, if not, prompt for input.
my %options;
getopts('n:m:', \%options);
getopts('f:n:m:', \%options);

## If a number isn't provided with -n, prompt for destination number.
if ( ! $options{n} ) {
Expand All @@ -38,14 +52,21 @@
'body' => $options{m},
);

## If we specified a From using -f, provide it in the body
if ( $options{f} ) {
$body{'from'} = $options{f};
}

## Get an OAuth token if required.
get_token(%config);

my $req = HTTP::Request->new( 'POST', 'https://tapi.telstra.com/v2/messages/sms' );
$req->header( 'Content-Type' => 'application/json' );
$req->header( 'Authorization' => 'Bearer ' . $config{token} );
$req->content( to_json(\%body) );
print "Sending: " . $req->content() . "\n";

my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);
print "Result: " . $res->status_line . "\n";
print $res->decoded_content . "\n";