File Coverage

blib/lib/SMS/Send/CZ/Smsmanager.pm
Criterion Covered Total %
statement 54 61 88.5
branch 3 8 37.5
condition 2 4 50.0
subroutine 13 13 100.0
pod 2 3 66.6
total 74 89 83.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package SMS::Send::CZ::Smsmanager;
4              
5             # ABSTRACT: SMS::Send driver for SMS Manager - Czech Republic
6              
7 2     2   479467 use warnings;
  2         4  
  2         145  
8 2     2   12 use strict;
  2         8  
  2         45  
9 2     2   7 use Carp;
  2         3  
  2         195  
10              
11             our $VERSION = "1.001";
12             $VERSION = eval $VERSION;
13              
14 2     2   1689 use LWP::UserAgent;
  2         123585  
  2         104  
15 2     2   22 use URI::Escape;
  2         4  
  2         162  
16 2     2   3639 use DateTime qw();
  2         1286585  
  2         101  
17 2     2   20 use base qw(SMS::Send::Driver);
  2         4  
  2         923  
18 2     2   1507 use Log::LogLite;
  2         28224  
  2         96  
19 2     2   1178 use Text::Unidecode;
  2         3121  
  2         138  
20 2     2   1314 use Data::Dumper;
  2         22340  
  2         1294  
21              
22             sub new {
23 1     1 1 71 my $class = shift;
24 1         4 my %params = @_;
25              
26 1         3 my $LOG_FILE = "/var/log/smsmanager.log";
27 1         2 my $ERROR_LOG_LEVEL = 6;
28              
29 1         11312 open HANDLE, ">>$LOG_FILE";
30 1         18 close HANDLE;
31              
32             # Create our LWP::UserAgent object
33 1         25 my $ua = LWP::UserAgent->new;
34              
35             # Create the object, saving any private params for later
36 1         2843 my $dt = DateTime->now(time_zone => 'Europe/Prague');
37             my $self = bless {
38             ua => $ua,
39             apikey => $params{_password},
40 1 50       17367 private => \%params,
41             stamp => $dt->strftime('%Y%m%dT%H%M%S'),
42             log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
43             }, $class;
44              
45 1         492 $self->log("Driver Smsmanager created", 4);
46            
47 1         363 $self;
48             }
49              
50             sub log {
51 2     2 0 26 my ($self, $msg, $level) = @_;
52              
53 2 50       20 if ($self->{'log'}) {
54 2         13 $self->{'log'}->write($msg, $level);
55             }
56             }
57              
58             sub send_sms {
59 1     1 1 1160 my ($self, %args) = @_;
60 1         2 my $url = 'https://http-api.smsmanager.cz/Send';
61              
62 1         7 $args{'text'} = unidecode($args{'text'});
63             my $params = {
64             'number' => $args{'to'} || '',
65             'message' => $args{'text'} || '',
66             'apikey' => $self->{apikey}
67 1   50     28 };
      50        
68              
69             # cleanup
70 1         4 $params->{'number'} =~ s{\D}{}g; # remove non-digits
71            
72             # send away
73 1         14 my $ua = LWP::UserAgent->new();
74 1         289 my $res = $ua->post($url, $params );
75              
76 1 50       458530 if( $res->is_success ) {
77 0         0 $self->log("HTTP SUCCESS: " . $args{'to'} . " - " . $args{'text'}, 4);
78 0         0 my $data = $res->decoded_content;
79 0 0       0 if ( substr($data, 0, 2) eq 'OK' ) {
80 0         0 $self->log("SMS sent: " . $data, 4);
81              
82 0         0 return 1;
83             }
84             else {
85 0         0 $self->log("SMS processing error: " . $data, 4);
86 0         0 return 0;
87             }
88             }
89             else {
90 1         14 $self->log("HTTP error #" . $res->code() . ": " . $res->message(), 4);
91 1         451 return 0;
92             }
93             }
94              
95             __END__
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =head1 NAME
102              
103             SMS::Send::CZ::Smsmanager - SMS::Send driver for SMS Manager - Czech Republic
104              
105             =head1 VERSION
106              
107             version 1.001
108              
109             =head1 SYNOPSIS
110              
111             use SMS::Send;
112              
113             my $sender = SMS::Send->new('CZ::Smsmanager',
114             _login => 'who',
115             _password => 'apikey',
116             );
117            
118             my $sent = $sender->send_sms(
119             text => 'Test SMS',
120             to => '604111111',
121             );
122            
123             # Did it send?
124             if ( $sent ) {
125             print "Sent test message\n";
126             } else {
127             print "Test message failed\n";
128             }
129              
130             =head1 NAME
131              
132             SMS::Send::CZ::Smsmanager - SMS::Send driver for SMS Manager - Czech Republic
133              
134             =head1 VERSION
135              
136             version 1.000
137              
138             =head1 METHODS
139              
140             =head2 send_sms
141              
142             Sends the message using privider's API at https://http-api.smsmanager.cz/Send and takes additional arguments:
143             'text' containing the message itself and 'to' with recipient's number.
144              
145             Processing information is automatically logged to /var/log/smsmanager.log to allow tracking of possible problems.
146              
147             Returns true if the msssage was successfully sent
148              
149             Returns false if an error occured
150              
151             =head1 AUTHOR
152              
153             Radek Šiman <rbit@rbit.cz>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2024 by R-Bit Technology, s.r.o.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 AUTHOR
163              
164             Radek Šiman <rbit@rbit.cz>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2024 by R-Bit Technology, s.r.o.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut