Bug 18282: operationId must be unique
[koha.git] / C4 / SMS.pm
1 package C4::SMS;
2
3 # Copyright 2007 Liblime
4 # Copyright 2015 Biblibre
5 # Copyright 2016 Catalyst
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 =head1 NAME
23
24 C4::SMS - send SMS messages
25
26 =head1 SYNOPSIS
27
28 my $success = C4::SMS->send_sms({ message     => 'This is my text message',
29                                   destination => '212-555-1212' });
30
31 =head1 DESCRIPTION
32
33 A wrapper for SMS::Send.
34
35 Can use a yaml file for config, the path to which is in the koha-conf.xml
36 <sms_send_config>__KOHA_CONF_DIR__/sms_send/</sms_send_config>
37
38 Each file needs to be in the format of
39 __KOHA_CONF_DIR__/sms_send/<driver>.yaml
40
41 For example for SMS::Send::UK::Kapow the config would be
42
43 /etc/koha/sites/instancename/sms_send/UK/Kapow.yaml for package install
44 or
45 /etc/koha/sms_send/UK/Kapow.yaml for tarball
46
47 A underscore character is prepended to all parameter names so they are
48 treated as driver-specific options (leading underscore must not appear
49 in config file).
50
51 =cut
52
53 use strict;
54 use warnings;
55
56 use C4::Context;
57 use File::Spec;
58
59
60
61 =head1 METHODS
62
63 =cut
64
65 # The previous implmentation used username and password.
66 # our $user = C4::Context->config('smsuser');
67 # our $pwd  = C4::Context->config('smspass');
68
69 =head2 send_sms
70
71 =cut
72
73 sub send_sms {
74     my $self = shift;
75     my $params= shift;
76
77     foreach my $required_parameter ( qw( message destination ) ) {
78         # Should I warn in some way?
79         return unless defined $params->{ $required_parameter };
80     }
81
82     eval { require SMS::Send; };
83     if ( $@ ) {
84         # we apparently don't have SMS::Send. Return a failure.
85         return;
86     }
87
88     # This allows the user to override the driver. See SMS::Send::Test
89     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
90     return unless $driver;
91
92     my ($sent, $sender);
93
94     my $subpath = $driver;
95     $subpath =~ s|::|/|g;
96
97     my $sms_send_config = C4::Context->config('sms_send_config');
98     my $conf_file = defined $sms_send_config
99         ? File::Spec->catfile( $sms_send_config, $subpath )
100         : $subpath;
101     $conf_file .= q{.yaml};
102
103     my %args;
104     if ( -f $conf_file ) {
105         require YAML;
106         my $conf = YAML::LoadFile( $conf_file );
107         %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
108     }
109
110     eval {
111         # Create a sender
112         $sender = SMS::Send->new(
113             $driver,
114             _login    => C4::Context->preference('SMSSendUsername'),
115             _password => C4::Context->preference('SMSSendPassword'),
116             %args,
117         );
118
119         # Send a message
120         $sent = $sender->send_sms(
121             to   => $params->{destination},
122             text => $params->{message},
123         );
124     };
125
126     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
127     #Catch those errors and fail the sms-sending gracefully.
128     if ($@) {
129         warn $@;
130         return;
131     }
132     # warn 'failure' unless $sent;
133     return $sent;
134 }
135
136 =head2 driver
137
138 =cut
139
140 sub driver {
141     my $self = shift;
142
143     # return 'US::SprintPCS';
144     return C4::Context->preference('SMSSendDriver');
145
146 }
147
148 1;
149
150 __END__
151