Bug 24412: (follow-up) prevent request.pl from failing
[koha.git] / C4 / SMS.pm
blobeff72b358c0a5e2af6c09c88c002f2e5fb7fe047
1 package C4::SMS;
3 # Copyright 2007 Liblime
4 # Copyright 2015 Biblibre
5 # Copyright 2016 Catalyst
7 # This file is part of Koha.
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.
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.
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>.
22 =head1 NAME
24 C4::SMS - send SMS messages
26 =head1 SYNOPSIS
28 my $success = C4::SMS->send_sms({ message => 'This is my text message',
29 destination => '212-555-1212' });
31 =head1 DESCRIPTION
33 A wrapper for SMS::Send.
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>
38 Each file needs to be in the format of
39 __KOHA_CONF_DIR__/sms_send/<driver>.yaml
41 For example for SMS::Send::UK::Kapow the config would be
43 /etc/koha/sites/instancename/sms_send/UK/Kapow.yaml for package install
45 /etc/koha/sms_send/UK/Kapow.yaml for tarball
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).
51 =cut
53 use strict;
54 use warnings;
56 use C4::Context;
57 use File::Spec;
61 =head1 METHODS
63 =cut
65 # The previous implmentation used username and password.
66 # our $user = C4::Context->config('smsuser');
67 # our $pwd = C4::Context->config('smspass');
69 =head2 send_sms
71 =cut
73 sub send_sms {
74 my $self = shift;
75 my $params= shift;
77 foreach my $required_parameter ( qw( message destination ) ) {
78 # Should I warn in some way?
79 return unless defined $params->{ $required_parameter };
82 eval { require SMS::Send; };
83 if ( $@ ) {
84 # we apparently don't have SMS::Send. Return a failure.
85 return;
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;
92 my ($sent, $sender);
94 my $subpath = $driver;
95 $subpath =~ s|::|/|g;
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};
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;
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,
119 # Send a message
120 $sent = $sender->send_sms(
121 to => $params->{destination},
122 text => $params->{message},
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;
132 # warn 'failure' unless $sent;
133 return $sent;
136 =head2 driver
138 =cut
140 sub driver {
141 my $self = shift;
143 # return 'US::SprintPCS';
144 return C4::Context->preference('SMSSendDriver');
150 __END__