blib/lib/Net/SMS/Cellsynt.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #!/usr/bin/perl
2 # Copyright 2009-2011, 2015, Olof Johansson <olof@cpan.org>
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.32';
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use WWW::Curl::Easy;
Subroutine BEGIN: called CC=1 SCAR=2.6
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<send_sms> 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 $self->{curl}->setopt(CURLOPT_URL, $uri);
210 $self->{curl}->setopt(CURLOPT_WRITEDATA, \$body);
211 $self->{curl}->setopt(CURLOPT_FOLLOWLOCATION, 1);
212 $self->{curl}->perform();
213  
214 if(not $body) {
215 return {
216 status => 'error-internal',
217 message => 'SMS gateway does not follow '.
218 'protocol (empty body)',
219 };
220 } elsif($body =~ /^OK: (.*)/) {
221 return {
222 status => 'ok',
223 id => $1,
224  
225 # Becuase of a bug in previous versions, we didn't
226 # set an "id" key, but instead, the reference id
227 # was reported via the "uri" key. For backwards
228 # compatibility, we'll continue support uri as well,
229 # but this will be removed in a future version.
230 uri => $1,
231 };
232 } elsif($body=~/^Error: (.*)/) {
233 return {
234 status => 'error-gateway',
235 message => $1,
236  
237 # Becuase of a bug in previous versions, we didn't
238 # set a "message" key, but instead, the error text
239 # was reported via the "uri" key. For backwards
240 # compatibility, we'll continue support uri as well,
241 # but this will be removed in a future version.
242 uri => $1,
243 };
244 } else {
245 return {
246 status => 'error-internal',
247 message => 'SMS gateway does not follow protocol',
248 };
249 }
250 }
251  
252 =head2 sender(origtype=>$origtype, orig=>$orig)
253  
254 Update sender. You can set either or both values. See constructor
255 documentation for valid values.
256  
257 =cut
258  
259 sub sender {
260 my $self = shift;
261 my $param = {
262 @_,
263 };
264  
265 $self->{origtype} = $param->{origtype} if $param->{origtype};
266 $self->{orig} = $param->{orig} if $param->{orig};
267 }
268  
269 1;
270  
271 =head1 SEE ALSO
272  
273 http://cellsynt.com/
274  
275 =head1 AVAILABILITY
276  
277 Latest stable version is available on CPAN. Current development
278 version is available on https://github.com/olof/Net-SMS-Cellsynt.
279  
280 =head1 COPYRIGHT
281  
282 Copyright (c) 2009-2011, 2015, Olof 'zibri' Johansson <olof@cpan.org>
283 All rights reserved.
284  
285 This program is free software; you can redistribute it and/or
286 modify it under the same terms as Perl itself.
287  
288 =cut
↑ Summary