require 'twilio-ruby'
client = Twilio::REST::Client.new "YOUR TWILIO SID", "YOUR TWILIO TOKEN"
# Get a list of messages sent to a given number...
messages = client.account.messages.list to: "YOU TWILIO NUMBER"
# Now print out each message
messages.each do |msg|
puts "#{msg.from}: #{msg.body}"
end
# Get all of the phone numbers:
numbers = messages.collect do |msg|
msg.from
end
# For each number call back using the '/voice' action in the sinatra application.
numbers.each do |num|
client.account.calls.create(to: num, from: "YOU TWILIO NUMBER", body: "http://server-with-sinatra-app/voice"
end