forked from telstra/MessagingAPI-perl-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_sms.pl
executable file
·72 lines (59 loc) · 1.8 KB
/
send_sms.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use Getopt::Std;
use Storable;
use lib dirname($0);
use functions;
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('f:n:m:', \%options);
## If a number isn't provided with -n, prompt for destination number.
if ( ! $options{n} ) {
print "Enter destination number in format +61......: ";
$options{n} = <STDIN>;
chomp($options{n});
}
if ( $options{n} eq "" ) { exit 1; }
## If a message isn't provided with -m, prompt for a message.
if ( ! $options{m} ) {
print "Enter message to send: ";
$options{m} = <STDIN>;
chomp($options{m});
}
if ( $options{m} eq "" ) { exit 1; }
my %body = (
'to' => $options{n},
'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";