diff --git a/functions.pm b/functions.pm index 6366630..cae698b 100644 --- a/functions.pm +++ b/functions.pm @@ -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; diff --git a/send_sms.pl b/send_sms.pl index 65ef7a2..8b455f1 100755 --- a/send_sms.pl +++ b/send_sms.pl @@ -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} ) { @@ -38,6 +52,11 @@ '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); @@ -45,7 +64,9 @@ $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";