| 1 | package SMS::Send::KR::CoolSMS; | ||
| 2 | # ABSTRACT: An SMS::Send driver for the coolsms.co.kr service | ||
| 3 | $SMS::Send::KR::CoolSMS::VERSION = '1.003'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use parent qw( SMS::Send::Driver ); | ||
| 7 | |||
| 8 | use DateTime; | ||
| 9 | use Digest::HMAC_MD5; | ||
| 10 | use HTTP::Tiny; | ||
| 11 | use JSON; | ||
| 12 | use String::Random; | ||
| 13 | |||
| 14 | our $URL = "https://api.coolsms.co.kr/sms/1.1"; | ||
| 15 | our $AGENT = 'SMS-Send-KR-CoolSMS/' . $SMS::Send::KR::CoolSMS::VERSION; | ||
| 16 | our $TIMEOUT = 3; | ||
| 17 | our $TYPE = 'SMS'; | ||
| 18 | our $COUNTRY = 'KR'; | ||
| 19 | our $DELAY = 0; | ||
| 20 | |||
| 21 | # | ||
| 22 | # supported country code from coolsms HTTP API PDF document | ||
| 23 | # http://open.coolsms.co.kr/download/222303 | ||
| 24 | # | ||
| 25 | my %country_code = ( | ||
| 26 | AR => { code => "AR", no => 54, name => "Argentina" }, | ||
| 27 | AM => { code => "AM", no => 374, name => "Armenia" }, | ||
| 28 | AU => { code => "AU", no => 61, name => "Australia" }, | ||
| 29 | AT => { code => "AT", no => 43, name => "Austria" }, | ||
| 30 | BH => { code => "BH", no => 973, name => "Bahrain" }, | ||
| 31 | BD => { code => "BD", no => 880, name => "Bangladesh" }, | ||
| 32 | BE => { code => "BE", no => 32, name => "Belgium" }, | ||
| 33 | BT => { code => "BT", no => 975, name => "Bhutan" }, | ||
| 34 | BO => { code => "BO", no => 591, name => "Bolivia" }, | ||
| 35 | BR => { code => "BR", no => 55, name => "Brazil" }, | ||
| 36 | BN => { code => "BN", no => 673, name => "Brunei Darussalam" }, | ||
| 37 | BG => { code => "BG", no => 359, name => "Bulgaria" }, | ||
| 38 | KH => { code => "KH", no => 855, name => "Cambodia" }, | ||
| 39 | CM => { code => "CM", no => 237, name => "Cameroon" }, | ||
| 40 | CL => { code => "CL", no => 56, name => "Chile" }, | ||
| 41 | CN => { code => "CN", no => 86, name => "China" }, | ||
| 42 | CO => { code => "CO", no => 57, name => "Colombia" }, | ||
| 43 | CU => { code => "CU", no => 53, name => "Cuba" }, | ||
| 44 | DK => { code => "DK", no => 45, name => "Denmark" }, | ||
| 45 | EG => { code => "EG", no => 20, name => "Egypt" }, | ||
| 46 | ET => { code => "ET", no => 251, name => "Ethiopia" }, | ||
| 47 | FI => { code => "FI", no => 358, name => "Finland" }, | ||
| 48 | FR => { code => "FR", no => 33, name => "France" }, | ||
| 49 | GA => { code => "GA", no => 241, name => "Gabon" }, | ||
| 50 | DE => { code => "DE", no => 49, name => "Germany" }, | ||
| 51 | GH => { code => "GH", no => 233, name => "Ghana" }, | ||
| 52 | GR => { code => "GR", no => 30, name => "Greece" }, | ||
| 53 | GL => { code => "GL", no => 299, name => "Greenland" }, | ||
| 54 | GY => { code => "GY", no => 592, name => "Guyana" }, | ||
| 55 | HK => { code => "HK", no => 852, name => "Hong Kong" }, | ||
| 56 | HU => { code => "HU", no => 36, name => "Hungary" }, | ||
| 57 | IS => { code => "IS", no => 354, name => "Iceland" }, | ||
| 58 | IN => { code => "IN", no => 91, name => "India" }, | ||
| 59 | IR => { code => "IR", no => 98, name => "Iran" }, | ||
| 60 | IQ => { code => "IQ", no => 964, name => "Iraq" }, | ||
| 61 | IE => { code => "IE", no => 353, name => "Ireland" }, | ||
| 62 | IL => { code => "IL", no => 972, name => "Israel" }, | ||
| 63 | IT => { code => "IT", no => 39, name => "Italy" }, | ||
| 64 | JP => { code => "JP", no => 81, name => "Japan" }, | ||
| 65 | KZ => { code => "KZ", no => 7, name => "Kazakhstan" }, | ||
| 66 | KE => { code => "KE", no => 254, name => "Kenya" }, | ||
| 67 | KR => { code => "KR", no => 82, name => "Korea" }, | ||
| 68 | KW => { code => "KW", no => 965, name => "Kuwait" }, | ||
| 69 | LA => { code => "LA", no => 856, name => "Lao People's Democratic Republic" }, | ||
| 70 | LB => { code => "LB", no => 961, name => "Lebanon" }, | ||
| 71 | LY => { code => "LY", no => 218, name => "Libya" }, | ||
| 72 | LU => { code => "LU", no => 352, name => "Luxembourg" }, | ||
| 73 | MO => { code => "MO", no => 853, name => "Macao" }, | ||
| 74 | MG => { code => "MG", no => 261, name => "Madagascar" }, | ||
| 75 | MY => { code => "MY", no => 60, name => "Malaysia" }, | ||
| 76 | MX => { code => "MX", no => 52, name => "Mexico" }, | ||
| 77 | MC => { code => "MC", no => 377, name => "Monaco" }, | ||
| 78 | MN => { code => "MN", no => 976, name => "Mongolia" }, | ||
| 79 | MM => { code => "MM", no => 95, name => "Myanmar" }, | ||
| 80 | NP => { code => "NP", no => 977, name => "Nepal" }, | ||
| 81 | NL => { code => "NL", no => 31, name => "Netherlands" }, | ||
| 82 | NZ => { code => "NZ", no => 64, name => "New Zealand" }, | ||
| 83 | NG => { code => "NG", no => 234, name => "Nigeria" }, | ||
| 84 | NO => { code => "NO", no => 47, name => "Norway" }, | ||
| 85 | PK => { code => "PK", no => 92, name => "Pakistan" }, | ||
| 86 | PY => { code => "PY", no => 595, name => "Paraguay" }, | ||
| 87 | PH => { code => "PH", no => 63, name => "Philippines" }, | ||
| 88 | PL => { code => "PL", no => 48, name => "Poland" }, | ||
| 89 | PT => { code => "PT", no => 351, name => "Portugal" }, | ||
| 90 | RO => { code => "RO", no => 40, name => "Romania" }, | ||
| 91 | RU => { code => "RU", no => 7, name => "Russian Federation" }, | ||
| 92 | SN => { code => "SN", no => 221, name => "Senegal" }, | ||
| 93 | SG => { code => "SG", no => 65, name => "Singapore" }, | ||
| 94 | SK => { code => "SK", no => 42, name => "Slovakia" }, | ||
| 95 | SI => { code => "SI", no => 386, name => "Slovenia" }, | ||
| 96 | ZA => { code => "ZA", no => 27, name => "South Africa" }, | ||
| 97 | ES => { code => "ES", no => 34, name => "Spain" }, | ||
| 98 | LK => { code => "LK", no => 94, name => "Sri Lanka" }, | ||
| 99 | SZ => { code => "SZ", no => 268, name => "Swaziland" }, | ||
| 100 | SE => { code => "SE", no => 46, name => "Sweden" }, | ||
| 101 | CH => { code => "CH", no => 41, name => "Switzerland" }, | ||
| 102 | SY => { code => "SY", no => 963, name => "Syrian Arab Republic" }, | ||
| 103 | TW => { code => "TW", no => 886, name => "Taiwan" }, | ||
| 104 | TH => { code => "TH", no => 66, name => "Thailand" }, | ||
| 105 | TR => { code => "TR", no => 90, name => "Turkey" }, | ||
| 106 | AE => { code => "AE", no => 971, name => "United Arab Emirates" }, | ||
| 107 | GB => { code => "GB", no => 44, name => "United Kingdom" }, | ||
| 108 | US => { code => "US", no => 1, name => "United States" }, | ||
| 109 | UZ => { code => "UZ", no => 7, name => "Uzbekistan" }, | ||
| 110 | VE => { code => "VE", no => 58, name => "Venezuela" }, | ||
| 111 | VN => { code => "VN", no => 84, name => "Viet Nam" }, | ||
| 112 | ); | ||
| 113 | |||
| 114 | # 7 => [ KZ / RU / UZ ] | ||
| 115 | my %country_no = map { $country_code{$_}{no} => $country_code{$_} } keys %country_code; | ||
| 116 | |||
| 117 | sub new { | ||
| 118 | my $class = shift; | ||
| 119 | my %params = ( | ||
| 120 | _url => $SMS::Send::KR::CoolSMS::URL, | ||
| 121 | _agent => $SMS::Send::KR::CoolSMS::AGENT, | ||
| 122 | _timeout => $SMS::Send::KR::CoolSMS::TIMEOUT, | ||
| 123 | _api_key => q{}, | ||
| 124 | _api_secret => q{}, | ||
| 125 | _from => q{}, | ||
| 126 | _type => $SMS::Send::KR::CoolSMS::TYPE, | ||
| 127 | _country => $SMS::Send::KR::CoolSMS::COUNTRY, | ||
| 128 | _delay => $SMS::Send::KR::CoolSMS::DELAY, | ||
| 129 | @_, | ||
| 130 | ); | ||
| 131 | |||
| 132 | die "$class->new: _api_key is needed\n" unless $params{_api_key}; | ||
| 133 | die "$class->new: _api_secret is needed\n" unless $params{_api_secret}; | ||
| 134 | die "$class->new: _from is needed\n" unless $params{_from}; | ||
| 135 | die "$class->new: _type is invalid\n" unless $params{_type} && $params{_type} =~ m/^(SMS|LMS)$/i; | ||
| 136 | |||
| 137 | my $self = bless \%params, $class; | ||
| 138 | return $self; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _auth_params { | ||
| 142 | my $self = shift; | ||
| 143 | |||
| 144 | my $api_key = $self->{_api_key}; | ||
| 145 | my $api_secret = $self->{_api_secret}; | ||
| 146 | |||
| 147 | my %auth_params = do { | ||
| 148 | my $time = time; | ||
| 149 | my $salt = String::Random::random_regex('\w{30}'); | ||
| 150 | my $signature = Digest::HMAC_MD5::hmac_md5_hex( "$time$salt", $api_secret ); | ||
| 151 | |||
| 152 | ( | ||
| 153 | api_key => $api_key, | ||
| 154 | timestamp => $time, | ||
| 155 | salt => $salt, | ||
| 156 | signature => $signature, | ||
| 157 | algorithm => 'md5', | ||
| 158 | encoding => 'hex', | ||
| 159 | ); | ||
| 160 | }; | ||
| 161 | |||
| 162 | return %auth_params; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub balance { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | my %ret = ( | ||
| 169 | success => 0, | ||
| 170 | reason => q{}, | ||
| 171 | detail => +{}, | ||
| 172 | ); | ||
| 173 | |||
| 174 | my $http = HTTP::Tiny->new( | ||
| 175 | agent => $self->{_agent}, | ||
| 176 | timeout => $self->{_timeout}, | ||
| 177 | SSL_options => { SSL_hostname => q{} }, # coolsms does not support SNI | ||
| 178 | ) or $ret{reason} = 'cannot generate HTTP::Tiny object', return \%ret; | ||
| 179 | my $url = $self->{_url} . "/balance"; | ||
| 180 | |||
| 181 | my $params = $http->www_form_urlencode(+{ $self->_auth_params }); | ||
| 182 | my $res = $http->get( "$url?$params" ); | ||
| 183 | $ret{reason} = 'cannot get valid response for GET request'; | ||
| 184 | if ( $res && $res->{success} ) { | ||
| 185 | $ret{detail} = decode_json( $res->{content} ); | ||
| 186 | $ret{reason} = 'OK'; | ||
| 187 | $ret{success} = 1; | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | $ret{detail} = $res; | ||
| 191 | $ret{reason} = $res->{reason}; | ||
| 192 | } | ||
| 193 | |||
| 194 | return \%ret; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub send_sms { | ||
| 198 | my $self = shift; | ||
| 199 | my %params = ( | ||
| 200 | _from => $self->{_from}, | ||
| 201 | _country => $self->{_country} || 'KR', | ||
| 202 | _type => $self->{_type} || 'SMS', | ||
| 203 | _delay => $self->{_delay} || 0, | ||
| 204 | _subject => $self->{_subject}, | ||
| 205 | _epoch => q{}, | ||
| 206 | @_, | ||
| 207 | ); | ||
| 208 | |||
| 209 | my $text = $params{text}; | ||
| 210 | my $to = $params{to}; | ||
| 211 | my $from = $params{_from}; | ||
| 212 | my $country = $params{_country}; | ||
| 213 | my $type = $params{_type}; | ||
| 214 | my $delay = $params{_delay}; | ||
| 215 | my $subject = $params{_subject}; | ||
| 216 | my $epoch = $params{_epoch}; | ||
| 217 | |||
| 218 | my %ret = ( | ||
| 219 | success => 0, | ||
| 220 | reason => q{}, | ||
| 221 | detail => +{}, | ||
| 222 | ); | ||
| 223 | |||
| 224 | $ret{reason} = 'text is needed', return \%ret unless $text; | ||
| 225 | $ret{reason} = 'to is needed', return \%ret unless $to; | ||
| 226 | $ret{reason} = '_type is invalid', return \%ret unless $type && $type =~ m/^(SMS|LMS)$/i; | ||
| 227 | |||
| 228 | my $http = HTTP::Tiny->new( | ||
| 229 | agent => $self->{_agent}, | ||
| 230 | timeout => $self->{_timeout}, | ||
| 231 | SSL_options => { SSL_hostname => q{} }, # coolsms does not support SNI | ||
| 232 | ) or $ret{reason} = 'cannot generate HTTP::Tiny object', return \%ret; | ||
| 233 | my $url = $self->{_url} . "/send"; | ||
| 234 | |||
| 235 | # | ||
| 236 | # country & to: adjust country code and destination number | ||
| 237 | # | ||
| 238 | if ( $to =~ /^\+(\d{1})/ && $country_no{$1} ) { | ||
| 239 | $country = $country_no{$1}{code}; | ||
| 240 | $to =~ s/^\+\d{1}//; | ||
| 241 | } | ||
| 242 | elsif ( $to =~ /^\+(\d{2})/ && $country_no{$1} ) { | ||
| 243 | $country = $country_no{$1}{code}; | ||
| 244 | $to =~ s/^\+\d{2}//; | ||
| 245 | } | ||
| 246 | elsif ( $to =~ /^\+(\d{3})/ && $country_no{$1} ) { | ||
| 247 | $country = $country_no{$1}{code}; | ||
| 248 | $to =~ s/^\+\d{3}//; | ||
| 249 | } | ||
| 250 | |||
| 251 | # | ||
| 252 | # datetime: reserve SMS | ||
| 253 | # | ||
| 254 | my $datetime; | ||
| 255 | if ($epoch) { | ||
| 256 | my $t = DateTime->from_epoch( | ||
| 257 | time_zone => 'Asia/Seoul', | ||
| 258 | epoch => $epoch, | ||
| 259 | ); | ||
| 260 | $datetime = $t->ymd(q{}) . $t->hms(q{}); | ||
| 261 | } | ||
| 262 | |||
| 263 | # | ||
| 264 | # subject | ||
| 265 | # | ||
| 266 | undef $subject if $type =~ m/SMS/i; | ||
| 267 | |||
| 268 | my %form = ( | ||
| 269 | $self->_auth_params, # authentication | ||
| 270 | to => $to, | ||
| 271 | from => $from, | ||
| 272 | text => $text, | ||
| 273 | type => uc $type, | ||
| 274 | #image | ||
| 275 | #image_encoding | ||
| 276 | #refname | ||
| 277 | country => $country, | ||
| 278 | datetime => $datetime, | ||
| 279 | subject => $subject, | ||
| 280 | charset => 'utf8', | ||
| 281 | #srk | ||
| 282 | #mode | ||
| 283 | #extension | ||
| 284 | delay => $delay, | ||
| 285 | ); | ||
| 286 | $form{$_} or delete $form{$_} for keys %form; | ||
| 287 | |||
| 288 | my $res = $http->post_form( $url, \%form ); | ||
| 289 | $ret{reason} = 'cannot get valid response for POST request'; | ||
| 290 | if ( $res && $res->{success} ) { | ||
| 291 | $ret{detail} = decode_json( $res->{content} ); | ||
| 292 | $ret{reason} = $ret{detail}{result_message}; | ||
| 293 | $ret{success} = 1 if $ret{detail}{result_code} eq '00'; | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | $ret{detail} = $res; | ||
| 297 | $ret{reason} = $res->{reason}; | ||
| 298 | } | ||
| 299 | |||
| 300 | return \%ret; | ||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ |