File Coverage

blib/lib/SMS/Send/CZ/Smseagle.pm
Criterion Covered Total %
statement 49 80 61.2
branch 2 14 14.2
condition 0 4 0.0
subroutine 14 15 93.3
pod 3 3 100.0
total 68 116 58.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package SMS::Send::CZ::Smseagle;
4              
5             # ABSTRACT: SMS::Send driver for SMSEagle - Czech Republic
6              
7 2     2   108685 use warnings;
  2         5  
  2         65  
8 2     2   10 use strict;
  2         6  
  2         37  
9 2     2   9 use Carp;
  2         6  
  2         161  
10              
11             our $VERSION = "1.000";
12             $VERSION = eval $VERSION;
13              
14 2     2   1469 use LWP::UserAgent;
  2         105866  
  2         88  
15 2     2   16 use URI::Escape;
  2         6  
  2         119  
16 2     2   1947 use DateTime qw();
  2         1099372  
  2         91  
17 2     2   22 use base 'SMS::Send::Driver';
  2         13  
  2         763  
18 2     2   404 use Digest::MD5 qw(md5 md5_hex);
  2         5  
  2         132  
19 2     2   971 use Log::LogLite;
  2         26834  
  2         66  
20 2     2   1230 use Data::Dumper;
  2         14040  
  2         135  
21 2     2   1137 use Text::Unidecode;
  2         2866  
  2         113  
22 2     2   1883 use XML::Simple;
  2         18345  
  2         15  
23              
24             sub new {
25 1     1 1 77 my $class = shift;
26 1         3 my %params = @_;
27              
28 1         2 my $LOG_FILE = "/var/log/smseagle.log";
29 1         2 my $ERROR_LOG_LEVEL = 6;
30              
31 1         7931 open HANDLE, ">>$LOG_FILE";
32 1         25 close HANDLE;
33              
34             # Create our LWP::UserAgent object
35 1         14 my $ua = LWP::UserAgent->new;
36              
37             # Create the object, saving any private params for later
38             my $self = bless {
39             ua => $ua,
40             login => $params{_login},
41             password => $params{_password},
42 1 50       3353 private => \%params,
43             log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
44             }, $class;
45 1         281 $self->log("Driver Smseagle created", 4);
46            
47 1         388 $self;
48             }
49              
50             sub log {
51 1     1 1 2 my ($self, $msg, $level) = @_;
52              
53 1 50       10 if ($self->{'log'}) {
54 1         4 $self->{'log'}->write($msg, $level);
55             }
56             }
57              
58             sub send_sms {
59 0     0 1   my ($self, %args) = @_;
60 0           my $url = 'https://smsapi.karvina.cz:10443/index.php/http_api/send_sms';
61            
62 0           $args{'text'} = unidecode($args{'text'});
63 0           $self->log("TEXT: " . $args{'text'} . ", TO: " . $args{'to'}, 4);
64              
65             my %params = (
66             'to' => $args{'to'} || '',
67             'message' => $args{'text'} || '',
68             'login' => $self->{'login'},
69 0   0       'pass' => $self->{'password'},
      0        
70             'responsetype' => 'xml'
71             );
72              
73             # cleanup
74 0           $params{'to'} =~ s{\D}{}g; # remove non-digits
75 0 0         if (length($params{'to'}) == 9) {
76 0           $params{'to'} = '00420' . $params{'to'};
77 0           $self->log("Auto-prefix: " . $args{'to'} . " => " . $params{'to'}, 4);
78             }
79            
80             # send away
81 0           my $uri = join( '&', map { $_ . '=' . uri_escape_utf8( $params{ $_ } ) } keys %params );
  0            
82            
83 0           my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
84 0           $ua->protocols_allowed( ['https'] );
85              
86 0           my $res = $ua->get($url . "?" . $uri);
87              
88 0 0         if( $res->{'_rc'} == 200 ) {
89 0           $params{'password'} = "------"; # hide password in logs
90 0           my $successLog = $url . "?" . join( '&', map { $_ . '=' . uri_escape_utf8( $params{ $_ } ) } keys %params );
  0            
91 0           $self->log("HTTP SUCCESS: " . $successLog, 4);
92            
93 0           my $parser = new XML::Simple;
94 0           my $data = $parser->XMLin($res->decoded_content);
95            
96 0 0         if ($data) {
97 0           my $logMsg;
98 0           my $result = 0;
99            
100 0 0         if ($data->{'status'} eq 'ok' ) {
    0          
101 0           $logMsg = "SMS #" . $data->{'message_id'} . " sent";
102 0           $result = 1;
103             }
104             elsif ($data->{'status'} eq 'error') {
105 0           $logMsg = "SMS processing error: " . $data->{'error_text'};
106             }
107            
108 0           $self->log($logMsg, 4);
109            
110 0           return $result;
111             }
112             }
113             else {
114 0           return 0;
115             }
116             }
117              
118             __END__
119              
120             =pod
121              
122             =encoding UTF-8
123              
124             =head1 NAME
125              
126             SMS::Send::CZ::Smseagle - SMS::Send driver for SMSEagle - Czech Republic
127              
128             =head1 VERSION
129              
130             version 1.000
131              
132             =head1 SYNOPSIS
133              
134             use SMS::Send;
135              
136             my $sender = SMS::Send->new('CZ::Smseagle',
137             _login => 'who',
138             _password => 'secret',
139             );
140            
141             my $sent = $sender->send_sms(
142             text => 'Test SMS',
143             to => '604111111',
144             );
145            
146             # Did it send?
147             if ( $sent ) {
148             print "Sent test message\n";
149             } else {
150             print "Test message failed\n";
151             }
152              
153             =head1 METHODS
154              
155             =head2 log
156              
157             Logs message to /var/log/smsmidlet.log if this file is accessible and writable
158              
159             =head2 send_sms
160              
161             Sends the message using prividers API at https://smsmidlet.com/http and takes additional arguments:
162             'text' containgin the message itself and 'to' with recipient's number.
163              
164             Processing information is automatically logged to /var/log/smsmidlet.log to allow tracking of possible problems.
165              
166             Returns true if the msssage was successfully sent
167              
168             Returns false if an error occured
169              
170             =cut
171              
172             =head1 AUTHOR
173              
174             Radek Šiman <rbit@rbit.cz>
175              
176             =head1 COPYRIGHT AND LICENSE
177              
178             This software is copyright (c) 2023 by R-Bit Technology, s.r.o.
179              
180             This is free software; you can redistribute it and/or modify it under
181             the same terms as the Perl 5 programming language system itself.
182              
183             =cut