Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Created November 3, 2012 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswitt/4006850 to your computer and use it in GitHub Desktop.
Save thomaswitt/4006850 to your computer and use it in GitHub Desktop.
How to send an SMS via Sipgate API and Ruby
require 'xmlrpc/client'
class SMS
def self.deliver(phonenumber, text)
user = Rack::Utils.escape(SIPGATE_USER_NAME)
url = "https://#{user}:#{SIPGATE_PASSWORD}@api.sipgate.net/RPC2"
client = XMLRPC::Client.new2(url)
client.call('samurai.ClientIdentify', {'ClientName' => 'Ruby-Client'} )
number = strip_phonenumber(phonenumber)
args = {
'RemoteUri' => "sip:#{number}@sipgate.net",
'TOS' => 'text',
'Content' => text[0,159]
}
if r = client.call('samurai.SessionInitiate', args)
Rails::logger.info("*** SMS sent to #{number} (#{r.inspect})")
Rails::logger.info("*** SMS text: #{text}") if Rails.env.development?
else
raise "sms send failed: #{r}"
end
end
private
def self.strip_phonenumber(number)
number.gsub!(/\D/, '')
number.gsub!(/^01/, '491')
number.gsub!(/^00491/, '491')
number
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment