#!/usr/bin/perl
#
# example.pl - Example of SMS api usage
#
#
# Copyright (c) 2007, DreamTime.net Inc.
# All rights reserved.
# Email: sales@diamondcard.us
# Phone: 1-303-997-0900
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the DreamTime.net Inc. nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY DreamTime.net Inc. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL DreamTime.net Inc. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#

					# Values to adjust
my $ACCID = 8;				# Your account Id
my $PINCODE =  '040020055087';		# Your pin code
my $SENDFROM = '4412345678945';		# SMS From phone number

                                        # SMS To phone number(s)
					# Always use international format. 
                                        # Do NOT enter any international dialing prefix.
my $DESTINATION = '449876543211';	# Message may be send to one number OR
my @DEST_LIST = ('449876544211',        # several numbers
                 '449876544212');
my $MESSAGE = 'test message';		# Message text to send

                                        # -- Local constants --
my $SERVER = 'http://sms/';
my $URI = 'SMSapi';
my $PROXY = $SERVER;
my $WSDL = $SERVER.'doc/sms-api.wsdl';

                                        # -- External routines --
use SOAP::Lite;
use SOAP::WSDL;
                                        # -- Local variables --
my $SENDIGNID;				# Sending ID for checking message status
my %params;
my $res;

                                        # -- START HERE --
#
# Send SMS
#
%params = ('AccId' => $ACCID,			# Account Id
           'PinCode' => $PINCODE,		# Pin code
           'MsgTxt' => $MESSAGE,		# Message to sent
           'SendFrom' => $SENDFROM,		# Send from
#    	   'Destination' => $DESTINATION,	# Destination number
	    'Destination' => \@DEST_LIST, 	# You can send SMS to the list of destinations
          );

print "Send SMS\n";
$res = &sendRequest('send', \%params);

if ($res->{ErrCode}) {                 	# Fail
  print "Error: $res->{ErrMsg}\n";	# Error message
  if ($res->{ErrCode} eq 'INVDEST') {	# Load list of invalid destinations if available
    if (ref($res->{InvalidDestinations}) eq 'ARRAY') {
      print "List of invalid phone numbers:\n".
    	    join(', ',@{$res->{InvalidDestinations}})."\n";
    }
    elsif ($res->{InvalidDestinations}) {
      print "Invalid phone number: $res->{InvalidDestinations}\n";
    }
  }
}
else {
  $SENDIGNID = $res->{SendingId};
  print "Message Sent. Sending Id for checking status: $res->{SendingId}\n";
}


#
# Check SMS sending status
#
%params = ('AccId' => $ACCID,      	# Account Id
           'PinCode' => $PINCODE,  	# Pin code
           'SendingId' => $SENDIGNID); 	# Sending Id

print "Check SMS sending status\n";
$res = &sendRequest('status', \%params);

if ($res->{ErrCode}) {                 	# Fail
  print "Error: $res->{ErrMsg}\n";	# Error message
}
else {					# Success
  print "Queue: ".$res->{Queue}."\n".
        "Sent: ".$res->{Sent}."\n".
        "Delivered: ".($res->{Delivered} ? $res->{Delivered} : 'N/A')."\n".
        "Failed: ".$res->{Failed}."\n".
        "Cost: ".$res->{Cost}.' '.$res->{Currency}."\n";
}

exit;


#
# You can use SOAP::Lite or SOAP::WSDL packages for sending requests.
#

#
# sendRequest - send request using SOAP::Lite
# Call:		$result = sendRequest($func, $params)
# 
sub sendRequest {
  my ($func, $params) = @_;
  my $soap = SOAP::Lite
                -> uri($URI)
                -> proxy($PROXY)
  		-> on_fault(sub { return {
	                           'ErrMsg' => ref $_[1] ? $_[1]->faultstring : $_[0]->transport->status,
				   'ErrCode' => 'ERR'}
                                })
  		-> $func($params);

  if (!$soap) {
    return {'ErrMsg' => 'SOAP creation error', 
	    'ErrCode' => 'ERR'}; 
  }

  if ($soap->{ErrMsg}) {
    return {'ErrMsg' => $soap->{ErrMsg}, 
	    'ErrCode' => 'ERR'};
  }
  return $soap->result();
}


#
# sendRequestWSDL - send request using SOAP::WSDL
# Call:         $result = sendRequest($func, $params)
#
sub sendRequestWSDL {
  my ($func, $params) = @_;
  my $soap = SOAP::WSDL->new( wsdl => $WSDL);
  $soap->wsdlinit();
  my $som = $soap->call( $func, ('inParams' => $params));
  return $som->result();
}

