Skip to content

Commit

Permalink
Twilio: support voice pages
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Mar 10, 2023
1 parent bbbcaf1 commit 9b45da8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
50 changes: 41 additions & 9 deletions lib/Synergy/Channel/Twilio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package Synergy::Channel::Twilio;

use Moose;
use experimental qw(signatures);
use HTML::Entities ();
use JSON::MaybeXS qw(encode_json decode_json);

use Synergy::Logger '$Logger';
Expand Down Expand Up @@ -127,26 +128,57 @@ sub send_message_to_user ($self, $user, $text, $alts = {}) {
$self->send_message($phone, $text, $alts);
}

my %LANGUAGE_FOR = (
61 => 'en-AU',
);

sub send_message ($self, $target, $text, $alts = {}) {
my $from = $self->from;

my $picked_code;

for my $code (sort { length $b <=> length $a } keys $self->numbers->%*) {
if ($target =~ /\A\+?\Q$code/) {
$from = $self->numbers->{$code};
$picked_code = $code;
last;
}
}

my $sid = $self->sid;
my $res_f = $self->http_post(
"https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json",
Content => [
From => $from,
To => $target,
Body => $text,
],
Authorization => "Basic " . $self->auth,
);
my $res_f;

if ($alts->{voice}) {
my $encoded = HTML::Entities::encode_entities($alts->{voice});

my $language = HTML::Entities::encode_entities(
$LANGUAGE_FOR{ $picked_code // 1 } // 'en-US'
);

$res_f = $self->http_post(
"https://api.twilio.com/2010-04-01/Accounts/$sid/Calls.json",
Content => [
From => $from,
To => $target,
Twiml => <<~"END"
<Response>
<Say language="$language" loop="3" voice="woman">$encoded</Say>
</Response>
END
],
Authorization => "Basic " . $self->auth,
);
} else {
$res_f = $self->http_post(
"https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json",
Content => [
From => $from,
To => $target,
Body => $text,
],
Authorization => "Basic " . $self->auth,
);
}

return $res_f->then(sub ($res) {
unless ($res->is_success) {
Expand Down
21 changes: 19 additions & 2 deletions lib/Synergy/Reactor/Page.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use warnings;
package Synergy::Reactor::Page;

use Moose;
with 'Synergy::Role::Reactor::CommandPost';
with 'Synergy::Role::Reactor::CommandPost',
'Synergy::Role::HasPreferences';

use utf8;

Expand Down Expand Up @@ -61,7 +62,15 @@ END
my $from = $event->from_user ? $event->from_user->username
: $event->from_address;

$to_channel->send_message_to_user($user, "$from says: $what");
my $want_voice = $self->get_user_preference($user, 'voice-page');

$to_channel->send_message_to_user(
$user,
"$from says: $what",
($want_voice
? { voice => "Hi, this is Synergy. You are being paged by $from, who says: $what" }
: ()),
);

$paged = 1;
}
Expand All @@ -86,4 +95,12 @@ END
}
};

__PACKAGE__->add_preference(
name => 'voice-page',
validator => sub ($self, $value, @) { return bool_from_text($value) },
default => 1,
help => "Should paging try make a voice call instead of a text message?",
description => "Should paging try make a voice call instead of a text message?",
);

1;

0 comments on commit 9b45da8

Please sign in to comment.