Module: Adhearsion::Asterisk::CallControllerMethods
- Defined in:
- lib/adhearsion/asterisk/call_controller_methods.rb
Constant Summary collapse
- DYNAMIC_FEATURE_EXTENSIONS =
{ :attended_transfer => lambda do || variable "TRANSFER_CONTEXT" => [:context] if && .has_key?(:context) extend_dynamic_features_with "atxfer" end, :blind_transfer => lambda do || variable "TRANSFER_CONTEXT" => [:context] if && .has_key?(:context) extend_dynamic_features_with 'blindxfer' end }
Instance Method Summary collapse
- #agi(name, *params) ⇒ Object
-
#disable_feature(feature_name) ⇒ Object
Disables a feature name specified in features.conf.
-
#enable_feature(*args) ⇒ Object
A high-level way of enabling features you create/uncomment from features.conf.
-
#execute(name, *params) ⇒ Object
This asterisk dialplan command allows you to instruct Asterisk to start applications which are typically run from extensions.conf and do not have AGI command equivalents.
-
#extend_dynamic_features_with(feature_name) ⇒ Object
helper method that should probably should private…
-
#get_variable(variable_name) ⇒ Object
Issue this command to access a channel variable that exists in the asterisk dialplan (i.e. extensions.conf) Use get_variable to pass information from other modules or high level configurations from the asterisk dialplan to the adhearsion dialplan.
-
#meetme(conference_id, options = {}) ⇒ Object
Used to join a particular conference with the MeetMe application.
-
#play_digits(argument) ⇒ Object
Executes SayDigits with the passed argument.
-
#play_numeric(argument) ⇒ Object
Executes SayNumber with the passed argument.
-
#play_soundfile(argument) ⇒ Boolean
Instruct Asterisk to play a sound file to the channel.
-
#play_time(*args) ⇒ Object
Plays the given Date, Time, or Integer (seconds since epoch) using the given timezone and format.
-
#play_tones(argument, wait = false) ⇒ Object
Executes Playtones with the passed argument.
-
#queue(queue_name) ⇒ Adhearsion::Asterisk::QueueProxy
Place a call in a queue to be answered by a registered agent.
-
#set_variable(variable_name, value) ⇒ Object
Pass information back to the asterisk dial plan.
-
#sip_add_header(header, value) ⇒ String
Issue the command to add a custom SIP header to the current call channel example use: sip_add_header(“x-ahn-test”, “rubyrox”).
-
#sip_get_header(header) ⇒ String
Issue the command to fetch a SIP header from the current call channel example use: sip_get_header(“x-ahn-test”).
-
#variable(*args) ⇒ Object
Allows you to either set or get a channel variable from Asterisk.
-
#verbose(message, level = nil) ⇒ Object
Sends a message to the console via the verbose message system.
-
#voicemail(*args) ⇒ Object
Send a caller to a voicemail box to leave a message.
-
#voicemail_main(options = {}) ⇒ Object
The voicemail_main method puts a caller into the voicemail system to fetch their voicemail or set options for their voicemail box.
Instance Method Details
#agi(name, *params) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 17 def agi(name, *params) component = Punchblock::Component::Asterisk::AGI::Command.new :name => name, :params => params execute_component_and_await_completion component complete_reason = component.complete_event.reason raise Adhearsion::Call::Hangup if complete_reason.is_a?(Punchblock::Event::Complete::Hangup) [:code, :result, :data].map { |p| complete_reason.send p } end |
#disable_feature(feature_name) ⇒ Object
Disables a feature name specified in features.conf. If you’re disabling it, it was probably set by enable_feature().
100 101 102 103 104 105 106 107 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 100 def disable_feature(feature_name) enabled_features_variable = variable 'DYNAMIC_FEATURES' enabled_features = enabled_features_variable.split('#') if enabled_features.include? feature_name enabled_features.delete feature_name variable 'DYNAMIC_FEATURES' => enabled_features.join('#') end end |
#enable_feature(*args) ⇒ Object
A high-level way of enabling features you create/uncomment from features.conf.
Certain Symbol features you enable (as defined in DYNAMIC_FEATURE_EXTENSIONS) have optional arguments that you can also specify here. The usage examples show how to do this.
Usage examples:
enable_feature :attended_transfer # Enables "atxfer"
enable_feature :attended_transfer, :context => "my_dial" # Enables "atxfer" and then
# sets "TRANSFER_CONTEXT" to :context's value
enable_feature :blind_transfer, :context => 'my_dial' # Enables 'blindxfer' and sets TRANSFER_CONTEXT
enable_feature "foobar" # Enables "foobar"
enable_feature("dup"); enable_feature("dup") # Enables "dup" only once.
def voicemail(*args)
= args.last.kind_of?(Hash) ? args.pop : {}
mailbox_number = args.shift
greeting_option = .delete :greeting
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 82 def enable_feature(*args) feature_name, = args.flatten if DYNAMIC_FEATURE_EXTENSIONS.has_key? feature_name instance_exec(, &DYNAMIC_FEATURE_EXTENSIONS[feature_name]) else unless .nil? or .empty? raise ArgumentError, "You cannot supply optional options when the feature name is " + "not internally recognized!" end extend_dynamic_features_with feature_name end end |
#execute(name, *params) ⇒ Object
This asterisk dialplan command allows you to instruct Asterisk to start applications which are typically run from extensions.conf and do not have AGI command equivalents.
For example, if there are specific asterisk modules you have loaded that will not be available through the standard commands provided through FAGI - then you can use EXEC.
37 38 39 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 37 def execute(name, *params) agi "EXEC #{name}", *params end |
#extend_dynamic_features_with(feature_name) ⇒ Object
helper method that should probably should private…
110 111 112 113 114 115 116 117 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 110 def extend_dynamic_features_with(feature_name) current_variable = variable("DYNAMIC_FEATURES") || '' enabled_features = current_variable.split '#' unless enabled_features.include? feature_name enabled_features << feature_name variable "DYNAMIC_FEATURES" => enabled_features.join('#') end end |
#get_variable(variable_name) ⇒ Object
Issue this command to access a channel variable that exists in the asterisk dialplan (i.e. extensions.conf) Use get_variable to pass information from other modules or high level configurations from the asterisk dialplan to the adhearsion dialplan.
@see: www.voip-info.org/wiki/view/get+variable Asterisk Get Variable
128 129 130 131 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 128 def get_variable(variable_name) code, result, data = agi "GET VARIABLE", variable_name data end |
#meetme(conference_id, options = {}) ⇒ Object
Used to join a particular conference with the MeetMe application. To use MeetMe, be sure you have a proper timing device configured on your Asterisk box. MeetMe is Asterisk’s built-in conferencing program.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 210 def meetme(conference_id, = {}) conference_id = conference_id.to_s.scan(/\w/).join command_flags = [:options].to_s # This is a passthrough string straight to Asterisk pin = [:pin] raise ArgumentError, "A conference PIN number must be numerical!" if pin && pin.to_s !~ /^\d+$/ # To disable dynamic conference creation set :use_static_conf => true use_static_conf = .has_key?(:use_static_conf) ? [:use_static_conf] : false # The 'd' option of MeetMe creates conferences dynamically. command_flags += 'd' unless command_flags.include?('d') || use_static_conf execute "MeetMe", conference_id, command_flags, [:pin] end |
#play_digits(argument) ⇒ Object
Executes SayDigits with the passed argument.
371 372 373 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 371 def play_digits(argument) execute "SayDigits", argument end |
#play_numeric(argument) ⇒ Object
Executes SayNumber with the passed argument.
364 365 366 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 364 def play_numeric(argument) execute "SayNumber", argument end |
#play_soundfile(argument) ⇒ Boolean
Instruct Asterisk to play a sound file to the channel.
390 391 392 393 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 390 def play_soundfile(argument) execute "Playback", argument get_variable('PLAYBACKSTATUS') == PLAYBACK_SUCCESS end |
#play_time(*args) ⇒ Object
Plays the given Date, Time, or Integer (seconds since epoch) using the given timezone and format.
:timezone
- Sends a timezone to asterisk. See /usr/share/zoneinfo for a list. Defaults to the machine timezone. :format
- This is the format the time is to be said in. Defaults to “ABdY ‘digits/at’ IMp”
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 340 def play_time(*args) argument, = args.flatten ||= {} return false unless .is_a? Hash timezone = .delete(:timezone) || '' format = .delete(:format) || '' epoch = case argument when Time || DateTime argument.to_i when Date format = 'BdY' unless format.present? argument.to_time.to_i end return false if epoch.nil? execute "SayUnixTime", epoch, timezone, format end |
#play_tones(argument, wait = false) ⇒ Object
Executes Playtones with the passed argument.
379 380 381 382 383 384 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 379 def play_tones(argument, wait = false) tones = [*argument].join(",") execute("Playtones", tones).tap do sleep tones.scan(/(?<=\/)\d+/).map(&:to_i).sum.to_f / 1000 if wait end end |
#queue(queue_name) ⇒ Adhearsion::Asterisk::QueueProxy
Place a call in a queue to be answered by a registered agent. You must then call #join!
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 315 def queue(queue_name) queue_name = queue_name.to_s @queue_proxy_hash_lock ||= Mutex.new @queue_proxy_hash_lock.synchronize do @queue_proxy_hash ||= {} if @queue_proxy_hash.has_key? queue_name return @queue_proxy_hash[queue_name] else proxy = @queue_proxy_hash[queue_name] = QueueProxy.new(queue_name, self) return proxy end end end |
#set_variable(variable_name, value) ⇒ Object
Pass information back to the asterisk dial plan.
Keep in mind that the variables are not global variables. These variables only exist for the channel related to the call that is being serviced by the particular instance of your adhearsion application. You will not be able to pass information back to the asterisk dialplan for other instances of your adhearsion application to share. Once the channel is “hungup” then the variables are cleared and their information is gone.
146 147 148 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 146 def set_variable(variable_name, value) agi "SET VARIABLE", variable_name, value end |
#sip_add_header(header, value) ⇒ String
161 162 163 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 161 def sip_add_header(header, value) execute "SIPAddHeader", "#{header}: #{value}" end |
#sip_get_header(header) ⇒ String
Issue the command to fetch a SIP header from the current call channel example use: sip_get_header(“x-ahn-test”)
@param the name of the SIP header to get
175 176 177 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 175 def sip_get_header(header) get_variable "SIP_HEADER(#{header})" end |
#variable(*args) ⇒ Object
Allows you to either set or get a channel variable from Asterisk. The method takes a hash key/value pair if you would like to set a variable Or a single string with the variable to get from Asterisk
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 184 def variable(*args) if args.last.kind_of? Hash assignments = args.pop raise ArgumentError, "Can't mix variable setting and fetching!" if args.any? assignments.each_pair do |key, value| set_variable key, value end else if args.size == 1 get_variable args.first else args.map { |var| get_variable var } end end end |
#verbose(message, level = nil) ⇒ Object
Sends a message to the console via the verbose message system.
of actions happening within Adhearsion.
verbose 'Processing call with Adhearsion' 3
55 56 57 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 55 def verbose(, level = nil) agi 'VERBOSE', , level end |
#voicemail(*args) ⇒ Object
Send a caller to a voicemail box to leave a message.
The method takes the mailbox_number of the user to leave a message for and a greeting_option that will determine which message gets played to the caller.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 233 def voicemail(*args) = args.last.kind_of?(Hash) ? args.pop : {} mailbox_number = args.shift greeting_option = .delete :greeting skip_option = .delete :skip raise ArgumentError, 'You supplied too many arguments!' if mailbox_number && .any? greeting_option = case greeting_option when :busy then 'b' when :unavailable then 'u' when nil then nil else raise ArgumentError, "Unrecognized greeting #{greeting_option}" end skip_option &&= 's' = "#{greeting_option}#{skip_option}" raise ArgumentError, "Mailbox cannot be blank!" if !mailbox_number.nil? && mailbox_number.blank? number_with_context = if mailbox_number then mailbox_number else raise ArgumentError, "You must supply ONE context name!" unless .size == 1 context_name, mailboxes = .to_a.first Array(mailboxes).map do |mailbox| raise ArgumentError, "Mailbox numbers must be numerical!" unless mailbox.to_s =~ /^\d+$/ [mailbox, context_name].join '@' end.join '&' end execute 'voicemail', number_with_context, case variable('VMSTATUS') when 'SUCCESS' then true when 'USEREXIT' then false else nil end end |
#voicemail_main(options = {}) ⇒ Object
The voicemail_main method puts a caller into the voicemail system to fetch their voicemail or set options for their voicemail box.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/adhearsion/asterisk/call_controller_methods.rb', line 275 def voicemail_main( = {}) mailbox, context, folder = .values_at :mailbox, :context, :folder authenticate = .has_key?(:authenticate) ? [:authenticate] : true folder = if folder if folder.to_s =~ /^[\w_]+$/ "a(#{folder})" else raise ArgumentError, "Voicemail folder must be alphanumerical/underscore characters only!" end elsif folder == '' raise ArgumentError, "Folder name cannot be an empty String!" else nil end real_mailbox = "" real_mailbox << "#{mailbox}" unless mailbox.blank? real_mailbox << "@#{context}" unless context.blank? = "" << "s" if !authenticate << folder unless folder.blank? command_args = [real_mailbox] command_args << unless .blank? command_args.clear if command_args == [""] execute 'VoiceMailMain', *command_args end |