From bf70507711178844cb3999eadb77d890bd5d41bb Mon Sep 17 00:00:00 2001 From: Steven Haigh Date: Wed, 15 Nov 2017 15:34:31 +1100 Subject: [PATCH 1/2] Add help text for --help. Added -f to change FROM field. --- send_sms.pl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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"; From 8c3e58dd821ccab1a53761484b60f1211695dd0d Mon Sep 17 00:00:00 2001 From: Steven Haigh Date: Mon, 20 Nov 2017 12:30:37 +1100 Subject: [PATCH 2/2] Don't fail on non-writable token store If we have to renew a token, we don't want to fail completly if we can't write the token to disk. As such, turn an implied die to a warn - as we can still send the API call - we'll just end up getting another token next run. --- functions.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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;