File Coverage

blib/lib/SMS/Send/Smstrade.pm
Criterion Covered Total %
statement 15 62 24.1
branch 0 24 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 3 3 100.0
total 23 102 22.5


line stmt bran cond sub pod time code
1             package SMS::Send::Smstrade;
2              
3 1     1   28980 use warnings;
  1         2  
  1         29  
4 1     1   5 use strict;
  1         2  
  1         31  
5 1     1   8047 use LWP::UserAgent;
  1         100458  
  1         28  
6 1     1   9 use URI::Escape;
  1         1  
  1         71  
7              
8 1     1   731 use parent qw(SMS::Send::Driver);
  1         327  
  1         5  
9              
10             =head1 NAME
11              
12             SMS::Send::Smstrade - An SMS::Send driver for the smstrade.de service
13              
14             =head1 VERSION
15              
16             Version 0.02
17              
18             =cut
19              
20             our $VERSION = '0.02';
21              
22              
23             =head1 SYNOPSIS
24              
25             # create the sender object
26             my $sender = SMS::Send::->new('Smstrade',
27             _apikey => '123',
28             _route => 'basic',
29             );
30             # send a message
31             my $sent = $sender->send_sms(
32             text => 'You message may use up to 160 chars',
33             to' => '+49 555 4444', # always use the intl. calling prefix
34             );
35            
36             if ( $sent ) {
37             print "Sent message\n";
38             } else {
39             print "Failed to send test message\n";
40             }
41              
42             =head1 DESCRIPTION
43              
44             L is an international L driver for
45             the smstrade service. It is a paid service which offers very competitive
46             prices.
47              
48             =head2 Preparing to use this driver
49              
50             You need to sign-up on L and get an API key.
51              
52             This API key is used instead of a username and password to authenticate yourself.
53              
54             =head2 Disclaimer
55              
56             The authors of this driver take no responibility for any cost accured on your bill
57             by using this module.
58              
59             Using this driver will cost you money. B
60              
61             =head1 METHODS
62              
63             =head2 new
64              
65             # Create new sender using this driver.
66             my $sender = SMS::Send::->new(
67             'Smstrade',
68             _apikey => '123',
69             _route => 'basic',
70             );
71            
72             The C constructor requires at least one parameter, which should be passed
73             throuh from the L constructor.
74              
75             =over
76              
77             =item _apikey
78              
79             The C<_apikey> param is the api key you get after signing up with smstrade.
80              
81             =item _route
82              
83             The C<_route> param determines how much the messages sent will cost you.
84             The more expensive routes offer you more options. See L
85             for more details. Not all features of the different routes are supported right now.
86              
87             Returns a new C object, or dies on error.
88              
89             =back
90              
91             =cut
92              
93             sub new {
94 0     0 1   my $class = shift;
95 0           my %params = @_;
96 0 0         exists $params{_apikey}
97             or die $class."->new requires _apikey parameter\n";
98 0 0         if(exists $params{_route}) {
99 0 0         if($params{_route} !~ m/^(?:basic|gold|direct)/) {
100 0           die $class."->new's _route parameter takes only one of: basic, gold or direct\n";
101             }
102             } else {
103 0           $params{_route} = 'basic';
104             }
105 0 0         exists $params{_from}
106             or $params{_from} = 'SMS::Send::Smstrade';
107 0 0         exists $params{_verbose}
108             or $params{_verbose} = 1;
109 0           my $self = \%params;
110 0           bless $self, $class;
111            
112 0           $self->{_url} = 'https://gateway.smstrade.de/';
113 0           $self->{_ua} = LWP::UserAgent::->new();
114 0           $self->{_ua}->agent('SMS::Send::Smstrade/0.1');
115 0 0         if($self->{_ua}->can('ssl_opts')) {
116 0           $self->{_ua}->ssl_opts( verify_hostname => 0, );
117             }
118            
119 0           return $self;
120             }
121              
122             =head2 responses
123              
124             List all known response codes with their explaination.
125              
126             =cut
127              
128             sub responses {
129 0     0 1   my $self = shift;
130            
131 0 0         if(!$self->{_responses}) {
132 0           $self->{_responses} = $self->_init_responses();
133             }
134            
135 0           return $self->{_responses};
136             }
137              
138             sub _init_responses {
139 0     0     my $self = shift;
140            
141             # see http://www.smstrade.de/pdf/SMS-Gateway_HTTP_API_v2_de.pdf, page 5
142 0           my $resp_ref = {
143             '10' => 'Destination Number not correct (Parameter: to)',
144             '20' => 'Source Number not correct (Parameter: from)',
145             '30' => 'Message not correct (Parameter: message)',
146             '31' => 'Message type not correct (Parameter: messagetype)',
147             '40' => 'SMS Route not correct (Parameter: route)',
148             '50' => 'Identification failed (Parameter: key)',
149             '60' => 'Insufficient Funds.',
150             '70' => 'Destination Network not covered. Use another route.',
151             '71' => 'Feature not available. Use another route.',
152             '80' => 'Failed to submit to SMS-C. Use another route or contact support.',
153             '100' => 'SMS successfull submitted.',
154             };
155            
156 0           return $resp_ref;
157             }
158              
159             =head2 send_sms
160              
161             Send an SMS. See L for the details.
162              
163             =cut
164              
165             sub send_sms {
166 0     0 1   my $self = shift;
167 0           my %params = @_;
168            
169 0           my $destination = $self->_clean_number($params{to});
170 0           my $message = substr($params{text},0,159);
171            
172 0           my %args = (
173             'key' => $self->{_apikey},
174             'message' => $message,
175             'to' => $destination,
176             'route' => $self->{_route},
177             'from' => $self->{_from},
178             'cost' => 1,
179             'message_id' => 1,
180             'count' => 1,
181             );
182            
183 0           my $content = join('&', map { uri_escape($_).'='.uri_escape($args{$_}) } keys %args);
  0            
184            
185 0           my $url = $self->{_url}.'?'.$content;
186 0           my $req = HTTP::Request::->new( GET => $url, );
187 0           my $res = $self->{_ua}->request($req);
188            
189 0 0         print 'Requesting URL '.$url."\n" if $self->{_verbose};
190            
191 0 0 0       if($res->is_success() && $res->content() =~ m/^100\D/) {
192 0 0         print 'Sent '.$message.' to '.$destination."\n" if $self->{_verbose};
193 0           return 1;
194             } else {
195 0           my $errstr = $res->content();
196 0 0         if($self->responses()->{$errstr}) {
197 0           $errstr .= ' - '.$self->responses()->{$errstr};
198             }
199 0 0         warn 'Failed to send '.$message.' to '.$destination.'. Error: '.$errstr if $self->{_verbose};
200 0           return;
201             }
202             }
203              
204             sub _clean_number {
205 0     0     my $self = shift;
206 0           my $number = shift;
207            
208             # strip all non-number chars
209 0           $number =~ s/\D//g;
210            
211 0           return $number;
212             }
213              
214             =head1 AUTHOR
215              
216             Dominik Schulz, C<< >>
217              
218             =head1 BUGS
219              
220             Please report any bugs or feature requests to C, or through
221             the web interface at L. I will be notified, and then you'll
222             automatically be notified of progress on your bug as I make changes.
223              
224              
225              
226              
227             =head1 SUPPORT
228              
229             You can find documentation for this module with the perldoc command.
230              
231             perldoc SMS::Send::Smstrade
232              
233              
234             You can also look for information at:
235              
236             =over 4
237              
238             =item * RT: CPAN's request tracker
239              
240             L
241              
242             =item * AnnoCPAN: Annotated CPAN documentation
243              
244             L
245              
246             =item * CPAN Ratings
247              
248             L
249              
250             =item * Search CPAN
251              
252             L
253              
254             =back
255              
256             =head1 LICENSE AND COPYRIGHT
257              
258             Copyright 2012 Dominik Schulz.
259              
260             This program is free software; you can redistribute it and/or modify it
261             under the terms of either: the GNU General Public License as published
262             by the Free Software Foundation; or the Artistic License.
263              
264             See http://dev.perl.org/licenses/ for more information.
265              
266              
267             =cut
268              
269             1; # End of SMS::Send::Smstrade