| 1 | package SMS::Send::UK::GovUkNotify; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | SMS::Send::UK::GovUkNotify | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | Version 0.0.1 | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | our $VERSION = '0.0.1'; | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | # Instantiate a sender object | ||
| 22 | my $sender = SMS::Send->new( | ||
| 23 | 'UK::GovUkNotify', | ||
| 24 | _template_id => 'template_id', | ||
| 25 | _key => 'key' | ||
| 26 | ); | ||
| 27 | |||
| 28 | # Send a message | ||
| 29 | my $sent = $sender->send_sms( | ||
| 30 | to => '0000000000' # In the format: +447312345679, | ||
| 31 | text => 'Dummy text' | ||
| 32 | ); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | L<SMS::Send::UK::GovUkNotify> is a L<SMS::Send> driver that provides | ||
| 37 | SMS message sending via the Gov.Uk Notify service. | ||
| 38 | |||
| 39 | Usage of this module requires the definition of a template with a single | ||
| 40 | placeholder ((text)) - the content of the text message sent will be | ||
| 41 | substituted in place of the placeholder | ||
| 42 | |||
| 43 | =head2 CAVEATS | ||
| 44 | |||
| 45 | You need a Gov.Uk Notify account in order to use this driver | ||
| 46 | |||
| 47 | =head1 METHODS | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | use base 'SMS::Send::Driver'; | ||
| 52 | use LWP::UserAgent(); | ||
| 53 | use JSON qw(encode_json); | ||
| 54 | use URI::Escape qw(uri_escape); | ||
| 55 | use Scalar::Util qw( looks_like_number ); | ||
| 56 | use Crypt::JWT qw(encode_jwt); | ||
| 57 | |||
| 58 | =head2 new | ||
| 59 | |||
| 60 | # Instantiate a sender object | ||
| 61 | my $sender = SMS::Send->new( | ||
| 62 | 'UK::GovUkNotify', | ||
| 63 | _key => 'key' | ||
| 64 | ); | ||
| 65 | |||
| 66 | The C<new> constructor accepts one parameter, which is required | ||
| 67 | |||
| 68 | The return value is a C<SMS::Send::UK::GovUkNotify> object. | ||
| 69 | |||
| 70 | =over 4 | ||
| 71 | |||
| 72 | =item 'UK::GovUkNotify' | ||
| 73 | |||
| 74 | The parameter identifying the driver name | ||
| 75 | |||
| 76 | =item _key | ||
| 77 | |||
| 78 | The C<_key> parameter is the full API key, as supplied by Gov.Uk Notify | ||
| 79 | https://docs.notifications.service.gov.uk/rest-api.html#api-keys | ||
| 80 | |||
| 81 | =item _template_id | ||
| 82 | |||
| 83 | The C<_template_id> parameter as supplied by Gov.Uk Notify | ||
| 84 | https://docs.notifications.service.gov.uk/rest-api.html#template-id-required | ||
| 85 | |||
| 86 | =back | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub new { | ||
| 91 | my $class = shift; | ||
| 92 | my %params = @_; | ||
| 93 | |||
| 94 | # Ensure we've been passed the parameters we're expecting | ||
| 95 | my @expected = ( '_key', '_template_id' ); | ||
| 96 | foreach my $expect(@expected) { | ||
| 97 | if (!exists $params{$expect}) { | ||
| 98 | croak join(', ', @expected) . ' parameters must be supplied'; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # Extract the components we need to create the JWT | ||
| 103 | my ($iss, $key) = $params{_key} =~ /^.+?-(.{36})-(.+)$/; | ||
| 104 | my $iat = time(); | ||
| 105 | my $payload = { | ||
| 106 | iss => $iss, | ||
| 107 | iat => $iat | ||
| 108 | }; | ||
| 109 | |||
| 110 | # Encode the JWT | ||
| 111 | my $token = encode_jwt(payload => $payload, alg => 'HS256', key => $key); | ||
| 112 | |||
| 113 | # Instantiate the object | ||
| 114 | my $self = bless { | ||
| 115 | token => $token, | ||
| 116 | template_id => $params{_template_id}, | ||
| 117 | base_url => 'https://api.notifications.service.gov.uk' | ||
| 118 | }, $class; | ||
| 119 | |||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 clean_to | ||
| 124 | |||
| 125 | # Examine a passed phone number and attempt to return the number | ||
| 126 | # in a format supported by the API, | ||
| 127 | # croak if a conversion is not possible | ||
| 128 | my $intl_number = clean_to($source_number); | ||
| 129 | |||
| 130 | The C<clean_to> method accepts a single required parameter: | ||
| 131 | |||
| 132 | The return value is the number in international format | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item source_number | ||
| 137 | |||
| 138 | A string containing the number to be cleaned | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub clean_number { | ||
| 145 | my $source_number = shift; | ||
| 146 | |||
| 147 | # The number may already be in the format we want | ||
| 148 | if ($source_number =~ /^\+44/) { | ||
| 149 | return $source_number; | ||
| 150 | } | ||
| 151 | # Replace a leading 44 with +44 | ||
| 152 | elsif ($source_number =~ /^44/) { | ||
| 153 | $source_number =~ s/^44/+44/; | ||
| 154 | return $source_number; | ||
| 155 | } | ||
| 156 | # Replace a leading 0 with +44 | ||
| 157 | elsif ($source_number =~ /^0/) { | ||
| 158 | $source_number =~ s/^0/+44/; | ||
| 159 | return $source_number; | ||
| 160 | } | ||
| 161 | # We can't do anything with this number | ||
| 162 | else { | ||
| 163 | croak 'Unrecognised number format: ' . $source_number; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 send_sms | ||
| 168 | |||
| 169 | # Send a message | ||
| 170 | my $sent = $sender->send_sms( | ||
| 171 | text => 'dummy_text' | ||
| 172 | to => '0000000000' # In the format: +447312345679 | ||
| 173 | ); | ||
| 174 | |||
| 175 | The C<send_sms> method accepts two parameters, both of which are required. | ||
| 176 | |||
| 177 | The return value is a 0 or 1 representing false or true, indicating whether | ||
| 178 | the send was successful. | ||
| 179 | |||
| 180 | =over 4 | ||
| 181 | |||
| 182 | =item text | ||
| 183 | |||
| 184 | Although this is a required parameter by Send::SMS, we don't actually use it. | ||
| 185 | The Gov.Uk Notify service uses a template ID (which is passed in our | ||
| 186 | constructor) and placeholder values. So we just silently ignore what was | ||
| 187 | passed in 'text' | ||
| 188 | |||
| 189 | =item to | ||
| 190 | |||
| 191 | A numeric string representing the phone number of the recipient, in a valid | ||
| 192 | international format (i.e. country code followed by the number) | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub send_sms { | ||
| 199 | my $self = shift; | ||
| 200 | my %params = @_; | ||
| 201 | |||
| 202 | # Ensure we've been passed the parameters we're expecting | ||
| 203 | my @expected = ('to', 'text'); | ||
| 204 | foreach my $expect(@expected) { | ||
| 205 | if (!exists $params{$expect}) { | ||
| 206 | croak join(', ', @expected) . ' parameters must be supplied'; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | my $ua = LWP::UserAgent->new; | ||
| 211 | my $to = clean_number($params{to}); | ||
| 212 | |||
| 213 | # Send the request | ||
| 214 | my $body = encode_json({ | ||
| 215 | phone_number => $to, | ||
| 216 | template_id => $self->{template_id}, | ||
| 217 | personalisation => { | ||
| 218 | text => $params{text} | ||
| 219 | } | ||
| 220 | }); | ||
| 221 | |||
| 222 | my $response = $ua->post( | ||
| 223 | $self->{'base_url'} . '/v2/notifications/sms', | ||
| 224 | 'Content-Type' => 'application/json', | ||
| 225 | Authorization => 'Bearer ' . $self->{'token'}, | ||
| 226 | Content => $body | ||
| 227 | ); | ||
| 228 | |||
| 229 | # Check the send succeded | ||
| 230 | if (!$response->is_success()) { | ||
| 231 | croak('API request failed: ' . $response->status_line()); | ||
| 232 | return 0; | ||
| 233 | } | ||
| 234 | |||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Andrew Isherwood C<< <andrew.isherwood at ptfs-europe.com> >> | ||
| 241 | |||
| 242 | =head1 BUGS | ||
| 243 | |||
| 244 | Please report any bugs or features to C<andrew.isherwood at ptfs-europe.com> | ||
| 245 | |||
| 246 | =head1 SUPPORT | ||
| 247 | |||
| 248 | You can find documentation for this module with the perldoc command. | ||
| 249 | |||
| 250 | perldoc SMS::Send::UK::GovUkNotify | ||
| 251 | |||
| 252 | =head1 COPYRIGHT & LICENSE | ||
| 253 | |||
| 254 | Copyright (C) 2020 PTFS Europe L<https://www.ptfs-europe.com/> | ||
| 255 | |||
| 256 | This library is free software; you can redistribute it and/or modify | ||
| 257 | it under the same terms as Perl itself | ||
| 258 | |||
| 259 | Additionally, you are again reminded that this software comes with | ||
| 260 | no warranty of any kind, including but not limited to the implied | ||
| 261 | warranty of merchantability. | ||
| 262 | |||
| 263 | ANY use may result in charges on your Gov.Uk Notify bill, and you should | ||
| 264 | use this software with care. The author takes no responsibility for | ||
| 265 | any such charges accrued. | ||
| 266 | |||
| 267 | =head1 ACKNOWLEDGEMENTS | ||
| 268 | |||
| 269 | Many thanks to the authors of the following modules that served as | ||
| 270 | inspiration for this one: | ||
| 271 | |||
| 272 | =over 4 | ||
| 273 | |||
| 274 | =item SMS::Send::US::TMobile | ||
| 275 | |||
| 276 | |||
| 277 | =item SMS::Send::US::Ipipi | ||
| 278 | |||
| 279 | |||
| 280 | =item SMS::Send::UK::Kapow | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | 1; # End of SMS::Send::UK::GovUkNotify | ||
| 287 |