Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created October 9, 2013 22:14
Show Gist options
  • Save JakubOboza/6909427 to your computer and use it in GitHub Desktop.
Save JakubOboza/6909427 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'amqp'
require 'json'
require 'securerandom'
# Example HTTP response, for AMPQ we need to use single even in outgoing
# {
# "events":[{
# "event":"send",
# "messages":[{
# "id":"4f7c9cea5e11b",
# "to":"6507993371",
# "message":"hello world'"
# }]
# }]
# }
# http://sms.envaya.org/serverapi/#events
HOST = ARGV[0] || "127.0.0.1"
QUEUE = ARGV[1] || "sms.gateway"
def message(to, message)
{
"event" => "send",
"messages" => [{
"id": SecureRandom.uuid,
"to": to,
"message": message
}]
}.to_json
end
EventMachine.run do
connection = AMQP.connect(:host => HOST)
puts "Connected"
ch = AMQP::Channel.new(connection)
q = ch.queue(QUEUE, :auto_delete => true)
x = ch.default_exchange
q.subscribe do |metadata, payload|
puts "Got message #{payload}"
# connection.close {
# EventMachine.stop { exit }
# }
end
x.publish message("+447429150645", "Hello Jakub"), :routing_key => q.name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment