Skip to content

Instantly share code, notes, and snippets.

@peterc
Created January 17, 2010 03:37
Show Gist options
  • Save peterc/279185 to your computer and use it in GitHub Desktop.
Save peterc/279185 to your computer and use it in GitHub Desktop.
Scrappy but very simple (and working!) Ruby + Twilio public voicemail system example - live demo at http://voicemail.coder.io/
# Very simple, but working, voice message system for Twilio in Ruby
# by Peter Cooper - http://twitter.com/peterc
#
# SEE A LIVE DEMO AT http://voicemail.coder.io/
#
# You can call the Twilio number, leave a message, as well as see the
# messages that are left.
#
# To set up on a server, just have this as the config.ru (yes, scrappy)
# on a Rack compliant server. Works great with Passenger..
#
# In Twilio, just set the root as the endpoint for your app.
# Access the root in a Web browser to see the voicemails.
require 'rubygems'
require 'sinatra'
require 'twiliolib'
require 'nokogiri'
class BasicVoicemail < Sinatra::Application
API_VERSION = '2008-08-01'
ACCOUNT_SID = 'REPLACE WITH YOUR OWN'
ACCOUNT_TOKEN = 'REPLACE WITH YOUR OWN'
post '/' do
@r = Twilio::Response.new
@r.addSay("Please leave a message. Include your name and press any digit to end. Maximum length is three minutes.", :voice => "woman")
@r.addRecord(:action => '/phone/recorded', :method => 'POST', :timeout => '4', :transcribe => 'false', :maxLength => 180)
@r.addSay("You did not leave a message. Stop wasting my time and piss off.")
@r.respond
end
post '/recorded' do
@r = Twilio::Response.new
@r.addSay("Thank you. Goodbyte!", :voice => "woman")
@r.respond
end
get '/' do
account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
resp = account.request("/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/Recordings", 'GET')
fail unless resp.is_a?(Net::HTTPSuccess)
@voicemails = []
Nokogiri::XML(resp.body).css('Recording').each do |recording|
@voicemails << { :sid => recording.at('Sid').inner_text, :created_at => recording.at('DateCreated').inner_text }
end
erb %{<h1>Voicemails received</h1><ul>
<% @voicemails.each do |voicemail| %>
<li><a href="https://api.twilio.com/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/Recordings/<%= voicemail[:sid] %>.mp3"><%= voicemail[:created_at] %></li>
<% end %>
</ul>}
end
end
run BasicVoicemail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment