Created
June 13, 2013 21:25
-
-
Save yuric/5777506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Calllogic | |
#Start here!!! | |
# Now I need to figure out the counter and validation for brasil numbers. (63) 9979-3040 | |
# Decided to do the counter increment on each individual model for now | |
def calllogic_entrypoint(msg, number, country, language, obj=nil) | |
puts "OBJ: #{obj.class}\n" | |
if obj == nil || obj.class.name == "Themagic" | |
if country == 'Brasil' | |
#Send through Tropo | |
puts "Send Brasil" | |
return send_via_tropo_call(msg, number, country, 55, language) | |
elsif country == 'United States' | |
puts "Send USA tropo" | |
return send_via_tropo_call(msg, number, country, 1, language) | |
else | |
#unknown country, log error | |
puts "Send USA twilio" | |
return send_via_tropo_call("Error Call Logic:Country:#{country}, msg:#{msg}", ENV['ADMIN_PHONE_NUMBER'], 'United States', 1, 1) | |
end | |
else | |
send_via_twilio_call(msg, number, obj) | |
end | |
end | |
def send_via_tropo_call(msg, number, country, country_code, language, multi_language=false) | |
msg = URI.escape("#{msg}") | |
if language == 0 | |
@voice = "Allison"#english | |
elsif language == 1 #spanish | |
@voice = "Esperanza" | |
elsif language == 2#portuguese | |
@voice = "Gabriela" | |
elsif language == 3#french | |
@voice = "Juliette" | |
elsif language == 4#russian | |
@voice = "Olga" | |
else | |
@voice = "Allison"#english | |
end | |
#Enviei uma mensagem test para o Thompson. 21 9520 1780 VIVO?? | |
#Enviei uma mensagem test para o Rodrigo TBM 21 7991 8990 TIM | |
# | |
if multi_language == true | |
url = "https://api.tropo.com/1.0/sessions?action=create&token=#{ENV['TROPO_TOKEN']}&numberToDial=#{country_code}#{number}&customerName=Parents&msg=#{msg}&module=calllogic&callerID=15416474904" | |
else | |
url = "https://api.tropo.com/1.0/sessions?action=create&token=#{ENV['TROPO_TOKEN']}&numberToDial=#{country_code}#{number}&customerName=Parents&msg=#{msg}&module=calllogic&voice=#{@voice}&callerID=15416474904" | |
end | |
puts url | |
puts msg | |
response = HTTParty.get(url) | |
# puts response.body, response.code, response.message#, response.headers.inspect | |
rsp = Hash.from_xml(response.body) | |
# puts rsp | |
if rsp["session"]["success"] && rsp["session"]["success"] == "true" | |
return rsp | |
elsif rsp["session"]["success"] && rsp["session"]["success"] == "false" | |
#Right now I just sms myself. Will keep this TODO on top of mind. | |
error = "Failed via Tropo. Reason:" + rsp["session"]["reason"] | |
send_via_twilio_call(error) | |
return rsp | |
elsif !rsp["session"]["success"]#response does not exist | |
error = "Failed via Tropo. No known Reason" | |
send_via_twilio_call(error, ENV['ADMIN_PHONE_NUMBER']) | |
return rsp | |
else | |
return rsp | |
end | |
end | |
def send_via_twilio_call(msg, number, obj)#TODO: change to send_via_twilio | |
begin | |
url = "https://#{tan}:#{tap}@sparkroad.herokuapp.com/#{obj.class.name.pluralize.downcase}/#{obj.id}/say#{obj.class.name}.xml" | |
puts "URL call twily: #{url}" | |
# credentials | |
@account_sid = ENV['TWILIO_ACCOUNT_SID'] | |
@auth_token = ENV['TWILIO_AUTH_TOKEN'] | |
# set up a client to talk to the Twilio REST API | |
@client = Twilio::REST::Client.new(@account_sid, @auth_token) | |
#call | |
# make a new outgoing call | |
@call = @client.account.calls.create( | |
:from => '+18885510432', | |
:to => "+1" + number, | |
:url => url | |
) | |
puts @call | |
rescue StandardError => failedWith | |
puts "failed with #{failedWith}" | |
#Deliveryerror.create(:objecttype => 1, :deliverytype => 2, :objectid => announce.id, :errormessage => failedWith, :user_id => user.id).save | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Smslogic | |
include Sharedmethodz | |
#Start here!!! | |
#TODO: Now I need to figure out the counter and validation for brasil numbers. (63) 9979-3040 | |
# Decided to do the counter increment on each individual model for now | |
#TODO: PRIORITY 01: Need to split messages into small twilio consumable chunks (140 chars) see code in other models | |
def split_large_sms (message) | |
arraySMS = [] | |
if message.length > 150 | |
arraySMS = message.scan(/.{1,145}/) | |
elsif message.length < 150 && message.length > 20 | |
arraySMS[0] = message | |
else | |
arraySMS[0] = message | |
end | |
return arraySMS | |
end | |
def smslogic_entrypoint(msg, number, country, obj=nil) | |
arraySMS = split_large_sms(msg) | |
begin | |
arraySMS.length.times do |i| | |
sms = arraySMS[i]+"[#{i+1}/#{arraySMS.length}]" | |
if obj && obj.class.name == "Attendancesheet" | |
#send_via_twilio_sms_with_number(sms, user_number, number) | |
send_via_twilio_sms_with_number(sms, number,"7078095995") | |
else | |
#["United States", "Brasil", "France"] | |
if country == 'Brasil' | |
#Send through Tropo | |
puts "Send Brasil" | |
send_via_tropo_sms(msg, number, country, 55) | |
elsif country == 'United States' | |
puts "Send USA" | |
send_via_tropo_sms(msg, number, country, 1) | |
else | |
#unknown country, log error | |
send_via_tropo_sms("SMSLOGIC failed: [if]", ENV['ADMIN_PHONE_NUMBER'], 'United States', 1) | |
end#if | |
end#if | |
end#array.lenght.times | |
rescue | |
return send_via_tropo_sms("SMSLOGIC failed: [begin]", ENV['ADMIN_PHONE_NUMBER'], 'United States', 1) | |
end | |
end#smslogic | |
def send_via_tropo_sms(msg, number, country, country_code) | |
msg = URI.escape("#{msg}") | |
#Enviei uma mensagem test para o Thompson. 21 9520 1780 VIVO?? | |
#Enviei uma mensagem test para o Rodrigo TBM 21 7991 8990 TIM | |
# | |
url = "https://api.tropo.com/1.0/sessions?action=create&token=#{ENV['TROPO_TOKEN']}&numberToDial=#{country_code}#{number}&customerName=Parents&msg=#{msg}&module=smslogic" | |
response = HTTParty.get(url) | |
# puts response.body, response.code, response.message#, response.headers.inspect | |
rsp = Hash.from_xml(response.body) | |
# puts rsp | |
if rsp["session"]["success"] && rsp["session"]["success"] == "true" | |
return rsp | |
elsif rsp["session"]["success"] && rsp["session"]["success"] == "false" | |
#Right now I just sms myself. Will keep this TODO on top of mind. | |
error = "Failed via Tropo. Reason:" + rsp["session"]["reason"] | |
send_via_twilio_sms(error) | |
return rsp | |
elsif !rsp["session"]["success"]#response does not exist | |
error = "Failed via Tropo. No known Reason" | |
send_via_twilio_sms(error) | |
return rsp | |
else | |
return rsp | |
end | |
end | |
def send_via_twilio_sms(sms)#TODO: change to send_via_twilio | |
begin | |
# credentials | |
@account_sid = ENV['TWILIO_ACCOUNT_SID'] | |
@auth_token = ENV['TWILIO_AUTH_TOKEN'] | |
# set up a client to talk to the Twilio REST API | |
@client = Twilio::REST::Client.new(@account_sid, @auth_token) | |
#send text message | |
@client.account.sms.messages.create( | |
:from => '+19162359121', | |
:to => "+1" + ENV['ADMIN_PHONE_NUMBER'], | |
:body => sms | |
) | |
rescue StandardError => failedWith | |
#puts failedWith | |
#Deliveryerror.create(:objecttype => 1, :deliverytype => 2, :objectid => announce.id, :errormessage => failedWith, :user_id => user.id).save | |
end | |
end | |
def send_via_twilio_sms_with_number(sms, user_number, number) | |
begin | |
# credentials | |
@account_sid = ENV['TWILIO_ACCOUNT_SID'] | |
@auth_token = ENV['TWILIO_AUTH_TOKEN'] | |
# set up a client to talk to the Twilio REST API | |
@client = Twilio::REST::Client.new(@account_sid, @auth_token) | |
#send text message | |
@client.account.sms.messages.create( | |
:from => "+1" + number, | |
:to => "+1" + user_number, | |
:body => sms | |
) | |
rescue StandardError => failedWith | |
puts "\n#{failedWith}" | |
#Deliveryerror.create(:objecttype => 1, :deliverytype => 2, :objectid => announce.id, :errormessage => failedWith, :user_id => user.id).save | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment