Created
November 3, 2012 09:50
-
-
Save thomaswitt/4006850 to your computer and use it in GitHub Desktop.
How to send an SMS via Sipgate API and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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