|
9 | 9 |
|
10 | 10 | my %config;
|
11 | 11 |
|
| 12 | +sub HELP_MESSAGE { |
| 13 | + print $0 . ' - Perl implementation of the Telstra Messaging API v2 |
| 14 | +Options: |
| 15 | + -n The number to send an SMS to formatted as: +61400000000 |
| 16 | + If not specified, prompted for input. |
| 17 | +
|
| 18 | + -m If specified, the message to send - otherwise prompted for input. |
| 19 | +
|
| 20 | + -f If specified, will change the From text on your message. Requires |
| 21 | + account support within the API. |
| 22 | +'; |
| 23 | + exit 0; |
| 24 | +} |
| 25 | + |
12 | 26 | if ( -f dirname($0) . "/tokenstore.bin" ) {
|
13 | 27 | %config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
|
14 | 28 | }
|
15 | 29 |
|
16 | 30 | ## Check if we have any command line options, if not, prompt for input.
|
17 | 31 | my %options;
|
18 |
| -getopts('n:m:', \%options); |
| 32 | +getopts('f:n:m:', \%options); |
19 | 33 |
|
20 | 34 | ## If a number isn't provided with -n, prompt for destination number.
|
21 | 35 | if ( ! $options{n} ) {
|
|
38 | 52 | 'body' => $options{m},
|
39 | 53 | );
|
40 | 54 |
|
| 55 | +## If we specified a From using -f, provide it in the body |
| 56 | +if ( $options{f} ) { |
| 57 | + $body{'from'} = $options{f}; |
| 58 | +} |
| 59 | + |
41 | 60 | ## Get an OAuth token if required.
|
42 | 61 | get_token(%config);
|
43 | 62 |
|
44 | 63 | my $req = HTTP::Request->new( 'POST', 'https://tapi.telstra.com/v2/messages/sms' );
|
45 | 64 | $req->header( 'Content-Type' => 'application/json' );
|
46 | 65 | $req->header( 'Authorization' => 'Bearer ' . $config{token} );
|
47 | 66 | $req->content( to_json(\%body) );
|
| 67 | +print "Sending: " . $req->content() . "\n"; |
48 | 68 |
|
49 | 69 | my $ua = LWP::UserAgent->new;
|
50 | 70 | my $res = $ua->request($req);
|
51 | 71 | print "Result: " . $res->status_line . "\n";
|
| 72 | +print $res->decoded_content . "\n"; |
0 commit comments