File Coverage

blib/lib/WWW/TWSMS.pm
Criterion Covered Total %
statement 18 101 17.8
branch 0 64 0.0
condition 0 12 0.0
subroutine 6 16 37.5
pod 0 10 0.0
total 24 203 11.8


line stmt bran cond sub pod time code
1             package WWW::TWSMS;
2              
3 1     1   23918 use 5.006;
  1         3  
  1         32  
4 1     1   4 use strict;
  1         2  
  1         28  
5 1     1   4 use warnings;
  1         5  
  1         33  
6 1     1   2323 use HTTP::Request::Common qw(GET POST);
  1         30252  
  1         78  
7 1     1   719 use HTTP::Cookies;
  1         12379  
  1         32  
8 1     1   1101 use LWP::UserAgent;
  1         22596  
  1         1476  
9              
10              
11             require Exporter;
12              
13             our @ISA = qw(Exporter);
14              
15             # Items to export into callers namespace by default. Note: do not export
16             # names by default without a very good reason. Use EXPORT_OK instead.
17             # Do not simply export all your public functions/methods/constants.
18              
19             # This allows declaration use WWW::TWSMS ':all';
20             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21             # will save memory.
22             our %EXPORT_TAGS = ( 'all' => [ qw(
23            
24             ) ] );
25              
26             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27              
28             our @EXPORT = qw(
29            
30             );
31              
32             our $VERSION = '0.01';
33              
34              
35             # Preloaded methods go here.
36              
37              
38              
39             sub new {
40            
41 0     0 0   my $class = shift;
42 0           my $self = {NOTHING => 'nothing'};
43 0           my %args = @_;
44            
45 0           bless($self, $class);
46 0           $self->reset();
47              
48 0 0         $self->{m_Username} = $args{Username} if (defined $args{Username});
49 0 0         $self->{m_Password} = $args{Password} if (defined $args{Password});
50 0 0         $self->{m_SmsType} = $args{SmsType} if (defined $args{SmsType});
51 0 0         $self->{m_SmsPopup} = $args{SmsPopup} if (defined $args{SmsPopup});
52 0 0         $self->{m_SmsMo} = $args{SmsMo} if (defined $args{SmsMo});
53 0 0         $self->{m_SmsEncoding} = $args{SmsEncoding} if (defined $args{SmsEncoding});
54 0 0         $self->{m_SmsVldtime} = $args{SmsVldtime} if (defined $args{SmsVldtime});
55 0 0         $self->{m_SmsDlvtime} = $args{Dlvtime} if (defined $args{Dlvtime});
56              
57 0           return $self;
58             }
59              
60              
61             sub reset {
62              
63             # pop value
64 0     0 0   my $self = shift();
65              
66             # check to make sure that this function is being called on an object
67 0 0         die "You must instantiate an object to use this function" if !(ref($self));
68              
69             #-----------------------------------------------------------------
70             # Define default package vars
71             #-----------------------------------------------------------------
72            
73 0           $self->{m_SoftwareWebsite} = "www.twsms.com";
74 0           $self->{m_SoftwareTitle} = "Perl TWSMS API";
75              
76            
77 0           $self->{m_ErrorCode} = 0;
78 0           $self->{m_ErrorDescription} = undef;
79 0           $self->{m_ErrorResolution} = undef;
80            
81 0           $self->{m_StatusCode} = undef;
82 0           $self->{m_StatusDescription} = undef;
83              
84            
85 0           $self->{m_SmsType} = 'now'; # now => 立即傳送 , dlv => 預約發送
86 0           $self->{m_SmsPopup} = ''; # 1 => 讓簡訊先顯示在手機畫面,但不會存到手機
87 0           $self->{m_SmsMo} = ''; # Y => 雙向簡訊
88 0           $self->{m_SmsEncoding} = 'big5'; # big5 => 中英文的內容,長度為70個字元
89             # ascii => 純英文的內容,長度為160個字元
90 0           $self->{m_SmsReceiver} = undef; # 接收簡訊的手機號碼
91 0           $self->{m_SmsVldtime} = undef; # 有效期限,以秒為單位
92 0           $self->{m_SmsDlvtime} = undef; # 預約時間,格式為YYYYMMDDHHII,如200405121830
93             # 當m_SmsType TAG為dlv才有效
94              
95 0           $self->{m_Secure} = 0;
96 0           $self->{m_ConnectionTimeout} = 30;
97 0           $self->{m_RemoteHost} = 'api.twsms.com';
98 0           $self->{m_RemotePort} = 80;
99            
100 0           $self->{m_Username} = undef;
101 0           $self->{m_Password} = undef;
102              
103              
104            
105             }
106              
107             sub msgReceiver {
108              
109 0     0 0   my $self = shift;
110 0 0         if (@_ == 1) {
111 0 0         $self->{m_SmsReceiver} = shift() if ($_[0] =~ m/^\d{10}$/);
112             }
113              
114 0 0 0       return $self->{m_SmsReceiver} if defined($self->{m_SmsReceiver}) || return undef;
115             }
116              
117             sub msgData {
118              
119 0     0 0   my $self = shift;
120            
121             # check to make sure that this function is being called on an object
122 0 0         die "You must instantiate an object to use this function" if !(ref($self));
123              
124 0 0         if (@_ == 1) { $self->{m_SmsMsgData} = shift(); }
  0            
125              
126 0 0 0       return $self->{m_SmsMsgData} if defined($self->{m_SmsMsgData}) || return undef;
127             }
128              
129             sub msgSend {
130            
131 0     0 0   my $self = shift;
132              
133 0           my $ua = LWP::UserAgent->new;
134 0           $ua->agent('Mozilla/5.0');
135              
136 0           my $req = POST "http://$self->{m_RemoteHost}/send.php",
137             [
138             'username' => $self->{m_Username},
139             'password' => $self->{m_Password},
140             'type' => $self->{m_SmsType},
141             'encoding' => $self->{m_SmsEncoding},
142             'mobile' => $self->{m_SmsReceiver},
143             'message' => $self->{m_SmsMsgData},
144             'popup' => $self->{m_SmsPopup},
145             'mo' => $self->{m_SmsMo},
146             'vldtime' => $self->{m_SmsVldtime},
147             'dlvtime' => $self->{m_SmsDlvtime}
148             ];
149              
150 0           my $file = $ua->request($req)->as_string;
151 0 0         my $CheckRes = $1 if ($file =~ m/msgid=(.*)/);
152 0 0         if ($CheckRes <= 0){
153 0           $self->errorCode($CheckRes);
154 0           $self->statusDescription('傳送失敗');
155             } else {
156 0           $self->statusCode($CheckRes);
157 0           $self->statusDescription('傳送完成');
158             }
159            
160             }
161              
162             sub statusCode {
163             # pop value
164 0     0 0   my $self = shift();
165            
166             # check to make sure that this function is being called on an object
167 0 0         die "You must instantiate an object to use this function" if !(ref($self));
168              
169 0 0         if (@_ == 1) { $self->{m_StatusCode} = shift(); }
  0            
170              
171 0 0 0       return $self->{m_StatusCode} if defined($self->{m_StatusCode}) || return undef;
172             }
173              
174             sub statusDescription {
175             # pop value
176 0     0 0   my $self = shift();
177            
178             # check to make sure that this function is being called on an object
179 0 0         die "You must instantiate an object to use this function" if !(ref($self));
180              
181 0 0         if (@_ == 1) { $self->{m_StatusDescription} = shift(); }
  0            
182              
183 0 0 0       return $self->{m_StatusDescription} if defined($self->{m_StatusDescription}) || return undef;
184             }
185              
186             sub errorCode {
187             # pop value
188 0     0 0   my $self = shift();
189 0           my %ERR_DESCRIPTION = ( '-1' => '發送失敗',
190             '-2' => '帳號或密碼錯誤',
191             '-3' => 'popup TAG 設定錯誤',
192             '-4' => 'mo TAG 設定錯誤',
193             '-5' => 'encoding TAG 設定錯誤',
194             '-6' => 'mobile TAG 設定錯誤',
195             '-7' => 'message TAG 設定錯誤',
196             '-8' => 'vldtime TAG 設定錯誤',
197             '-9' => 'dlvtime TAG 設定錯誤',
198             '-10' => '簡訊通數不足',
199             '-11' => '帳號停用',
200             '-12' => 'type TAG 設定錯誤',
201             '-13' => '當使用wap push功能時,不可採用預約方式發送',
202             '-14' => '來源IP沒有使用權限',
203             '-99' => '系統錯誤(如出現此錯誤,請通知客服中心)'
204             );
205             # check to make sure that this function is being called on an object
206 0 0         die "You must instantiate an object to use this function" if !(ref($self));
207              
208              
209              
210 0 0         if (@_ == 1) {
211 0           $self->{m_ErrorCode} = shift();
212 0 0         $ERR_DESCRIPTION{$self->{m_ErrorCode}} ? $self->errorDescription($ERR_DESCRIPTION{$self->{m_ErrorCode}}) : $self->errorDescription($self->{m_ErrorCode});
213             }
214              
215 0 0 0       return $self->{m_ErrorCode} if defined($self->{m_ErrorCode}) || return undef;
216             }
217              
218             sub errorDescription {
219             # pop value
220 0     0 0   my $self = shift();
221              
222             # check to make sure that this function is being called on an object
223 0 0         die "You must instantiate an object to use this function" if !(ref($self));
224              
225 0 0         if (@_ == 1) { $self->{m_ErrorDescription} = shift(); }
  0            
226              
227 0 0 0       return $self->{m_ErrorDescription} if defined($self->{m_ErrorDescription}) || return undef;
228             }
229              
230             sub success {
231             # pop value
232 0     0 0   my $self = shift();
233            
234             # check to make sure that this function is being called on an object
235 0 0         die "You must instantiate an object to use this function" if !(ref($self));
236              
237             # if the error_code is between 0 and 10 then its an okay response.
238 0 0         if ($self->errorCode > 0) {
239 0           return 1;
240             }
241            
242 0           return 0;
243             }
244              
245              
246              
247             1;
248             __END__