Skip to content

Instantly share code, notes, and snippets.

@ajesler
Last active December 20, 2015 22:09
Show Gist options
  • Save ajesler/6202509 to your computer and use it in GitHub Desktop.
Save ajesler/6202509 to your computer and use it in GitHub Desktop.
SoT Demo: Consumer for SQS and sends message via Twilio
require 'rubygems'
require 'aws-sdk'
require 'twilio-ruby'
account_sid = ENV['twilio_account_sid']
auth_token = ENV['twilio_auth_token']
client = Twilio::REST::Client.new account_sid, auth_token
from = ENV['twilo_from_number']
sqs = AWS::SQS.new( :access_key_id => ENV['aws_access_key'], :secret_access_key => ENV['aws_secret_key'])
q = sqs.queues.named(ENV['sqs_queue_name'])
sent_to = []
puts "Listening for messages..."
q.poll do |msg|
puts "Got message: #{msg.body}"
msg_parts = msg.body.split(':')
number = msg_parts[0]
name = msg_parts[1]
if number =~ /\+\d{7,}/
if !sent_to.include? number
sent_to.push number
client.account.sms.messages.create(
:from => from,
:to => number,
:body => "Congratulations #{name}, you have deployed an application to the cloud!"
)
puts "\tSending message to #{number}"
else
puts "\tNot sending to #{number} as it has already been sent to."
end
else
puts "\tNot a number: #{number}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment