| 1 | package SMS::Send::Telenor; | ||
| 2 | use HTTP::Tiny; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.05'; | ||
| 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; | ||
| 116 | |||
| 117 | =head1 NAME | ||
| 118 | |||
| 119 | SMS::Send::Telenor - SMS::Send driver to send messages via Telenor SMS Pro | ||
| 120 | |||
| 121 | =head1 SYNOPSIS | ||
| 122 | |||
| 123 | use SMS::Send; | ||
| 124 | |||
| 125 | # Create a sender | ||
| 126 | my $sender = SMS::Send->new('Telenor', | ||
| 127 | _login => 'your_aql_username', | ||
| 128 | _password => 'your_aql_password', | ||
| 129 | ); | ||
| 130 | |||
| 131 | # Send a message | ||
| 132 | my $sent = $sender->send_sms( | ||
| 133 | text => 'This is a test message', | ||
| 134 | to => '+4612345678', | ||
| 135 | ); | ||
| 136 | |||
| 137 | if ( $sent ) { | ||
| 138 | print "Message sent ok\n"; | ||
| 139 | } else { | ||
| 140 | print "Failed to send message\n"; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | =head1 DESCRIPTION | ||
| 145 | |||
| 146 | A driver for SMS::Send to send SMS text messages via Telenor SMS Pro API | ||
| 147 | |||
| 148 | This is not intended to be used directly, but instead called by SMS::Send | ||
| 149 | (see synopsis above for a basic illustration, and see SMS::Send's documentation | ||
| 150 | for further information). | ||
| 151 | |||
| 152 | |||
| 153 | =head1 AUTHOR | ||
| 154 | |||
| 155 | Eivin Giske Skaaren, <lt>eivin@sysmystic.com<gt> | ||
| 156 | |||
| 157 | |||
| 158 | =head1 COPYRIGHT AND LICENSE | ||
| 159 | |||
| 160 | Copyright (C) 2015 by Eivin Giske Skaaren | ||
| 161 | |||
| 162 | This library is free software; you can redistribute it and/or modify | ||
| 163 | it under the same terms as Perl itself, either Perl version 5.8.7 or, | ||
| 164 | at your option, any later version of Perl 5 you may have available. | ||
| 165 | |||
| 166 | |||
| 167 | =cut | ||
| 168 | __END__ |