public
Last active

Need to be notified of something immediately via telephone? Twilio/AWS/Ruby mashup to do just that

  • Download Gist
notify.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'twilio-ruby'
require 'aws'
 
# Creates a phone call using the Twilio and AWS SDK's that uses text-to-speech to play a message
# Expected to be called from the command line with the respective arguments
# Creates an XML document that is uploaded to S3. This XML document tells Twilio what to do once the call is connected
 
# pass your Twilio creds via environmental variables
twilio_sid = ENV['TWILIO_SID']
twilio_token = ENV['TWILIO_TOKEN']
 
# pass your AWS information via environemental variables
aws_key_id = ENV['AWS_KEY_ID']
aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
aws_bucket = ENV['AWS_BUCKET'] # this bucket should already exist
# you might set the lifecycle on this bucket to delete files after a day or so
 
recipient = ARGV[0] #+15555551234
sender = ARGV[1] #+15555551234
message = ARGV[2] #"THIS IS MY MESSAGE"
 
# create the TwiML
response = Twilio::TwiML::Response.new do |r|
r.Say message, voice: 'woman', loop: 0
end
 
# upload the TwiML to S3
filename = Time.now.to_i.to_s + '.xml'
s3 = AWS::S3.new(access_key_id: aws_key_id, secret_access_key: aws_secret_access_key)
bucket = s3.buckets[aws_bucket]
file = bucket.objects[filename].write(response.text, content_type: 'application/xml')
 
# create a URL for the file that Twilio can access
url = file.url_for(:read, expires: 300, secure: false).to_s
 
# initiate the call passing the url of the XML document
client = Twilio::REST::Client.new twilio_sid, twilio_token
client.account.calls.create(url: url, to: recipient, from: sender, method: "GET", if_machine: 'Continue')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.