diff options
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 |