Bug 18089 - All XSLT testing singleBranchMode = 0 fails to show even if install has...
[koha.git] / C4 / SMS.pm
blobc6031179d7398f4a81a02115f443d5a91a694dce
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;
93 my ($sent, $sender);
95 my $subpath = $driver;
96 $subpath =~ s|::|/|;
98 my $conf_file =
99 File::Spec->catfile( C4::Context->config('sms_send_config'), $subpath )
100 . q{.yaml};
101 my %args;
102 if ( -f $conf_file ) {
103 require YAML;
104 my $conf = YAML::LoadFile( $conf_file );
105 %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
108 eval {
109 # Create a sender
110 $sender = SMS::Send->new(
111 $driver,
112 _login => C4::Context->preference('SMSSendUsername'),
113 _password => C4::Context->preference('SMSSendPassword'),
114 %args,
117 # Send a message
118 $sent = $sender->send_sms(
119 to => $params->{destination},
120 text => $params->{message},
124 #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
125 #Catch those errors and fail the sms-sending gracefully.
126 if ($@) {
127 warn $@;
128 return;
130 # warn 'failure' unless $sent;
131 return $sent;
134 =head2 driver
136 =cut
138 sub driver {
139 my $self = shift;
141 # return 'US::SprintPCS';
142 return C4::Context->preference('SMSSendDriver');
148 __END__