Initial commit -- packaging in progress
[u/jw35/ucam-sms-send.git] / Send.pm
1 #!/usr/bin/perl
2
3 { package Ucam::SMS::Send;
4
5 use warnings;
6 use strict;
7 use LWP::UserAgent;
8 use HTML::Entities;
9
10 use vars qw/$VERSION/;
11 $VERSION = '1.00';
12
13 sub new
14 {
15 my $proto = shift;
16 my $class = ref($proto) || $proto;
17 my $self = {};
18 bless ($self,$class);
19
20 my %args = (@_);
21
22 # Save allowed arguments in $self, ignore extraneous ones (e.g. that
23 # can only be used in other calls).
24 $self->{'url'} = $args{'url'};
25 # The message-specific details...
26 $self->{'msg'}{'token'} = $args{'token'};
27 $self->{'msg'}{'bulk'} = $args{'bulk'};
28 $self->{'msg'}{'truncate'} = $args{'truncate'};
29 $self->{'msg'}{'long_message'} = $args{'long_message'};
30 $self->{'msg'}{'test'} = $args{'test'};
31
32 return $self;
33 }
34
35 sub SendMessage
36 {
37 my $self = shift;
38 my %args = (@_);
39
40 # Set default URL for retrieving status.
41 $self->{'url'} = "https://api.phone.cam.ac.uk/api/text/1/submitSMS";
42
43 # Save allowed arguments in $self, ignore extraneous ones (e.g. that
44 # can only be used in other calls or are misspelled).
45 $self->{'url'} = $args{'url'} if defined $args{'url'};
46 # The message-specific details...
47 $self->{'msg'}{'token'} = $args{'token'} if defined $args{'token'};
48 $self->{'msg'}{'number'} = $args{'number'} if defined $args{'number'};
49 $self->{'msg'}{'message'} = $args{'message'} if defined $args{'message'};
50 $self->{'msg'}{'bulk'} = $args{'bulk'} if defined $args{'bulk'};
51 $self->{'msg'}{'truncate'} = $args{'truncate'} if defined $args{'truncate'};
52 $self->{'msg'}{'long_message'} = $args{'long_message'} if defined $args{'long_message'};
53 $self->{'msg'}{'test'} = $args{'test'} if defined $args{'test'};
54
55 my $ourname = (caller(0))[3];
56 die "$ourname: mandatory argument token is undefined\n" unless defined $self->{'msg'}{'token'};
57 die "$ourname: mandatory argument number is undefined\n" unless defined $self->{'msg'}{'number'};
58 die "$ourname: mandatory argument message is undefined\n" unless defined $self->{'msg'}{'message'};
59
60 print "";
61 my $ua = LWP::UserAgent->new;
62 $ua->timeout(10); # max 10s for response
63
64
65 my $url = $self->{'url'};
66 my $response = $ua->post($url,$self->{'msg'});
67 print ""; # for breakpointing
68 unless ($response->is_success)
69 {
70 my $msg = $self->{'msg'}{'message'};
71 my $number = $self->{'msg'}{'number'};
72 my $status = $response->code;
73 my $message = $response->message;
74 die "$0: error sending SMS message '$msg' to $number: HTTP status $status message '$message'\n";
75 }
76
77 # Parse SMS service response (HTTP response content).
78
79 my @content = split(/\n/,$response->content);
80 my %results = ();
81 foreach my $line (@content)
82 {
83 chomp($line);
84 if ($line =~ /^(\S+):\s+(.*)\s*$/)
85 {
86 my ($key,$value) = ($1,$2);
87 $results{$1} = $2;
88
89 $self->{'msg'}{'messageid'} = $2 if $1 eq 'Msg-ID';
90 # warn "$key: $value\n";
91 }
92 else
93 {
94 warn "unrecognised result line: $line";
95 }
96 }
97
98 my $status = $results{'Status'} eq 'OK';
99 return ($status,%results);
100
101 }
102
103 sub GetStatus
104 {
105 my $self = shift;
106 my %args = (@_);
107
108 # Set default URL for retrieving status.
109 $self->{'url'} = "https://api.phone.cam.ac.uk/api/text/1/getMessageStatus";
110
111 # Save allowed arguments in $self, ignore extraneous ones (e.g. that
112 # can only be used in other calls or are misspelled).
113
114 $self->{'url'} = $args{'url'} if defined $args{'url'};
115 $self->{'msg'}{'token'} = $args{'token'} if defined $args{'token'};
116 $self->{'msg'}{'messageid'} = $args{'messageid'} if defined $args{'messageid'};
117
118 my $ourname = (caller(0))[3];
119 die "$ourname: mandatory argument token is undefined\n" unless defined $self->{'msg'}{'token'};
120 die "$ourname: mandatory argument messageid is undefined (was it a test-mode message?)\n" unless defined $self->{'msg'}{'messageid'};
121
122 print "";
123 my $ua = LWP::UserAgent->new;
124 $ua->timeout(10); # max 10s for response
125
126 my $url = $self->{'url'};
127 my $response = $ua->post($url,$self->{'msg'});
128 print ""; # for breakpointing
129 unless ($response->is_success)
130 {
131 my $msgid = $self->{'msg'}{'messageid'};
132 my $msg = $self->{'msg'}{'message'};
133 my $number = $self->{'msg'}{'number'};
134 my $status = $response->code;
135 my $message = $response->message;
136 die "$0: error getting SMS message ID '$msgid' status: HTTP status $status message '$message'\n";
137 }
138
139 # Parse SMS service response (HTTP response content).
140
141 my @content = split(/\n/,$response->content);
142 my %results = ();
143 foreach my $line (@content)
144 {
145 chomp($line);
146 if ($line =~ /^(\S+):\s+(.*)\s*$/)
147 {
148 my ($key,$value) = ($1,$2);
149 $results{$1} = $2;
150 }
151 else
152 {
153 warn "unrecognised result line: $line";
154 }
155 }
156
157
158 my $status = $results{'Status'} eq 'OK';
159
160 print "";
161 return ($status,%results);
162
163 }
164 #~~~
165 sub GetBalance
166 {
167 my $self = shift;
168 my %args = (@_);
169
170 # Set default URL for retrieving status.
171 $self->{'url'} = "https://api.phone.cam.ac.uk/api/text/1/getBalance";
172
173 # Save allowed arguments in $self, ignore extraneous ones (e.g. that
174 # can only be used in other calls or are misspelled).
175
176 $self->{'msg'}{'token'} = $args{'token'} if defined $args{'token'};
177
178 my $ourname = (caller(0))[3];
179 die "$ourname: mandatory argument token is undefined\n" unless defined $self->{'msg'}{'token'};
180
181 print "";
182 my $ua = LWP::UserAgent->new;
183 $ua->timeout(10); # max 10s for response
184
185 my $url = $self->{'url'};
186 my $response = $ua->post($url,$self->{'msg'});
187 print ""; # for breakpointing
188 unless ($response->is_success)
189 {
190 my $msgid = $self->{'msg'}{'messageid'};
191 my $msg = $self->{'msg'}{'message'};
192 my $number = $self->{'msg'}{'number'};
193 my $status = $response->code;
194 my $message = $response->message;
195 die "$0: error getting SMS message ID '$msgid' status: HTTP status $status message '$message'\n";
196 }
197
198 # Parse SMS service response (HTTP response content).
199
200 my @content = split(/\n/,$response->content);
201 my %results = ();
202 foreach my $line (@content)
203 {
204 chomp($line);
205 if ($line =~ /^(\S+):\s+(.*)\s*$/)
206 {
207 my ($key,$value) = ($1,$2);
208 $results{$1} = $2;
209 }
210 else
211 {
212 warn "unrecognised result line: $line";
213 }
214 }
215
216
217 my $status = $results{'Status'} eq 'OK';
218
219 print "";
220 return ($status,%results);
221
222 }
223
224
225 #my $testmsg = Ucam::SMS::Send->new();#test => 'true');
226 #my $token = 'vjaa-aaqb-zlni-hwvo-rhee';
227 #my ($status,%results) = $testmsg->SendMessage(number => '07757080423',
228 # token => $token,
229 # message => 'test message 1-2-3');
230 #($status,%results) = $testmsg->GetStatus(token => $token);
231 #print "SendMessage status: $status\n";
232 #foreach my $key (sort(keys(%results)))
233 #{
234 # printf "%-11s %s\n",$key,$results{$key};
235 #}
236 #sleep(1);
237 #($status,%results) = $testmsg->GetStatus(token => $token);
238 #print "GetStatus#1: $status\n";
239 #foreach my $key (sort(keys(%results)))
240 #{
241 # printf "%-11s %s\n",$key,$results{$key};
242 #}
243 #sleep(1);
244 #($status,%results) = $testmsg->GetStatus(token => $token);
245 #print "GetStatus#2: $status\n";
246 #foreach my $key (sort(keys(%results)))
247 #{
248 # printf "%-11s %s\n",$key,$results{$key};
249 #}
250 #($status,%results) = $testmsg->GetBalance(token => $token);
251 #print "Balance status:: $status\n";
252 #foreach my $key (sort(keys(%results)))
253 #{
254 # printf "%-11s %s\n",$key,$results{$key};
255 #}
256 #
257 #print "";
258
259
260 } # end of package
261
262 1;