Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'sinatra' # Get with "gem install sinatra"
- require 'restful_adhearsion'
- require 'pp'
- #Create our Adhearsion object connected to the RESTful API of Adhearsion
- Adhearsion = RESTfulAdhearsion.new(:host => "localhost",
- :port => 5000,
- :user => "jicksta",
- :password => "roflcopterz")
- # You'll need to change this for your own format.
- # Note: this will soon be handled by the Call Routing DSL in Adhearsion.
- def format_number(number)
- if number.length == 10
- return "SIP/tsgglobal/1#{number}"
- elsif number.length == 11
- return "SIP/tsgglobal/#{number}"
- else
- return "SIP/elastix-trunk/#{number}"
- end
- end
- def call_with_source(source)
- #ahn_log.click_to_call "Finding call with source #{source.inspect} in #{Adhearsion.active_calls.size} active calls"
- retVal = Hash.new
- Adhearsion.active_calls.to_a.find do |call|
- if call.variables[:sourcedid].include? source
- if (defined? call.variables[:msg_filename] && call.variables[:msg_filename] != nil)
- retVal[:msg_filename] = call.variables[:msg_filename]
- retVal[:result] = "alive"
- end
- else
- retVal[:msg_filename] = nil
- retVal[:result] = "dead"
- end
- return retVal
- end
- end
- post "/call" do
- options = { :channel => format_number(params[:source]),
- :context => "adhearsion",
- :priority => "1",
- :callerid => "8664680900",
- :exten => "1000",
- :async => 'true',
- :variable => "destination=#{format_number(params[:destination])},sourcedid=#{params[:source]}",
- }
- #Originates a call over the AMI interface
- Adhearsion.originate(options)
- #puts options.to_json
- "ok".to_json
- end
- post "/hangup" do
- #Get the channel of the active call, then hang it up
- channel_of_active_call = Adhearsion.hangup_channel_with_destination params[:call_to_hangup]
- "ok".to_json
- end
- get '/status' do
- # The line below will return either {result:"alive"} or {result:"dead"} to the browser
- Adhearsion.call_with_source(params[:source]).to_json
- #{:result => Adhearsion.call_with_source(params[:source])}.to_json
- end
- get '/' do
- #Build our web form that has the JQuery sprinkled in
- <<-HTML
- <html><head>
- <title>Record a greeting</title>
- <script src="jquery.js" type="text/javascript"></script>
- <script src="call.js" type="text/javascript"></script>
- <link href="style.css" media="screen" rel="stylesheet" type="text/css" />
- </head><body>
- <div id="content">
- <h1>Record a greeting</h1>
- <div id="call-form">
- <p><label for="source">Your number: </label><input type="text" id="source" name="source"/></p>
- <p><button onclick="new AhnCall($('#call'), $('#source').val(), $('#destination').val())">Start call</button></p>
- <input type="hidden" name="destination" value="1" />
- </div>
- </div>
- <div id="call" class="hidden">
- <p>Starting...</p>
- </div>
- </body></html>
- HTML
- end
Add Comment
Please, Sign In to add comment