File Coverage

blib/lib/SMS/Send/CZ/Konzulta.pm
Criterion Covered Total %
statement 46 57 80.7
branch 3 10 30.0
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 63 81 77.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package SMS::Send::CZ::Konzulta;
4              
5             # ABSTRACT: SMS::Send driver for Konzulta - Czech Republic
6              
7 2     2   74241 use warnings;
  2         3  
  2         81  
8 2     2   10 use strict;
  2         3  
  2         44  
9 2     2   9 use Carp;
  2         7  
  2         187  
10              
11             our $VERSION = "1.000";
12             $VERSION = eval $VERSION;
13              
14 2     2   10 use base 'SMS::Send::Driver';
  2         3  
  2         799  
15 2     2   1428 use Log::LogLite;
  2         24423  
  2         141  
16 2     2   1560 use XML::Simple;
  2         17116  
  2         16  
17 2     2   1773 use LWP::UserAgent;
  2         87938  
  2         89  
18 2     2   2179 use DateTime qw();
  2         811450  
  2         830  
19              
20             sub new {
21 1     1 1 56 my $class = shift;
22 1         3 my %params = @_;
23              
24 1         1 my $LOG_FILE = "/var/log/konzulta.log";
25 1         1 my $ERROR_LOG_LEVEL = 6;
26              
27 1         196 open HANDLE, ">>$LOG_FILE";
28 1         7 close HANDLE;
29              
30 1         7 my $dt = DateTime->now(time_zone => 'Europe/Prague');
31             my $self = bless {
32             login => $params{_login},
33             password => $params{_password},
34 1 50       10653 stamp => $dt->strftime('%Y%m%dT%H%M%S'),
35             log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
36             }, $class;
37              
38 1         364 $self->log("Driver Konzulta created", 4);
39            
40 1         268 $self;
41             }
42              
43             sub log {
44 3     3 1 18 my ($self, $msg, $level) = @_;
45              
46 3 50       21 if ($self->{'log'}) {
47 3         12 $self->{'log'}->write($msg, $level);
48             }
49             }
50              
51             sub send_sms {
52 1     1 1 1186 my ($self, %args) = @_;
53 1         2 my $url = 'https://www.sms-operator.cz/webservices/webservice.aspx';
54            
55 1         7 $self->log("TEXT: " . $args{'text'} . ", TO: " . $args{'to'}, 4);
56            
57 1         158 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
58 1         2171 my $message = "<SmsServices>
59             <DataHeader>
60             <DataType>SMS</DataType>
61             <UserName>$self->{'login'}</UserName>
62             <Password>$self->{'password'}</Password>
63             </DataHeader>
64             <DataArray>
65             <DataTemplate>
66             <Text>$args{'text'}</Text>
67             <DataItem>
68             <MobileTerminate>$args{'to'}</MobileTerminate>
69             <SmsId>$self->{'stamp'}</SmsId>
70             </DataItem>
71             </DataTemplate>
72             </DataArray>
73             </SmsServices>
74             ";
75            
76 1         5 my $res = $ua->post($url, Content_Type => 'text/xml', Content => $message);
77              
78 1 50       9708 if( $res->is_success ) {
79 0         0 $self->log("HTTP SUCCESS", 4);
80 0 0       0 if ( $res->decoded_content ) {
81 0         0 my $parser = new XML::Simple;
82 0         0 my $data = $parser->XMLin($res->decoded_content);
83 0 0       0 if ($data->{'DataArray'}->{'DataItem'}->{'Status'} == 0) {
84 0         0 $self->log("SMS #" . $data->{'DataArray'}->{'DataItem'}->{'SmsId'} . " sent", 4);
85            
86 0         0 return 1;
87             }
88             else {
89 0         0 $self->log("SMS processing error: " . $data->{'DataArray'}->{'DataItem'}->{'Status'}, 4);
90 0         0 return 0;
91             }
92             }
93             else {
94 0         0 $self->log("SMS processing error: invalid credentials?", 4);
95 0         0 return 0;
96             }
97             }
98             else {
99 1         13 $self->log("HTTP error #" . $res->code() . ": " . $res->message(), 4);
100 1         278 return 0;
101             }
102             }
103              
104             __END__
105              
106             =pod
107              
108             =encoding UTF-8
109              
110             =head1 NAME
111              
112             SMS::Send::CZ::Konzulta - SMS::Send driver for Konzulta - Czech Republic
113              
114             =head1 VERSION
115              
116             version 1.000
117              
118             =head1 SYNOPSIS
119              
120             use SMS::Send;
121              
122             my $sender = SMS::Send->new('CZ::Konzulta',
123             _login => 'who',
124             _password => 'secret',
125             );
126            
127             my $sent = $sender->send_sms(
128             text => 'Test SMS',
129             to => '604111111',
130             );
131            
132             # Did it send?
133             if ( $sent ) {
134             print "Sent test message\n";
135             } else {
136             print "Test message failed\n";
137             }
138              
139             =head1 METHODS
140              
141             =head2 log
142              
143             Logs message to /var/log/konzulta.log if this file is accessible and writable
144              
145             =head2 send_sms
146              
147             Sends the message using provider's API at https://www.sms-operator.cz/webservices/webservice.aspx and takes additional arguments:
148             'text' containgin the message itself and 'to' with recipient's number.
149              
150             Processing information is automatically logged to /var/log/konzulta.log to allow tracking of possible problems.
151              
152             Returns true if the msssage was successfully sent
153              
154             Returns false if an error occured
155              
156             =cut
157              
158             =head1 AUTHOR
159              
160             Radek Å iman <rbit@rbit.cz>
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is copyright (c) 2017 by R-Bit Technology, s.r.o.
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =cut