Skip to content

Instantly share code, notes, and snippets.

@eric
Created September 24, 2008 02:36
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 eric/12459 to your computer and use it in GitHub Desktop.
Save eric/12459 to your computer and use it in GitHub Desktop.
Adhearsion example that demonstrates the basics of click-to-call
# Example of a simple click-to-call script. Includes a trick showing how to
# create a module for dialplan helpers. Tested on Cloudvox via AGI (Asterisk's
# phone call API) and Adhearsion Ruby library.
local_config = YAML::load(File.read(File.dirname(__FILE__) + '/local_config.yml')) rescue {}
module DialplanHelper
attr_accessor :default_swift_voice
attr_accessor :default_outgoing_context
def input_phone_number(timeout)
phone_number = wait_for_digit(timeout).to_s
phone_number << input(phone_number == '1' ? 10 : 9, :timeout => timeout).to_s
phone_number
end
def swift(message, options = {})
voice = options[:voice] || default_swift_voice
message = "#{voice}^#{message}" unless voice.empty?
execute('swift', '%p' % message)
end
def cloudvox_dial(number, options = {})
dial("Local/#{number}@#{default_outgoing_context}", options)
end
end
default {
# Include some helpers to make our life easier
extend DialplanHelper
# Setup our default voice and default outgoing context
self.default_swift_voice = 'Callie'
self.default_outgoing_context = local_config['outgoing_context']
unless connect_to = call.variables[:connect_to]
swift 'Please enter the number you wish to call.'
connect_to = input_phone_number(10.seconds)
end
# Detect if we got a valid phone number or not
unless connect_to.to_s.match(/^1?\d{10}$/)
ahn_log "Could not call #{connect_to.inspect}."
play 'cannot-complete-as-dialed'
sleep 0.3
play 'check-number-dial-again'
else
# Get swift to speak each number separately
spaced_numbers = connect_to.split(//).join(' ')
swift "Calling '#{spaced_numbers}'. Please wait..."
cloudvox_dial connect_to, :caller_id => local_config['caller_id']
end
}
require 'drb'
require 'yaml'
source_number = ARGV[0]
dest_number = ARGV[1]
# Execute this by running "ruby initiate.rb 2065551212 2095551212"
unless dest_number
puts "Usage: #{$0} <first_number> <second_number>"
exit 1
end
local_config = YAML::load(File.read(File.dirname(__FILE__) + '/local_config.yml')) rescue {}
agi_config = local_config['agi'] || {}
drb_config = local_config['drb'] || {}
agi_url = URI::Generic.build :scheme => 'agi', :port => agi_config['listening_port'],
:host => agi_config['listening_host'] || Socket.gethostname,
:path => '/',
:query => "context=default&connect_to=#{dest_number}"
Adhearsion = DRbObject.new_with_uri "druby://localhost:#{drb_config['port']}"
Adhearsion.proxy.call_and_exec "Local/#{source_number}@#{local_config['outgoing_context']}",
'AGI', :args => agi_url.to_s, :caller_id => local_config['caller_id']
outgoing_context: outgoing-837
caller_id: 2063576220
drb:
port: 24257
ami:
host: vox4.cloudvox.com
port: 13539
username: manager
password: "050e296348b37ff"
agi:
listening_port: 4573
## Adhearsion startup file: config/startup.rb
local_config = YAML::load(File.read(File.dirname(__FILE__) + '/../local_config.yml')) rescue {}
Adhearsion::Configuration.configure do |config|
# Supported levels (in increasing severity) -- :debug < :info < :warn < :error < :fatal
config.logging :level => :info
config.enable_asterisk local_config['agi'].symbolize_keys
config.asterisk.enable_ami local_config['ami']
config.enable_drb local_config['drb'].symbolize_keys
end
Adhearsion::Initializer.start_from_init_file(__FILE__, File.dirname(__FILE__) + "/..")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment