Permalink
Browse files

Twilio integration

  • Loading branch information...
1 parent fd1d7d2 commit 638b4a151b953a93bfc2940a6830750addfc5cea Jiren committed Aug 25, 2010
View
15 config/vendors.yml
@@ -13,10 +13,13 @@ bulksmspune:
clickatell:
provider_url: 'http://api.clickatell.com'
- api_key: 3255022
- username: 'jirenpatel'
- password: 'jp12345'
- #api_key: 3255051 # 3255022
- #username: 'gautamrege' #'jirenpatel'
- #password: 'grege123' #'jp12345'
+ api_key:
+ username:
+ password:
+twilio:
+ provider_url: 'https://api.twilio.com'
+ account_sid:
+ auth_token:
+ api_version: '2008-08-01'
+ phone_number:
View
76 lib/splat/vendors/twilio/twilio_gateway.rb
@@ -0,0 +1,76 @@
+
+require 'net/https'
+require File.dirname(__FILE__) + '/xml/twilio_xml'
+
+module Splat
+
+ class Twilio < Gateway
+
+ def initialize()
+ super
+ self.required_config([:provider_url, :account_sid, :auth_token])
+ @service_url = self.config_option(:provider_url)
+ @account_sid = self.config_option(:account_sid)
+ @auth_token = self.config_option(:auth_token)
+ @api_version = self.config_option(:api_version) || '2008-08-01'
+ @phone_number = self.config_option(:phone_number)
+
+ @send_service_url = URI.parse("#{@service_url}/#{@api_version}/Accounts/#{@account_sid}/SMS/Messages")
+
+ #if options[:callbacks]
+ # before_send = options[:callback][:before_send]
+ #end
+ end
+
+ def send_sms(message, number, options = {})
+ options[:response].message = call_service(message, number)
+ options[:response]
+ end
+
+ def send_bulk_sms(message, numbers, options = {})
+ numbers.each do |number|
+ options[:response].add(number, call_service(message, number))
+ end
+ options[:response]
+ end
+
+ def send_bulk_sms_with_insertion(message, insertions = {}, options = {})
+
+ number_message_map = insert_values(message, insertions)
+ number_message_map.each do |number, message|
+ options[:response].add(number, call_service(message, parse_number(number)))
+ end
+ options[:response]
+ end
+
+ private
+
+ def call_service(message, number)
+
+ http = Net::HTTP.new(@send_service_url.host, @send_service_url.port)
+ http.use_ssl = true
+
+ request = Net::HTTP::Post.new(@send_service_url.request_uri)
+ request.basic_auth(@account_sid, @auth_token)
+ request.set_form_data({'To' => parse_number(number),
+ 'From' => @phone_number,
+ 'Body' => message })
+
+ response = http.request(request)
+ xml_obj = XSD::Mapping.xml2obj(response.body)
+
+ if xml_obj.RestException
+ return xml_obj.RestException.message
+ end
+
+ xml_obj.SMSMessage.status
+
+ end
+
+ def parse_number(number)
+ number.tr(' ', '')
+ end
+
+ end
+
+end
View
35 lib/splat/vendors/twilio/xml/twilio.xsd
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+ <xs:complexType name="RestException">
+ <xs:sequence>
+ <xs:element name="Status" type="xs:string"/>
+ <xs:element name="Message" type="xs:string"/>
+ <xs:element name="Code" type="xs:string"/>
+ <xs:element name="MoreInfo" type="xs:string"/>
+ </xs:sequence>
+ </xs:complexType>
+
+ <xs:complexType name="SMSMessage">
+ <xs:sequence>
+ <xs:element name="Sid" type="xs:string"/>
+ <xs:element name="DateCreated" type="xs:string"/>
+ <xs:element name="DateUpdated" type="xs:string"/>
+ <xs:element name="DateSent" type="xs:string"/>
+ <xs:element name="AccountSid" type="xs:string"/>
+ <xs:element name="To" type="xs:string"/>
+ <xs:element name="From" type="xs:string"/>
+ <xs:element name="Body" type="xs:string"/>
+ <xs:element name="Status" type="xs:string"/>
+ <xs:element name="Flags" type="xs:string"/>
+ </xs:sequence>
+ </xs:complexType>
+
+ <xs:complexType name="TwilioResponse">
+ <xs:sequence>
+ <xs:element name="RestException" type="RestException" />
+ <xs:element name="SMSMessage" type="SMSMessage" />
+ </xs:sequence>
+ </xs:complexType>
+
+</xs:schema>
View
175 lib/splat/vendors/twilio/xml/twilio_xml.rb
@@ -0,0 +1,175 @@
+require 'xsd/qname'
+
+# {}RestException
+class RestException
+ @@schema_type = "RestException"
+ @@schema_ns = nil
+ @@schema_element = [["status", ["SOAP::SOAPString", XSD::QName.new(nil, "Status")]], ["message", ["SOAP::SOAPString", XSD::QName.new(nil, "Message")]], ["code", ["SOAP::SOAPString", XSD::QName.new(nil, "Code")]], ["moreInfo", ["SOAP::SOAPString", XSD::QName.new(nil, "MoreInfo")]]]
+
+ def Status
+ @status
+ end
+
+ def Status=(value)
+ @status = value
+ end
+
+ def Message
+ @message
+ end
+
+ def Message=(value)
+ @message = value
+ end
+
+ def Code
+ @code
+ end
+
+ def Code=(value)
+ @code = value
+ end
+
+ def MoreInfo
+ @moreInfo
+ end
+
+ def MoreInfo=(value)
+ @moreInfo = value
+ end
+
+ def initialize(status = nil, message = nil, code = nil, moreInfo = nil)
+ @status = status
+ @message = message
+ @code = code
+ @moreInfo = moreInfo
+ end
+end
+
+# {}SMSMessage
+class SMSMessage
+ @@schema_type = "SMSMessage"
+ @@schema_ns = nil
+ @@schema_element = [["sid", ["SOAP::SOAPString", XSD::QName.new(nil, "Sid")]], ["dateCreated", ["SOAP::SOAPString", XSD::QName.new(nil, "DateCreated")]], ["dateUpdated", ["SOAP::SOAPString", XSD::QName.new(nil, "DateUpdated")]], ["dateSent", ["SOAP::SOAPString", XSD::QName.new(nil, "DateSent")]], ["accountSid", ["SOAP::SOAPString", XSD::QName.new(nil, "AccountSid")]], ["to", ["SOAP::SOAPString", XSD::QName.new(nil, "To")]], ["from", ["SOAP::SOAPString", XSD::QName.new(nil, "From")]], ["body", ["SOAP::SOAPString", XSD::QName.new(nil, "Body")]], ["status", ["SOAP::SOAPString", XSD::QName.new(nil, "Status")]], ["flags", ["SOAP::SOAPString", XSD::QName.new(nil, "Flags")]]]
+
+ def Sid
+ @sid
+ end
+
+ def Sid=(value)
+ @sid = value
+ end
+
+ def DateCreated
+ @dateCreated
+ end
+
+ def DateCreated=(value)
+ @dateCreated = value
+ end
+
+ def DateUpdated
+ @dateUpdated
+ end
+
+ def DateUpdated=(value)
+ @dateUpdated = value
+ end
+
+ def DateSent
+ @dateSent
+ end
+
+ def DateSent=(value)
+ @dateSent = value
+ end
+
+ def AccountSid
+ @accountSid
+ end
+
+ def AccountSid=(value)
+ @accountSid = value
+ end
+
+ def To
+ @to
+ end
+
+ def To=(value)
+ @to = value
+ end
+
+ def From
+ @from
+ end
+
+ def From=(value)
+ @from = value
+ end
+
+ def Body
+ @body
+ end
+
+ def Body=(value)
+ @body = value
+ end
+
+ def Status
+ @status
+ end
+
+ def Status=(value)
+ @status = value
+ end
+
+ def Flags
+ @flags
+ end
+
+ def Flags=(value)
+ @flags = value
+ end
+
+ def initialize(sid = nil, dateCreated = nil, dateUpdated = nil, dateSent = nil, accountSid = nil, to = nil, from = nil, body = nil, status = nil, flags = nil)
+ @sid = sid
+ @dateCreated = dateCreated
+ @dateUpdated = dateUpdated
+ @dateSent = dateSent
+ @accountSid = accountSid
+ @to = to
+ @from = from
+ @body = body
+ @status = status
+ @flags = flags
+ end
+end
+
+# {}TwilioResponse
+class TwilioResponse
+ @@schema_type = "TwilioResponse"
+ @@schema_ns = nil
+ @@schema_element = [["restException", ["RestException", XSD::QName.new(nil, "RestException")]], ["sMSMessage", ["SMSMessage", XSD::QName.new(nil, "SMSMessage")]]]
+
+ def RestException
+ @restException
+ end
+
+ def RestException=(value)
+ @restException = value
+ end
+
+ def SMSMessage
+ @sMSMessage
+ end
+
+ def SMSMessage=(value)
+ @sMSMessage = value
+ end
+
+ def initialize(restException = nil, sMSMessage = nil)
+ @restException = restException
+ @sMSMessage = sMSMessage
+ end
+end
View
8 splat.gemspec
@@ -5,10 +5,10 @@
Gem::Specification.new do |s|
s.name = %q{josh-splat}
- s.version = "0.0.2"
+ s.version = "0.0.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Gautam Rege"]
+ s.authors = ["Gautam Rege", "Jiren Patel", "Sethupathi Asokan"]
s.date = %q{2010-07-13}
s.description = %q{
SPlat is an integration platform to make use of SMS integration really easy. Using this platform has the following advantages:
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
s.email = %q{info@joshsoftware.com}
s.extra_rdoc_files = [
"LICENSE",
- "README.rdoc"
+ "README.rdoc"
]
s.files = [
".document",
@@ -62,7 +62,7 @@ Gem::Specification.new do |s|
"lib/splat.rb",
"test.rb"
]
- s.homepage = %q{http://github.com/gautamrege/splat}
+ s.homepage = %q{http://github.com/joshsoftware/Splat}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
View
17 test.rb
@@ -2,17 +2,16 @@
require 'rubygems'
require 'lib/splat'
-number = '+91 9850888082'
gateways = {}
#gateways['bulksms'] = Splat::Base.new(:bulksmspune)
#gateways['vmobo'] = Splat::Base.new(:vmobo)
-gateways['clickatell'] = Splat::Base.new(:clickatell)
+#gateways['clickatell'] = Splat::Base.new(:clickatell)
+gateways['twilio'] = Splat::Base.new(:twilio)
-=begin
#number = '+1 5599726538'
-number = '+91 9881395656'
+number = '+91 9960054954'
msg = "Testing for splat"
gateways.each do |provider, obj|
p "Sending SMS via #{provider}"
@@ -21,6 +20,7 @@
end
p "========================"
+=begin
msg = "Testing for splat"
numbers = ['+91 9960054954', '+91 9850888082']
gateways.each do |provider, obj|
@@ -29,16 +29,17 @@
end
p "========================"
-=end
-custom_msg = "Hi $1, hold on to your $2."
-options = {'+91 9960054954' => ['Sagar', 'coffee'],
- '+91 9850888082' => ['Gautam' , 'stocks']}
+
+custom_msg = "Hi, $1, hold on to your $2."
+options = {'+91 9960054954' => ['Sagar', 'coffee']}
+ #'+91 9850888082' => ['Gautam' , 'stocks']}
#'+91 9880397111' => ['Jane' , 'skirt']}
gateways.each do |provider, obj|
p obj.send_bulk_sms_with_insertion(custom_msg, options)
p "Bulk Send successfully via #{provider}"
end
+=end

0 comments on commit 638b4a1

Please sign in to comment.