Skip to content

Instantly share code, notes, and snippets.

@adambard
Created October 15, 2012 01:12
Show Gist options
  • Save adambard/3890353 to your computer and use it in GitHub Desktop.
Save adambard/3890353 to your computer and use it in GitHub Desktop.
Voicemail-by-email with Twilio
require 'rubygems'
require 'twilio-ruby'
require 'sinatra'
require 'pony'
require 'open-uri'
# Config
MY_NUMBER = "+19995551234"
TO_EMAIL = "voicemail@adambard.com"
SMTP_HOST = "<SMTP hostname here>"
SMTP_USER = "<your username here>"
SMTP_PASSWORD = "<your password here>"
# Send a message with an mp3 voicemail attachment
def send_message(subject, message, file_url)
Pony.mail({
:to => TO_EMAIL
:body => message,
:via => :smtp,
:via_options => {
:address => SMTP_HOST,
:user_name => SMTP_USER,
:password => SMTP_PASSWORD,
:port => SMTP_PORT,
:authentication => :plain },
:attachments => {"message.mp3" => open(file_url) {|f| f.read }}
})
end
# Answer the call and try to call MY_NUMBER
get '/answer' do
Twilio::TwiML::Response.new do |r|
r.Say "Thank you for calling. Please wait while we attempt to connect you."
r.Dial :timeout => "10", :action => "/record-or-hangup", :method => "get" do |d|
r.Number MY_NUMBER
end
end.text
end
# If DialCallStatus is "completed", just hang up. Otherwise, record a message
get '/record-or-hangup' do
Twilio::TwiML::Response.new do |r|
if params['DialCallStatus'] == 'completed'
r.Hangup
else
r.Say "Sorry, looks like nobody's around. Please leave a message."
r.Record :action => "/save-recording", :method => "get"
end
end.text
end
# Email the message, then thank the caller and hang up
get '/save-recording' do
send_message("New Message on the hotline",
"Message length: #{params['RecordingDuration']}",
params['RecordingUrl'] + '.mp3')
Twilio::TwiML::Response.new do |r|
r.Say "Thank you for calling. Bye!"
r.Hangup
end.text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment