stevenbeales
12/29/2018 - 6:36 PM

Twilio Sinatra

# Need the Sinatra Library to make a web app!
require 'sinatra'

# This is the URL that can be used for the Voice URL on your Twilio Number
post '/messages' do
  content_type 'text/xml'
  "<Response>
    <Message>This is an SMS reply.</Message>
  </Response>"
end

# This is the URL that can be used for the Voice URL on your Twilio Number
post '/voice' do
  content_type 'text/xml'
  "<Response>
    <Say>This is a text-to-speach block.</Say>
    <Play>http://cdn.wawra.co/sw.mp3</Play>
  </Response>"
end
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