Skip to content

Instantly share code, notes, and snippets.

@JustinAiken
Created October 21, 2012 22:16
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 JustinAiken/3928710 to your computer and use it in GitHub Desktop.
Save JustinAiken/3928710 to your computer and use it in GitHub Desktop.
SinAhnTra
require 'sinatra'
require 'json'
require 'ahn_api_helpers'
class AhnApi < Sinatra::Base
set :port, 8080
set :environment, ENV['AHN_ENV']
helpers Helpers
before { authenticate! }
get '/calls' do
{
active_calls_count: call_count,
active_calls: call_list
}.to_json
end
get '/teleclick' do
options = {
dial_me: params[:num2],
play_message: params[:play_message]
}
Adhearsion::OutboundCall.originate(
build_dial_string(params[:num1]),
from: '8558893939',
headers: options,
controller: TeleClickController
)
"Sent! Connecting #{params[:num1]} to #{params[:num2]}..."
end
end
module Helpers
def authenticate!
unless params[:api_key] == API_KEY && params[:api_secret] == API_SECRET
halt "Error Code 0x5FA7"
end
end
def call_count
Adhearsion::active_calls.count
end
def call_list
all_calls = Adhearsion::active_calls
if all_calls
call_list = []
all_calls.each do |call|
call_list << {
channel: call[1].variables[:x_agi_channel],
tracking_number: call[1].variables[:x_agi_dnid],
caller_id: call[1].variables[:x_agi_callerid],
disclaimer_played: call[1].controllers[0][:disclaimer_played],
message_played: call[1].controllers[0][:message_played],
recording_file_name: call[1].controllers[0][:recording_file_name],
} unless CallFlow.find_by_dnis(call[1].variables[:x_agi_dnid]) == nil #only get routes in the active call list
end
call_list
else
[]
end
end
end
require 'autoload_paths'
require 'ahn_api'
Thread.new { AhnApi.run! unless ENV['AHN_ENV'] == 'test' }
class TeleClickController < Adhearsion::CallController
def run
answer
interruptible_play PLEASE_WAIT_WAV if call.variables[:play_message]
dial_string = build_dial_string call.variables[:dial_me]
dial(dial_string, from: digits_of(call.to))
hangup
end
def digits_of(sip_string)
sip_string.to_s.gsub(/[^\d]/, '')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment