# -*- encoding : utf-8 -*- require 'savon' require 'log4r' #require 'pry' require 'api_smith' module LanguageTools class DetectLanguageDotCom include APISmith::Client base_uri "http://ws.detectlanguage.com" endpoint "0.2" def initialize(api_key) @api_key = api_key add_query_options! :key => @api_key end def detect(text) (post "detect", :extra_query => {:q => text})["data"]["detections"].first["language"] end end class AlchemyLanguage include APISmith::Client base_uri "http://access.alchemyapi.com/" endpoint "calls/text" def initialize(api_key) @api_key = api_key add_query_options! :outputMode => 'json', :apikey => @api_key end def detect(text) (post "TextGetLanguage", :extra_query => {:text => text})["language"] end end end 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 # @detectlanguage_api_key, @alchemyapi_api_key = args.shift, args.shift @sent_message_ids, @sent_message_statuses = Array.new, Hash.new @language = LanguageTools::DetectLanguageDotCom.new(args.shift) 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 #{@language.detect(text)} language" #TODO: handle other languages as well unless @language.detect(text) == 'ru' sms_parts = text.scan(/.{1,160}/mu) @log.debug "#{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(/.{1,70}/mu) @log.debug "#{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_inbox_messages client = Savon::Client.new(INBOX_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_messages do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} end resp[:get_messages_response][:get_messages_result][:message] 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_s end def get_status(*message_id) connect unless @client message_id.flatten! message_id.each do |id| @log.debug "#{id.to_s}" 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[id] = 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', 'api_key for detectlanguage' #sms.send_message 'PHONENUMBER', 'message in any language', 'FROM', 'expire int hours'