| 1 | package SMS::Send::Plivo; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use 5.008_005; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use JSON::PP; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use WWW::Plivo::API; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | use parent qw(SMS::Send::Driver); | ||
| 13 | |||
| 14 | our $VERSION = '0.01_01'; | ||
| 15 | $VERSION = eval $VERSION; | ||
| 16 | |||
| 17 | =encoding utf-8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | SMS::Send::Plivo - SMS::Send backend for Plivo API | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | (This is a Plivo port of the existing SMS::Send::Twilio module. See | ||
| 26 | that module for more detailed documentation.) | ||
| 27 | |||
| 28 | use SMS::Send; | ||
| 29 | # Create an object. There are three required values: | ||
| 30 | my $sender = SMS::Send->new('Plivo', | ||
| 31 | _accounauthid => 'ACb657bdcb16f06893fd127e099c070eca', | ||
| 32 | _authtoken => 'b857f7afe254fa86c689648447e04cff', | ||
| 33 | _from => '+15005550006', | ||
| 34 | ); | ||
| 35 | |||
| 36 | # Send a message to me | ||
| 37 | my $sent = $sender->send_sms( | ||
| 38 | text => 'Messages can be up to 1600 characters', | ||
| 39 | to => '+31645742418', | ||
| 40 | ); | ||
| 41 | |||
| 42 | # Did it send? | ||
| 43 | if ( $sent ) { | ||
| 44 | print "Sent test message\n"; | ||
| 45 | } else { | ||
| 46 | print "Test message failed\n"; | ||
| 47 | } | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | SMS::Send::Plivo is an SMS::Send driver for the Plivo web service. | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =head2 new | ||
| 56 | |||
| 57 | # Create a new sender using this driver | ||
| 58 | my $sender = SMS::Send->new('Plivo', | ||
| 59 | _accountauthid => 'ACb657bdcb16f06893fd127e099c070eca', | ||
| 60 | _authtoken => 'b857f7afe254fa86c689648447e04cff', | ||
| 61 | _from => '+15005550006', | ||
| 62 | ); | ||
| 63 | |||
| 64 | The C<new> constructor takes three parameters, which should be passed | ||
| 65 | through from the L<SMS::Send> constructor. | ||
| 66 | |||
| 67 | =head2 send_sms | ||
| 68 | |||
| 69 | It's really easy; if it returns a true value, sending the message was OK. | ||
| 70 | If not we'd see an error message on STDERR. | ||
| 71 | |||
| 72 | # Send a message to me | ||
| 73 | my $sent = $sender->send_sms( | ||
| 74 | text => 'Messages can be up to 1600 characters', | ||
| 75 | to => '+31645742418', | ||
| 76 | ); | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub new { | ||
| 81 | my $class = shift; | ||
| 82 | my %params = @_; | ||
| 83 | |||
| 84 | # check required parameters | ||
| 85 | for my $param (qw ( _accountauthid _from _authtoken )) { | ||
| 86 | exists $params{$param} | ||
| 87 | or croak $class . "->new requires $param parameter"; | ||
| 88 | } | ||
| 89 | |||
| 90 | my $self = \%params; | ||
| 91 | bless $self, $class; | ||
| 92 | |||
| 93 | # Create plivo object | ||
| 94 | $self->{plivo} = WWW::Plivo::API->new( | ||
| 95 | AccountAuthid => $self->{_accountauthid}, | ||
| 96 | AuthToken => $self->{_authtoken}, | ||
| 97 | ) or croak $class . "->new can't set up connection: $!"; | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub send_sms { | ||
| 103 | my $self = shift; | ||
| 104 | my %params = @_; | ||
| 105 | |||
| 106 | # Get the message and destination | ||
| 107 | my $message = delete $params{text}; | ||
| 108 | my $recipient = delete $params{to}; | ||
| 109 | |||
| 110 | my $response = $self->{plivo}->POST( | ||
| 111 | 'Message', | ||
| 112 | From => $self->{_from}, | ||
| 113 | To => $recipient, | ||
| 114 | Body => $message, | ||
| 115 | ); | ||
| 116 | |||
| 117 | if ( ($response->{code} >= 200) && ($response->{code} <= 204) ) { | ||
| 118 | my $result = JSON::PP->new->utf8->decode( $response->{content} ); | ||
| 119 | |||
| 120 | if ( $result->{message_uuid} ) { | ||
| 121 | return $result->{message_uuid}; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | elsif ( $response->{code} == '400' ) { | ||
| 125 | my $result = JSON::PP->new->utf8->decode( $response->{content} ); | ||
| 126 | if ( $result->{message} ) { | ||
| 127 | croak "$result->{message}"; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | croak "Can't send SMS: $response->{code} $response->{message}"; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head1 AUTHOR | ||
| 135 | |||
| 136 | Mike Lempriere, C<< <mikevntnr at cpan.org> >> | ||
| 137 | |||
| 138 | =head1 BUGS | ||
| 139 | |||
| 140 | Please report any bugs or feature requests to C<bug-sms-send-plivo at rt.cpan.org>, or through | ||
| 141 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=SMS-Send-Plivo>. I will be notified, and then you'll | ||
| 142 | automatically be notified of progress on your bug as I make changes. | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | =head1 SUPPORT | ||
| 148 | |||
| 149 | You can find documentation for this module with the perldoc command. | ||
| 150 | |||
| 151 | perldoc SMS::Send::Plivo | ||
| 152 | |||
| 153 | |||
| 154 | You can also look for information at: | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 159 | |||
| 160 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=SMS-Send-Plivo> | ||
| 161 | |||
| 162 | =item * CPAN Ratings | ||
| 163 | |||
| 164 | L<https://cpanratings.perl.org/d/SMS-Send-Plivo> | ||
| 165 | |||
| 166 | =item * Search CPAN | ||
| 167 | |||
| 168 | L<https://metacpan.org/release/SMS-Send-Plivo> | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | |||
| 173 | =head1 ACKNOWLEDGEMENTS | ||
| 174 | |||
| 175 | |||
| 176 | =head1 LICENSE AND COPYRIGHT | ||
| 177 | |||
| 178 | This software is Copyright (c) 2023 by Mike Lempriere. | ||
| 179 | |||
| 180 | This is free software, licensed under: | ||
| 181 | |||
| 182 | The Artistic License 2.0 (GPL Compatible) | ||
| 183 | |||
| 184 | |||
| 185 | =head1 SEE ALSO | ||
| 186 | |||
| 187 | L<SMS::Send> | ||
| 188 | L<WWW::Plivo::API> | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | 1; # End of SMS::Send::Plivo | ||