Skip to content

Instantly share code, notes, and snippets.

@johnnygoodman
Created August 14, 2012 18:46
Show Gist options
  • Save johnnygoodman/3351630 to your computer and use it in GitHub Desktop.
Save johnnygoodman/3351630 to your computer and use it in GitHub Desktop.
# Resource called by the Tropo WebAPI URL setting
post '/index.json' do
# Fetches the HTTP Body (the session) of the POST and parse it into a native Ruby Hash object
v = Tropo::Generator.parse request.env["rack.input"].read
# Fetching certain variables from the resulting Ruby Hash of the session details
# into Sinatra/HTTP sessions; this can then be used in the subsequent calls to the
# Sinatra application
session[:from] = v[:session][:from]
session[:network] = v[:session][:to][:network]
session[:channel] = v[:session][:to][:channel]
# Create a Tropo::Generator object which is used to build the resulting JSON response
t = Tropo::Generator.new
puts "inspect v[:session]: #{v[:session].inspect}"
if v[:session][:initial_text] #if so, this is not a voice session.
v[:session][:initial_text].match(/([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}))/i)
email = $1
if email
e = Email.new(:email => email, :full_text => v[:session][:initial_text], :timestamp => Time.now, :good_email => true, :phone => v[:session][:from][:id])
e.save
#tell the user we've gotten their email
t.message({
:to => v[:session][:from][:id], #'2818310750', #v[:result][:actions][:number_to_text][:value],
:network => "SMS",
:say => {:value => "Thanks! We will contact you at #{email} shortly."}})
jid = JID::new(ENV['JOHNNYBOT_USER'])
password = ENV['JOHNNYBOT_PASS']
cl = Client::new(jid)
cl.connect
cl.auth(password)
to = "johnny.goodman@subdomain.cpap.com"
subject = "Valid email received"
body = "Valid email received #{v[:session][:initial_text]} #{v[:session][:from][:id]}"
m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)
cl.send m
else
e = Email.new(:full_text => v[:session][:initial_text], :timestamp => Time.now, :good_email => false, :phone => v[:session][:from][:id])
e.save
#tell the user we don't know what their email is
t.message({
:to => v[:session][:from][:id], #'2818310750', #v[:result][:actions][:number_to_text][:value],
:network => "SMS",
:say => {:value => "Sorry, but something went wrong. Please text us only your email address."}})
jid = JID::new(ENV['JOHNNYBOT_USER'])
password = ENV['JOHNNYBOT_PASS']
cl = Client::new(jid)
cl.connect
cl.auth(password)
to = "johnny.goodman@subdomain.cpap.com"
subject = "Invalid email received"
body = "Invalid email received #{v[:session][:initial_text]} #{v[:session][:from][:id]}"
m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)
cl.send m
end
else
#this is a voice call, forward to the CPAP.com phones
t.transfer "+18003565221"
end
# Return the JSON response via HTTP to Tropo
t.response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment