File Coverage

blib/lib/Net/SMS/Cellsynt.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # Copyright 2009-2011, Olof Johansson
3             #
4             # This program is free software; you can redistribute it and/or
5             # modify it under the same terms as Perl itself.
6              
7             package Net::SMS::Cellsynt;
8             our $VERSION = 0.31;
9 1     1   27242 use strict;
  1         2  
  1         42  
10 1     1   6 use warnings;
  1         1  
  1         27  
11 1     1   1639 use WWW::Curl::Easy;
  0            
  0            
12             use URI;
13             use URI::Escape;
14             use URI::QueryParam;
15             use Carp;
16              
17             =pod
18              
19             =head1 NAME
20              
21             Net::SMS::Cellsynt - Send SMS through Cellsynt SMS gateway
22              
23             =head1 SYNOPSIS
24              
25             use Net::SMS::Cellsynt;
26            
27             $sms = Net::SMS::Cellsynt->new(
28             origtype=>'alpha',
29             orig=>'zibri',
30             username=>'foo',
31             password=>'bar',
32             );
33              
34             $sms->send_sms(
35             to=>$recipient,
36             text=>'this text is being sent to you bu Net::SMS::Cellsynt',
37             );
38              
39             =head1 DESCRIPTION
40              
41             Net::SMS::Cellsynt provides a perl object oriented interface to the
42             Cellsynt SMS HTTP API, which allows you to send SMS from within your
43             script or application.
44              
45             To use this module you must have a Cellsynt account.
46              
47             =head1 CONSTRUCTOR
48              
49             =head2 new( parameters )
50            
51             =head3 MANDATORY PARAMETERS
52              
53             =over 8
54              
55             =item username => $username
56            
57             Your Cellsynt username.
58              
59             =item password => $password
60              
61             Your Cellsynt password.
62              
63             =item origtype => $origtype
64              
65             Type of originator. This can be either "alpha", where you supply a
66             string in orig parameter that the recpient will see as sender (note
67             that the recipient cannot answer this types of SMS); numeric, where
68             you supply a telephone number in the orig parameter and shortcode
69             where you supply a numerical short code for a operator network.
70              
71             =item orig => $orig
72              
73             This is the "sender" the recpient sees when recieving the SMS.
74             Depending on the value of origtype this should be a string, a
75             telephone number or a numerical shortcode. (See origtype)
76              
77             =back
78              
79             =head3 OPTIONAL PARAMETERS
80              
81             =over 8
82              
83             =item ttl
84              
85             This value determines how long the SMS can be tried to be delivered,
86             in seconds. If this value is above the operator's max value, the
87             operator's value is used. Default is not set.
88              
89             =item concat
90              
91             Setting this to a value above 1 will allow for longer SMS:es to be
92             sent. One SMS can use 153 bytes, and with this you can send up to
93             6 SMS:es (918 bytes).
94              
95             =item simulate
96              
97             If set to a value other than 0, the module will output the URI that
98             would be used if this wasn't a simulation, and return, when callng
99             the B subroutine. Default is 0.
100              
101             =item uri
102              
103             Set an alternative URI to a service implementing the Cellsynt API.
104             Default is "https://se-1.cellsynt.net/sms.php".
105              
106             =back
107              
108             =cut
109              
110             sub new {
111             my $class = shift;
112             my $self = {
113             uri => 'https://se-1.cellsynt.net/sms.php',
114             simulate => 0,
115             @_,
116             };
117             $self->{curl} = new WWW::Curl::Easy;
118              
119             bless $self, $class;
120             return $self;
121             }
122              
123             =head1 METHODS
124              
125             =head2 send_sms(to=>$recipient, $text=>"msg")
126              
127             Will send message "msg" as an SMS to $recipient, unless the
128             object has set the simulate object; then the send_msg will output
129             the URI that would be used to send the SMS.
130              
131             $recipient is a telephone number in international format: The
132             Swedish telephone number 0700123456 will translate into
133             0046700123456 --- it is the caller's responsibility to convert
134             numbers into this format before calling send_sms.
135              
136             The $text parameter is the SMS "body". This must be encoded using
137             ISO-8859-1. It must not be longer than 160 characters.
138              
139             The method will return a hashref containing a status key. If the
140             status key is "ok", the key "id" is also present, containing the
141             tracking ID supplied by the SMS gateway. If the status key
142             matches /error-\w+/, the key "message" is also present. I.e.:
143              
144             { status => 'ok', id => 'abcdef123456' }
145             { status => 'error-interal', message => 'example error message' }
146             { status => 'error-gateway', message => 'example error message' }
147              
148             The module differentiate between errors from the SMS gateway
149             provider and internal errors. The message in error-gateway comes
150             directly from the provider.
151              
152             =cut
153              
154             sub send_sms {
155             my $self = shift;
156             my $param = {
157             @_,
158             };
159              
160             my $base = $self->{uri};
161             my $test = $self->{test};
162              
163             my $username = $self->{username};
164             my $password = $self->{password};
165             my $origtype = $self->{origtype};
166             my $orig = $self->{orig};
167             #my $text = uri_escape($param->{text});
168             my $text = $param->{text};
169             my $ttl = $param->{ttl};
170             my $concat = $param->{concat};
171              
172             my $dest = $param->{to};
173              
174             my $uri = URI->new($base);
175              
176             if($dest !~ /^00/) {
177             return {
178             status => 'error-internal',
179             message => 'Phone number not in expected format'
180             };
181             }
182              
183             $uri->query_param(username => $username);
184             $uri->query_param(password => $password);
185             $uri->query_param(destination => $dest);
186             $uri->query_param(text => $text);
187             $uri->query_param(originatortype => $origtype);
188             $uri->query_param(originator => $orig);
189              
190             $uri->query_param(expiry => $ttl) if defined $ttl;
191             $uri->query_param(concat => $concat) if defined $concat;
192              
193             # this username is used in the example script.
194             if($username eq 'zibri') {
195             return {
196             status => 'error-internal',
197             message => 'Don\'t run the example script as is',
198             };
199             }
200              
201             if($test) {
202             return {
203             status => 'ok-test',
204             uri => $uri,
205             };
206             }
207              
208             my $body;
209              
210             my $curl = new WWW::Curl::Easy;
211             open(my $curld, ">", \$body);
212             $curl->setopt(CURLOPT_URL, $uri);
213             $curl->setopt(CURLOPT_WRITEDATA, \$curld);
214             $curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
215             $curl->perform();
216             close $curld;
217              
218             if(not defined $body) {
219             return {
220             status => 'error-internal',
221             message => 'SMS gateway does not follow '.
222             'protocol (empty body)',
223             };
224             } elsif($body =~ /^OK: (.*)/) {
225             return {
226             status => 'ok',
227             uri => $1,
228             };
229             } elsif($body=~/^Error: (.*)/) {
230             return {
231             status => 'error-gateway',
232             uri => $1,
233             };
234             } else {
235             return {
236             status => 'error-internal',
237             message => 'SMS gateway does not follow protocol',
238             };
239             }
240             }
241              
242             =head2 sender(origtype=>$origtype, orig=>$orig)
243              
244             Update sender. You can set either or both values. See constructor
245             documentation for valid values.
246              
247             =cut
248              
249             sub sender {
250             my $self = shift;
251             my $param = {
252             @_,
253             };
254              
255             $self->{origtype} = $param->{origtype} if $param->{origtype};
256             $self->{orig} = $param->{orig} if $param->{orig};
257             }
258              
259             1;
260              
261             =head1 SEE ALSO
262              
263             http://cellsynt.com/
264              
265             =head1 AVAILABILITY
266              
267             Latest stable version is available on CPAN. Current development
268             version is available on https://github.com/olof/Net-SMS-Cellsynt.
269              
270             =head1 COPYRIGHT
271              
272             Copyright (c) 2009-2011, Olof 'zibri' Johansson
273             All rights reserved.
274              
275             This program is free software; you can redistribute it and/or
276             modify it under the same terms as Perl itself.
277              
278             =cut
279