| 1 | package SMS::Send::Tellustalk; | ||
| 2 | use HTTP::Tiny; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.01'; | ||
| 6 | use base 'SMS::Send::Driver'; | ||
| 7 | use MIME::Base64; | ||
| 8 | use JSON; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, %args) = @_; | ||
| 13 | die "$class needs hash_ref with _login and _password. (Optional _sender.)\n" unless $args{'_login'} && $args{'_password'}; | ||
| 14 | my $self = bless {%args}, $class; | ||
| 15 | my $creds = "$self->{_login}" . ":" . "$self->{_password}"; | ||
| 16 | chomp($self->{base64_cred} = "Basic " . encode_base64($creds)); | ||
| 17 | $self->{base_url} = 'https://tellus-talk.appspot.com'; | ||
| 18 | $self->{send_url} = $self->{base_url} . '/send/v1'; | ||
| 19 | $self->{_sender} = $self->{_sender} // 'FROM SENDER'; #Add the text that describes who sent the sms if not sent as _sender to new. Max 11 chars. | ||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub send_sms { | ||
| 24 | my ($self, %args) = @_; | ||
| 25 | my $json_args = { | ||
| 26 | text => "$args{'text'}", | ||
| 27 | to => "sms:$args{'to'}", | ||
| 28 | sms_originator_source => "text", | ||
| 29 | sms_originator_text => "$self->{_sender}" | ||
| 30 | }; | ||
| 31 | my $response = _post($self, to_json($json_args)); | ||
| 32 | if ($response->{status} eq "200") { | ||
| 33 | return 1; | ||
| 34 | } | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _post { | ||
| 39 | my ($self, $content) = @_; | ||
| 40 | return HTTP::Tiny->new->post( | ||
| 41 | $self->{send_url} => { | ||
| 42 | content => $content, | ||
| 43 | headers => { | ||
| 44 | "accept" => "application/json", | ||
| 45 | "content-type" => "application/json", | ||
| 46 | "authorization" => "$self->{base64_cred}" | ||
| 47 | } | ||
| 48 | } | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | |||
| 54 | __END__ |