# -*- encoding : utf-8 -*- require 'savon' require 'log4r' require 'wtf_lang' module Esendex class Client include Log4r INBOX_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/InboxService.asmx?wsdl' SEND_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/SendService.asmx?wsdl' ACCOUNT_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/AccountService.asmx?wsdl' attr_accessor :username, :password, :account_reference attr_reader :sent_message_ids, :sent_message_statuses def initialize(*args) @username = args.shift @password = args.shift @account_reference = args.shift @sent_message_ids, @sent_message_statuses = [], {} Savon.configure do |config| config.raise_errors = true config.log = false config.log_level = :error HTTPI.log = false end @log = Logger.new "#{self.class.name}" @log.outputters = Outputter.stdout @log.level = ERROR @message_kind = String.new end def send_message(recipient, text, originator, validityperiod) @log.debug "TO:#{recipient} text size:#{text.size} in #{text.full_lang} language" #TODO: handle other languages as well unless text.full_lang == 'russian' sms_parts = text.scan(/.{610}/mu) sms_parts << text[sms_parts.to_s.size..text.size] @message_kind = 'Text' @log.debug "#{@message_kind}:#{text.scan(/./mu)} = #{text.size}. #{sms_parts.size} part/s." sms(recipient, sms_parts, originator, validityperiod) else sms_parts = text.scan(/.{70}/mu) sms_parts << text[sms_parts.to_s.size..text.size] @message_kind = 'Unicode' @log.debug "#{@message_kind}:#{text.scan(/./mu)} != #{text.size}. #{sms_parts.size} part/s." sms(recipient, sms_parts, originator, validityperiod) end end def get_credit client = Savon::Client.new(ACCOUNT_SERVICE_WSDL) #http://jira.codehaus.org/browse/JRUBY-5529 - jruby-openssl in --1.9 jruby mode client.http.auth.ssl.verify_mode=(:none) resp = client.request :com, :get_message_limit do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} end resp.to_hash[:get_message_limit_response][:get_message_limit_result].to_i end def get_status(*message_id) connect unless @client if message_id.size > 1 message_id.each do |id| resp = @client.request :com, :get_message_status do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} soap.body = {"com:id" => id.to_s} end @sent_message_statuses << resp.to_hash[:get_message_status_response][:get_message_status_result] end else resp = @client.request :com, :get_message_status do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} soap.body = {"com:id" => message_id.first} end resp.to_hash[:get_message_status_response][:get_message_status_result] end end private def connect @client = Savon::Client.new(SEND_SERVICE_WSDL) #http://jira.codehaus.org/browse/JRUBY-5529 - jruby-openssl in --1.9 jruby mode @client.http.auth.ssl.verify_mode=(:none) end def sms(recipient, messages, originator, validityperiod) @log.debug "#{@message_kind}:#{messages.size} parts." connect messages.each do |message| @log.debug "#{@message_kind}:#{message.size} bytes. Sending..." resp = @client.request :com, :send_message_full do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} soap.body = {"com:recipient" => recipient, "com:body" => message, "com:type" => @message_kind, "com:originator" => originator, "com:validityperiod" => validityperiod} end @log.debug "Sent #{@message_kind}:#{recipient}:#{message.size} bytes" @sent_message_ids << resp.to_hash[:send_message_full_response][:send_message_full_result] end @log.debug "-=| #{messages.join(' + ')} |=-" @log.debug "-----" end end #module Client end #module Esendex #sms = Esendex::Client.new 'username', 'password', 'account_reference', 'from', 'expire int hours' #sms.send_message 'PHONENUMBER', 'message in any language'