| 1 | package Service::Engine::Alerting::Twilio; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Service::Engine; | ||
| 8 | use Service::Engine::Utility::Twilio::API; | ||
| 9 | |||
| 10 | our $Config; | ||
| 11 | our $Log; | ||
| 12 | our $Alert; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | |||
| 16 | my ($class,$options) = @_; | ||
| 17 | |||
| 18 | # set some defaults | ||
| 19 | my $attributes = {'method'=>''}; | ||
| 20 | |||
| 21 | # load options | ||
| 22 | if (ref($options) eq 'HASH') { | ||
| 23 | foreach my $option (keys %{$options}) { | ||
| 24 | $attributes->{$option} = $options->{$option}; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | # pull in some Service::Engine globals | ||
| 29 | $Config = $Service::Engine::Config; | ||
| 30 | $Log = $Service::Engine::Log; | ||
| 31 | |||
| 32 | my $self = bless $attributes, $class; | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | sub sendSMS { | ||
| 39 | my ($self, $args) = @_; | ||
| 40 | |||
| 41 | $Alert = $Service::Engine::Alert; | ||
| 42 | |||
| 43 | my $status = 1; | ||
| 44 | |||
| 45 | my $msg = $args->{'msg'}; | ||
| 46 | my $options = $args->{'options'}; | ||
| 47 | my $handle = $args->{'handle'}; | ||
| 48 | my $contacts = $options->{'contacts'}; | ||
| 49 | my $groups = $options->{'groups'}; | ||
| 50 | my $recipients = $Alert->get_recipients({'groups'=>$groups,'contacts'=>$contacts}); | ||
| 51 | |||
| 52 | foreach my $recipient (keys %{$recipients}) { | ||
| 53 | if ($recipients->{$recipient}->{'mobile_number'}) { | ||
| 54 | $self->_send_sms({to=>$recipients->{$recipient}->{'mobile_number'},'msg'=>$msg, 'options'=>$options, 'handle'=>$handle}); | ||
| 55 | $Log->log({'msg'=>"sending an SMS: $msg with options " . Dumper($options),'level'=>3}); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | return $status; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _send_sms { | ||
| 63 | |||
| 64 | my ($self, $args) = @_; | ||
| 65 | |||
| 66 | my $msg = $args->{'msg'}; | ||
| 67 | my $to = $args->{'to'}; | ||
| 68 | my $handle = $args->{'handle'}; | ||
| 69 | |||
| 70 | if (!$msg || !$to) { | ||
| 71 | $Log->log({'msg'=>"Missing required options to send SMS " . Dumper($args),'level'=>2}); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | my $handle_config = $Config->get_config('alerting')->{'modules'}->{'Twilio'}->{$handle}; | ||
| 76 | if (ref($handle_config) ne 'HASH') { | ||
| 77 | $Log->log({'msg'=>"Missing $handle config " . Dumper($handle_config),'level'=>2}); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $AccountSid = $handle_config->{'AccountSid'}; | ||
| 82 | my $AuthToken = $handle_config->{'AuthToken'}; | ||
| 83 | my $from = $handle_config->{'from'}; | ||
| 84 | |||
| 85 | if (!$AccountSid || !$AuthToken || !$from) { | ||
| 86 | $Log->log({'msg'=>"Missing required account options to send SMS " . Dumper($handle_config),'level'=>2}); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | my $twilio = Service::Engine::Utility::Twilio::API->new('AccountSid' => $AccountSid, | ||
| 91 | 'AuthToken' => $AuthToken); | ||
| 92 | |||
| 93 | my $response = $twilio->POST( 'Messages', | ||
| 94 | From => $from, | ||
| 95 | To => $to, | ||
| 96 | Body => $msg); | ||
| 97 | |||
| 98 | $Log->log({'msg'=>$response->{content},'level'=>3}); | ||
| 99 | |||
| 100 | return; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | 1; |