| 1 | package SMS::Send::KR::APIStore; | ||
| 2 | # ABSTRACT: An SMS::Send driver for the apistore.co.kr SMS service | ||
| 3 | |||
| 4 | use utf8; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '0.001'; | ||
| 9 | |||
| 10 | use parent qw( SMS::Send::Driver ); | ||
| 11 | |||
| 12 | use HTTP::Tiny; | ||
| 13 | use JSON; | ||
| 14 | |||
| 15 | our $URL = "http://api.openapi.io/ppurio/1/message"; | ||
| 16 | our $AGENT = 'SMS-Send-KR-APIStore/' . $SMS::Send::KR::APIStore::VERSION; | ||
| 17 | our $TIMEOUT = 3; | ||
| 18 | our $TYPE = 'SMS'; | ||
| 19 | our $DELAY = 0; | ||
| 20 | |||
| 21 | our %ERROR_CODE = ( | ||
| 22 | '4100' => 'sms:전달', | ||
| 23 | '4421' => 'sms:타임아웃', | ||
| 24 | '4426' => 'sms:재시도한도초과', | ||
| 25 | '4425' => 'sms:단말기호처리중', | ||
| 26 | '4400' => 'sms:음영지역', | ||
| 27 | '4401' => 'sms:단말기전원꺼짐', | ||
| 28 | '4402' => 'sms:단말기메시지저장초과', | ||
| 29 | '4410' => 'sms:잘못된번호', | ||
| 30 | '4422' => 'sms:단말기일시정지', | ||
| 31 | '4427' => 'sms:기타단말기문제', | ||
| 32 | '4405' => 'sms:단말기 busy', | ||
| 33 | '4423' => 'sms:단말기착신거부', | ||
| 34 | '4412' => 'sms:착신거절', | ||
| 35 | '4411' => 'sms:NPDB 에러', | ||
| 36 | '4428' => 'sms:시스템에러', | ||
| 37 | '4404' => 'sms:가입자위치정보없음', | ||
| 38 | '4413' => 'sms:SMSC 형식오류', | ||
| 39 | '4414' => 'sms:비가입자,결번,서비스정지', | ||
| 40 | '4424' => 'sms:URL SMS 미지원폰', | ||
| 41 | '4403' => 'sms:메시지삭제됨', | ||
| 42 | '4430' => 'sms:스팸', | ||
| 43 | '4420' => 'sms:기타에러', | ||
| 44 | '6600' => 'mms:전달', | ||
| 45 | '6601' => 'mms:타임아웃', | ||
| 46 | '6602' => 'mms:핸드폰호처리중', | ||
| 47 | '6603' => 'mms:음영지역', | ||
| 48 | '6604' => 'mms:전원이꺼져있음', | ||
| 49 | '6605' => 'mms:메시지저장개수초과', | ||
| 50 | '6606' => 'mms:잘못된번호', | ||
| 51 | '6607' => 'mms:서비스일시정지', | ||
| 52 | '6608' => 'mms:기타단말기문제', | ||
| 53 | '6609' => 'mms:착신거절', | ||
| 54 | '6610' => 'mms:기타에러', | ||
| 55 | '6611' => 'mms:통신사의 SMC 형식오류', | ||
| 56 | '6612' => 'mms:게이트웨이의형식오류', | ||
| 57 | '6613' => 'mms:서비스불가단말기', | ||
| 58 | '6614' => 'mms:핸드폰호불가상태', | ||
| 59 | '6615' => 'mms:SMC 운영자에의해삭제', | ||
| 60 | '6616' => 'mms:통신사의메시지큐초과', | ||
| 61 | '6617' => 'mms:통신사의스팸처리', | ||
| 62 | '6618' => 'mms:공정위의스팸처리', | ||
| 63 | '6619' => 'mms:게이트웨이의스팸처리', | ||
| 64 | '6620' => 'mms:발송건수초과', | ||
| 65 | '6621' => 'mms:메시지의길이초과', | ||
| 66 | '6622' => 'mms:잘못된번호형식', | ||
| 67 | '6623' => 'mms:잘못된데이터형식', | ||
| 68 | '6624' => 'mms:MMS 정보를찾을수없음', | ||
| 69 | '6670' => 'mms:이미지파일크기제한', | ||
| 70 | '9903' => '선불사용자 사용금지', | ||
| 71 | '9904' => 'Block time(날짜제한)', | ||
| 72 | '9082' => '발송해제', | ||
| 73 | '9083' => 'IP 차단', | ||
| 74 | '9023' => 'Callback error', | ||
| 75 | '9905' => 'Block time(요일제한)', | ||
| 76 | '9010' => '아이디 틀림', | ||
| 77 | '9011' => '비밀번호 틀림', | ||
| 78 | '9012' => '중복접속량 많음', | ||
| 79 | '9013' => '발송시간 지난 데이터', | ||
| 80 | '9014' => '시간제한(리포트 수신대기 timeout)', | ||
| 81 | '9020' => 'Wrong Data Format', | ||
| 82 | '9021' => '', | ||
| 83 | '9022' => 'Wrong Data Format(cinfo가 특수 문자/공백을 포함)', | ||
| 84 | '9080' => 'Deny User Ack', | ||
| 85 | '9214' => 'Wrong Phone Num', | ||
| 86 | '9311' => 'Fax File Not Found', | ||
| 87 | '9908' => 'PHONE, FAX 선불사용자 제한기능', | ||
| 88 | '9090' => '기타에러', | ||
| 89 | '-1' => '잘못된 데이터 형식 발송오류', | ||
| 90 | ); | ||
| 91 | |||
| 92 | sub new { | ||
| 93 | my $class = shift; | ||
| 94 | my %params = ( | ||
| 95 | _url => $SMS::Send::KR::APIStore::URL, | ||
| 96 | _agent => $SMS::Send::KR::APIStore::AGENT, | ||
| 97 | _timeout => $SMS::Send::KR::APIStore::TIMEOUT, | ||
| 98 | _from => q{}, | ||
| 99 | _type => $SMS::Send::KR::APIStore::TYPE, | ||
| 100 | _delay => $SMS::Send::KR::APIStore::DELAY, | ||
| 101 | _id => q{}, | ||
| 102 | _api_store_key => q{}, | ||
| 103 | @_, | ||
| 104 | ); | ||
| 105 | |||
| 106 | die "$class->new: _id is needed\n" unless $params{_id}; | ||
| 107 | die "$class->new: _api_store_key is needed\n" unless $params{_api_store_key}; | ||
| 108 | die "$class->new: _from is needed\n" unless $params{_from}; | ||
| 109 | die "$class->new: _type is invalid\n" | ||
| 110 | unless $params{_type} && $params{_type} =~ m/^(SMS|LMS)$/i; | ||
| 111 | |||
| 112 | my $self = bless \%params, $class; | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub send_sms { | ||
| 117 | my $self = shift; | ||
| 118 | my %params = ( | ||
| 119 | _from => $self->{_from}, | ||
| 120 | _type => $self->{_type} || 'SMS', | ||
| 121 | _delay => $self->{_delay} || 0, | ||
| 122 | _subject => $self->{_subject}, | ||
| 123 | _epoch => q{}, | ||
| 124 | @_, | ||
| 125 | ); | ||
| 126 | |||
| 127 | my $text = $params{text}; | ||
| 128 | my $to = $params{to}; | ||
| 129 | my $from = $params{_from}; | ||
| 130 | my $type = $params{_type}; | ||
| 131 | my $delay = $params{_delay}; | ||
| 132 | my $subject = $params{_subject}; | ||
| 133 | my $epoch = $params{_epoch}; | ||
| 134 | |||
| 135 | my %ret = ( | ||
| 136 | success => 0, | ||
| 137 | reason => q{}, | ||
| 138 | detail => +{}, | ||
| 139 | ); | ||
| 140 | |||
| 141 | $ret{reason} = 'text is needed', return \%ret unless $text; | ||
| 142 | $ret{reason} = 'to is needed', return \%ret unless $to; | ||
| 143 | $ret{reason} = '_type is invalid', return \%ret | ||
| 144 | unless $type && $type =~ m/^(SMS|LMS)$/i; | ||
| 145 | |||
| 146 | my $http = HTTP::Tiny->new( | ||
| 147 | agent => $self->{_agent}, | ||
| 148 | timeout => $self->{_timeout}, | ||
| 149 | default_headers => { 'x-waple-authorization' => $self->{_api_store_key} }, | ||
| 150 | ) or $ret{reason} = 'cannot generate HTTP::Tiny object', return \%ret; | ||
| 151 | my $url = sprintf '%s/%s/%s', $self->{_url}, lc($type), $self->{_id}; | ||
| 152 | |||
| 153 | # | ||
| 154 | # delay / send_time: reserve SMS | ||
| 155 | # | ||
| 156 | my $send_time; | ||
| 157 | if ($delay) { | ||
| 158 | my $t = DateTime->now( time_zone => 'Asia/Seoul' )->add( seconds => $delay ); | ||
| 159 | $send_time = $t->ymd(q{}) . $t->hms(q{}); | ||
| 160 | } | ||
| 161 | if ($epoch) { | ||
| 162 | my $t = DateTime->from_epoch( | ||
| 163 | time_zone => 'Asia/Seoul', | ||
| 164 | epoch => $epoch, | ||
| 165 | ); | ||
| 166 | $send_time = $t->ymd(q{}) . $t->hms(q{}); | ||
| 167 | } | ||
| 168 | |||
| 169 | # | ||
| 170 | # subject | ||
| 171 | # | ||
| 172 | undef $subject if $type =~ m/SMS/i; | ||
| 173 | |||
| 174 | my %form = ( | ||
| 175 | dest_phone => $to, | ||
| 176 | send_phone => $from, | ||
| 177 | subject => $subject, | ||
| 178 | msg_body => $text, | ||
| 179 | send_time => $send_time, | ||
| 180 | ); | ||
| 181 | $form{$_} or delete $form{$_} for keys %form; | ||
| 182 | |||
| 183 | my $res = $http->post_form( $url, \%form ); | ||
| 184 | $ret{reason} = 'cannot get valid response for POST request'; | ||
| 185 | if ( $res && $res->{success} ) { | ||
| 186 | $ret{detail} = decode_json( $res->{content} ); | ||
| 187 | $ret{success} = 1 if $ret{detail}{result_code} eq '200'; | ||
| 188 | |||
| 189 | $ret{reason} = 'unknown error'; | ||
| 190 | $ret{reason} = 'user error' if $ret{detail}{result_code} eq '100'; | ||
| 191 | $ret{reason} = 'ok' if $ret{detail}{result_code} eq '200'; | ||
| 192 | $ret{reason} = 'parameter error' if $ret{detail}{result_code} eq '300'; | ||
| 193 | $ret{reason} = 'etc error' if $ret{detail}{result_code} eq '400'; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $ret{detail} = $res; | ||
| 197 | $ret{reason} = 'unknown error'; | ||
| 198 | } | ||
| 199 | |||
| 200 | return \%ret; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub report { | ||
| 204 | my ( $self, $cmid_obj ) = @_; | ||
| 205 | |||
| 206 | my %ret = ( | ||
| 207 | success => 0, | ||
| 208 | reason => q{}, | ||
| 209 | cmid => q{}, | ||
| 210 | call_status => q{}, | ||
| 211 | dest_phone => q{}, | ||
| 212 | report_time => q{}, | ||
| 213 | umid => q{}, | ||
| 214 | ); | ||
| 215 | |||
| 216 | $ret{reason} = 'cmid is needed', return \%ret unless defined $cmid_obj; | ||
| 217 | |||
| 218 | my $cmid; | ||
| 219 | if ( !ref($cmid_obj) ) { | ||
| 220 | $cmid = $cmid_obj; | ||
| 221 | } | ||
| 222 | elsif ( ref($cmid_obj) eq 'HASH' ) { | ||
| 223 | $cmid = $cmid_obj->{detail}{cmid}; | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | $ret{reason} = 'invalid cmid'; | ||
| 227 | return \%ret; | ||
| 228 | } | ||
| 229 | $ret{cmid} = $cmid; | ||
| 230 | |||
| 231 | my $http = HTTP::Tiny->new( | ||
| 232 | agent => $self->{_agent}, | ||
| 233 | timeout => $self->{_timeout}, | ||
| 234 | default_headers => { 'x-waple-authorization' => $self->{_api_store_key} }, | ||
| 235 | ) or $ret{reason} = 'cannot generate HTTP::Tiny object', return \%ret; | ||
| 236 | my $url = sprintf '%s/%s/%s', $self->{_url}, 'report', $self->{_id}; | ||
| 237 | |||
| 238 | my %form = ( cmid => $cmid ); | ||
| 239 | $form{$_} or delete $form{$_} for keys %form; | ||
| 240 | my $params = $http->www_form_urlencode( \%form ); | ||
| 241 | |||
| 242 | my $res = $http->get("$url?$params"); | ||
| 243 | $ret{reason} = 'cannot get valid response for GET request'; | ||
| 244 | if ( $res && $res->{success} ) { | ||
| 245 | my $detail = decode_json( $res->{content} ); | ||
| 246 | |||
| 247 | $ret{success} = 1 if $detail->{call_status} =~ m/^(4100|6600)$/; | ||
| 248 | $ret{reason} = $ERROR_CODE{ $detail->{call_status} }; | ||
| 249 | $ret{call_status} = $detail->{call_status}; | ||
| 250 | $ret{dest_phone} = $detail->{dest_phone}; | ||
| 251 | $ret{report_time} = $detail->{report_time}; | ||
| 252 | $ret{umid} = $detail->{umid}; | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | $ret{detail} = $res; | ||
| 256 | $ret{reason} = 'unknown error'; | ||
| 257 | } | ||
| 258 | |||
| 259 | return \%ret; | ||
| 260 | } | ||
| 261 | |||
| 262 | 1; | ||
| 263 | |||
| 264 | # | ||
| 265 | # This file is part of SMS-Send-KR-APIStore | ||
| 266 | # | ||
| 267 | # This software is copyright (c) 2015 by Keedi Kim. | ||
| 268 | # | ||
| 269 | # This is free software; you can redistribute it and/or modify it under | ||
| 270 | # the same terms as the Perl 5 programming language system itself. | ||
| 271 | # | ||
| 272 | |||
| 273 | __END__ |