Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Last active February 4, 2018 16:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save danielwestendorf/4962409 to your computer and use it in GitHub Desktop.
Need to be notified of something immediately via telephone? Twilio/AWS/Ruby mashup to do just that
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment