diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-10-16 12:08:49 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-10-16 12:08:49 +0100 |
commit | d9d2ca67b4feb3f550a432051606fe0df2c3680f (patch) | |
tree | 77fafbdf05e7608b98a66f9528b406b2ee0329ec /perllib/FixMyStreet/SMS.pm | |
parent | 69cd91b70b488ebba89558cbc41d2472ecbbec5a (diff) | |
parent | 8a3072bfdc11121da5a11932c248ab13f6f0d315 (diff) |
Merge branch 'sms-mssid-error'
Diffstat (limited to 'perllib/FixMyStreet/SMS.pm')
-rw-r--r-- | perllib/FixMyStreet/SMS.pm | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/perllib/FixMyStreet/SMS.pm b/perllib/FixMyStreet/SMS.pm index c71cceadc..874108706 100644 --- a/perllib/FixMyStreet/SMS.pm +++ b/perllib/FixMyStreet/SMS.pm @@ -3,7 +3,7 @@ package FixMyStreet::SMS; use strict; use warnings; -# use JSON::MaybeXS; +use JSON::MaybeXS; use Moo; use Number::Phone::Lib; use WWW::Twilio::API; @@ -28,6 +28,11 @@ has from => ( default => sub { FixMyStreet->config('TWILIO_FROM_PARAMETER') }, ); +has messaging_service => ( + is => 'lazy', + default => sub { FixMyStreet->config('TWILIO_MESSAGING_SERVICE_SID') }, +); + sub send_token { my ($class, $token_data, $token_scope, $to) = @_; @@ -44,24 +49,23 @@ sub send_token { return { random => $random, token => $token_obj->token, - result => $result, + %$result, }; } sub send { my ($self, %params) = @_; my $output = $self->twilio->POST('Messages.json', - From => $self->from, + $self->from ? (From => $self->from) : (), + $self->messaging_service ? (MessagingServiceSid => $self->messaging_service) : (), To => $params{to}, Body => $params{body}, ); - # At present, we do nothing and assume sent okay. - # TODO add error checking - # my $data = decode_json($output->{content}); - # if ($output->{code} != 200) { - # return { error => "$data->{message} ($data->{code})" }; - # } - # return { success => $data->{sid} }; + my $data = decode_json($output->{content}); + if ($output->{code} >= 400) { + return { error => "$data->{message} ($data->{code})" }; + } + return { success => $data->{sid} }; } =head2 parse_username |