| 1 | package SMS::Send::Telenor; | ||
| 2 | use HTTP::Tiny; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.04'; | ||
| 6 | use base 'SMS::Send::Driver'; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($class, @arg_arr) = @_; | ||
| 10 | my $args = { | ||
| 11 | _login => "$arg_arr[1]", | ||
| 12 | _password => "$arg_arr[3]" | ||
| 13 | }; | ||
| 14 | |||
| 15 | die "$class needs hash_ref with _login and password.\n" unless $args->{_login} && $args->{_password}; | ||
| 16 | my $self = bless {%$args}, $class; | ||
| 17 | $self->{send_url} = 'http://sms-pro.net/services/' . $args->{_login} . '/sendsms'; | ||
| 18 | $self->{status_url} = 'http://sms-pro.net/services/' . $args->{_login} . '/status'; | ||
| 19 | $self->{sms_sender} = 'FROM SENDER'; #Add the text that describes who sent the sms | ||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub send_sms { | ||
| 24 | my ($self, @arg_arr) = @_; | ||
| 25 | my $args = { | ||
| 26 | customer_id => "$self->{_login}", | ||
| 27 | password => "$self->{_password}", | ||
| 28 | message => "$arg_arr[1]", | ||
| 29 | to_msisdn => "$arg_arr[3]", | ||
| 30 | sms_sender => "$self->{sms_sender}" | ||
| 31 | }; | ||
| 32 | |||
| 33 | my $sms_xml = _build_sms_xml($args); | ||
| 34 | my $response = _post($self->{send_url}, $sms_xml); | ||
| 35 | |||
| 36 | my $rv = 1; | ||
| 37 | if (defined $response) { | ||
| 38 | $rv = _verify_response("$response->{content}"); | ||
| 39 | } | ||
| 40 | return 1 if $rv eq '0'; | ||
| 41 | return 0; | ||
| 42 | } | ||
| 43 | |||
| 44 | # This status subroutine is not used by SMS::Send but can be used directly with the driver. | ||
| 45 | sub sms_status { | ||
| 46 | my ($self, $mobilectrl_id) = @_; | ||
| 47 | my $args = { | ||
| 48 | customer_id => "$self->{_login}", | ||
| 49 | mobilectrl_id => "$mobilectrl_id" | ||
| 50 | }; | ||
| 51 | my $xml = _build_status_xml($args); | ||
| 52 | return _post($self->{status_url}, $xml); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _post { | ||
| 56 | my ($url, $sms_xml) = @_; | ||
| 57 | return HTTP::Tiny->new->post( | ||
| 58 | $url => { | ||
| 59 | content => $sms_xml, | ||
| 60 | headers => { | ||
| 61 | "Content-Type" => "application/xml", | ||
| 62 | }, | ||
| 63 | }, | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _build_sms_xml { | ||
| 68 | my $args = shift; | ||
| 69 | return '<?xml version="1.0" encoding="ISO-8859-1"?>' | ||
| 70 | . '<mobilectrl_sms>' | ||
| 71 | . '<header>' | ||
| 72 | . '<customer_id>' | ||
| 73 | . $args->{customer_id} | ||
| 74 | . '</customer_id>' | ||
| 75 | . '<password>' | ||
| 76 | . $args->{password} | ||
| 77 | . '</password>' | ||
| 78 | . '<from_alphanumeric>' | ||
| 79 | . $args->{sms_sender} | ||
| 80 | . '</from_alphanumeric>' | ||
| 81 | . '</header>' | ||
| 82 | . '<payload>' | ||
| 83 | . '<sms account="71700">' | ||
| 84 | . '<message><![CDATA[' | ||
| 85 | . $args->{message} | ||
| 86 | . ']]></message>' | ||
| 87 | . '<to_msisdn>' | ||
| 88 | . $args->{to_msisdn} | ||
| 89 | . '</to_msisdn>' | ||
| 90 | . '</sms>' | ||
| 91 | . '</payload>' | ||
| 92 | . '</mobilectrl_sms>'; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _build_status_xml { | ||
| 96 | my $args = shift; | ||
| 97 | return '<?xml version="1.0" encoding="ISO-8859-1"?>' | ||
| 98 | . '<mobilectrl_delivery_status_request>' | ||
| 99 | . '<customer_id>' | ||
| 100 | . $args->{customer_id} | ||
| 101 | . '</customer_id>' | ||
| 102 | . '<status_for type="mobilectrl_id">' | ||
| 103 | . $args->{mobilectrl_id} | ||
| 104 | . '</status_for>' | ||
| 105 | . '</mobilectrl_delivery_status_request>'; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _verify_response { | ||
| 109 | my $content = shift; | ||
| 110 | if ($content =~ /\<status\>(\d)\<\/status>/) { | ||
| 111 | return $1; | ||
| 112 | } | ||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | 1; |