Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Created August 17, 2011 08:51
Show Gist options
  • Save bjhaid/1151120 to your computer and use it in GitHub Desktop.
Save bjhaid/1151120 to your computer and use it in GitHub Desktop.
switchvox.yml
default:
switchvox_ip: PBX.IP.Com
faye_ip: 0.0.0.0:9292
development:
switchvox_ip: PBX.IP.Com
faye_ip: 0.0.0.0:9292
production:
switchvox_ip: PBX.IP.Com
faye_ip: 0.0.0.0:9292
switchvox.rb
yaml_config = YAML.load_file("#{RAILS_ROOT}/config/switchvox.yml") || {}
CONFIG = (yaml_config['default'] || {}).symbolize_keys.merge((yaml_config[RAILS_ENV] || {}).symbolize_keys)
incomings_controller
require 'switchvox'
class IncomingsController < ApplicationController
def index
@incoming = Incoming.all
end
#the new method creates a record of the calls in database, queries sugarCRM for records matching the caller's phone number and publishes the data via the messaging server(faye)
def new
@incoming = Incoming.create({:exten_number => params[:exten_number], :cid_number => params[:cid_number]})
if (params[:cid_number])[0] == "8"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
elsif (params[:cid_number])[0] == "7"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
elsif (params[:cid_number])[0] == "1"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
end
if @incoming.update_attributes(params[:incoming])
require 'eventmachine'
#the IP address for the machine hosting the faye messaging server should be passed here
faye = Faye::Client.new('http://' + "#{CONFIG.faye_ip}" + '/faye')
unless @contact.nil?
msg = JSON.dump('channel' => "/incomings", 'data' => {'exten_id' => "#{@incoming.exten_number}", 'caller_id' => "#{@contact.id}", 'caller_name' => "#{@contact.account_name.split(' ').map {|w| w.capitalize }.join(' ')}", 'caller_number' => "#{params[:cid_number]}" })
uri = URI.parse('http://' + "#{CONFIG.faye_ip}" + '/faye')
Net::HTTP.post_form(uri, :message => msg)
#EM.run {
#faye.publish("/incomings", {'exten_id' => "#{@incoming.exten_number}", 'caller_id' => "#{@contact.id}", 'caller_name' => "#{@contact.account_name.capitalize}", 'caller_number' => "#{params[:cid_number]}" })
#}
else
msg = JSON.dump('channel' => "/incomings", 'data' => {"exten_id" => "#{@incoming.exten_number}", "caller_number" => "#{params[:cid_number]}", "caller_name" => "no details exists for this user"})
uri = URI.parse('http://' + "#{CONFIG.faye_ip}" + '/faye')
Net::HTTP.post_form(uri, :message => msg)
#EM.run {
#faye.publish("/incomings", {"exten_id" => "#{@incoming.exten_number}", "caller_number" => "#{params[:cid_number]}", "caller_name" => "no details exists for this user"})
#}
end
# end
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @incoming }
end
end
def create
@incoming = Incoming.new(params[:incoming])
end
# GET /incomings/1
# GET /incomings/1.xml
def call
Switchvox_call.calling(params[:dial_number])
end
def update
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment