Skip to content

Commit 0353921

Browse files
committed
Initial import of code
1 parent 5641a89 commit 0353921

7 files changed

+193
-0
lines changed

README

Whitespace-only changes.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Getting started
2+
3+
4+
The Telstra SMS Messaging API allows your applications to send and receive SMS text messages from Australia's leading network operator.
5+
6+
It also allows your application to track the delivery status of both sent and received SMS messages.
7+
8+
## Configuration
9+
10+
Edit functions.pm and fill out:
11+
```
12+
my $consumer_key = "";
13+
my $consumer_secret = "";
14+
```
15+
16+
See the documentation at [Dev.Telstra.com](https://dev.telstra.com/content/messaging-api) for more information

functions.pm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use LWP::UserAgent;
2+
use LWP::ConnCache;
3+
use HTTP::Request;
4+
use File::Basename;
5+
use JSON;
6+
use Storable;
7+
8+
## Create a global UA agent.
9+
my $ua = LWP::UserAgent->new;
10+
$ua->timeout(10);
11+
12+
## Telstra DEV details from https://dev.telstra.com/
13+
my $consumer_key = "";
14+
my $consumer_secret = "";
15+
16+
sub get_token(\%) {
17+
my $hash = shift;
18+
19+
## Check we already have a valid token
20+
if ( $hash->{token} and $hash->{token_expires} > time() ) {
21+
warn "Oauth token present and valid... Using existing token.\n";
22+
return;
23+
}
24+
25+
my %form = (
26+
'grant_type' => 'client_credentials',
27+
'scope' => 'NSMS',
28+
'client_id' => $consumer_key,
29+
'client_secret' => $consumer_secret,
30+
);
31+
32+
my $res = $ua->post( 'https://sapi.telstra.com/v1/oauth/token', \%form );
33+
if ( $res->status_line =~ m/200/ ) {
34+
warn "Successfully obtained new OAuth token...\n";
35+
my $response = JSON->new->decode($res->decoded_content);
36+
$hash->{token} = $response->{access_token};
37+
$hash->{token_expires} = $response->{expires_in} + time() - 60;
38+
39+
## Write the file to disk.
40+
store $hash, dirname($0) . "/tokenstore.bin";
41+
}
42+
43+
return;
44+
}
45+
46+
return 1;

send_sms.pl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use File::Basename;
5+
use Getopt::Std;
6+
use Storable;
7+
use lib dirname($0);
8+
use functions;
9+
10+
my %config;
11+
12+
if ( -f dirname($0) . "/tokenstore.bin" ) {
13+
%config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
14+
}
15+
16+
## Check if we have any command line options, if not, prompt for input.
17+
my %options;
18+
getopts('n:m:', \%options);
19+
20+
## If a number isn't provided with -n, prompt for destination number.
21+
if ( ! $options{n} ) {
22+
print "Enter destination number in format +61......: ";
23+
$options{n} = <STDIN>;
24+
chomp($options{n});
25+
}
26+
if ( $options{n} eq "" ) { exit 1; }
27+
28+
## If a message isn't provided with -m, prompt for a message.
29+
if ( ! $options{m} ) {
30+
print "Enter message to send: ";
31+
$options{m} = <STDIN>;
32+
chomp($options{m});
33+
}
34+
if ( $options{m} eq "" ) { exit 1; }
35+
36+
my %body = (
37+
'to' => $options{n},
38+
'body' => $options{m},
39+
);
40+
41+
## Get an OAuth token if required.
42+
get_token(%config);
43+
44+
my $req = HTTP::Request->new( 'POST', 'https://tapi.telstra.com/v2/messages/sms' );
45+
$req->header( 'Content-Type' => 'application/json' );
46+
$req->header( 'Authorization' => 'Bearer ' . $config{token} );
47+
$req->content( to_json(\%body) );
48+
49+
my $ua = LWP::UserAgent->new;
50+
my $res = $ua->request($req);
51+
print "Result: " . $res->status_line . "\n";

subscription_create.pl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use File::Basename;
5+
use Storable;
6+
use lib dirname($0);
7+
use functions;
8+
9+
my %config;
10+
11+
if ( -f dirname($0) . "/tokenstore.bin" ) {
12+
%config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
13+
}
14+
15+
## Get an OAuth token if required.
16+
get_token(%config);
17+
18+
my %body = (
19+
"activeDays" => "65535",
20+
);
21+
22+
my $req = HTTP::Request->new( 'POST', 'https://tapi.telstra.com/v2/messages/provisioning/subscriptions' );
23+
$req->header( 'Content-Type' => 'application/json' );
24+
$req->header( 'Authorization' => 'Bearer ' . $config{token} );
25+
$req->content( to_json(\%body) );
26+
27+
my $ua = LWP::UserAgent->new;
28+
my $res = $ua->request($req);
29+
print "Result: " . $res->status_line . "\n";
30+
print $res->content;

subscription_delete.pl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use File::Basename;
5+
use Storable;
6+
use lib dirname($0);
7+
use functions;
8+
9+
my %config;
10+
11+
if ( -f dirname($0) . "/tokenstore.bin" ) {
12+
%config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
13+
}
14+
15+
## Get an OAuth token if required.
16+
get_token(%config);
17+
18+
my $req = HTTP::Request->new( 'DELETE', 'https://tapi.telstra.com/v2/messages/provisioning/subscriptions' );
19+
$req->header( 'Content-Type' => 'application/json' );
20+
$req->header( 'Authorization' => 'Bearer ' . $config{token} );
21+
22+
my $ua = LWP::UserAgent->new;
23+
my $res = $ua->request($req);
24+
print "Result: " . $res->status_line . "\n";
25+
print $res->content . "\n";

subscription_get.pl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use File::Basename;
5+
use Storable;
6+
use lib dirname($0);
7+
use functions;
8+
9+
my %config;
10+
11+
if ( -f dirname($0) . "/tokenstore.bin" ) {
12+
%config = %{ retrieve(dirname($0) . "/tokenstore.bin") };
13+
}
14+
15+
## Get an OAuth token if required.
16+
get_token(%config);
17+
18+
my $req = HTTP::Request->new( 'GET', 'https://tapi.telstra.com/v2/messages/provisioning/subscriptions' );
19+
$req->header( 'Content-Type' => 'application/json' );
20+
$req->header( 'Authorization' => 'Bearer ' . $config{token} );
21+
22+
my $ua = LWP::UserAgent->new;
23+
my $res = $ua->request($req);
24+
print "Result: " . $res->status_line . "\n";
25+
print $res->content . "\n";

0 commit comments

Comments
 (0)