Module: Librevox::Applications

Included in:
Listener::Outbound
Defined in:
lib/librevox/applications.rb

Overview

All applications should call ‘application` with the following parameters:

`name` - name of the application
`args` - arguments as a string to be sent to FreeSWITCH (optional)
`params` - parameters for tweaking the command (optional)

Instance Method Summary collapse

Instance Method Details

#answer(&block) ⇒ Object

Answers an incoming call or session.



11
12
13
# File 'lib/librevox/applications.rb', line 11

def answer &block
  application "answer", &block
end

#att_xfer(endpoint, &block) ⇒ Object

TODO:

Add support for origination_cancel_key

Make an attended transfer

Examples:

att_xfer("user/davis")

See Also:



20
21
22
# File 'lib/librevox/applications.rb', line 20

def att_xfer endpoint, &block
  application "att_xfer", endpoint, &block
end

#bind_meta_app(args = {}, &block) ⇒ Object

Binds an application to the specified call legs.

Examples:

bind_meta_app :key          => 2,
              :listen_to    => "a",
              :respond_on   => "s",
              :application  => "execute_extension",
              :parameters   => "dx XML features"

See Also:



32
33
34
35
36
37
38
# File 'lib/librevox/applications.rb', line 32

def bind_meta_app args={}, &block
  arg_string =
    args.values_at(:key, :listen_to, :respond_on, :application).join(" ")
  arg_string += "::#{args[:parameters]}" if args[:parameters]

  application "bind_meta_app", arg_string, &block
end

#bridge(*args, &block) ⇒ Object

Bridges an incoming call to an endpoint, optionally taking an array of channel variables to set. If given an array of arrays, each contained array of endpoints will be called simultaneously, with the next array of endpoints as failover. See the examples below for different constructs and the callstring it sends to FreeSWITCH.

Examples:

bridge "user/coltrane", "user/backup-office"
#=> user/coltrane,user/backup-office

With channel variables

bridge "user/coltrane", "user/backup-office", :some_var => "value"
#=> {some_var=value}user/coltrane,user/backup-office

With failover

bridge ['user/coltrane', 'user/davis'], ['user/sun-ra', 'user/taylor']
#=> user/coltrane,user/davis|user/sun-ra,user/taylor

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/librevox/applications.rb', line 56

def bridge *args, &block
  variables = if args.last.is_a? Hash
                # We need to sort the key/value pairs to facilitate testing.
                # This can be removed once 1.8-compat is dropped.
                key_value_pairs = args.pop.sort {|x,y| x.to_s <=> y.to_s}
                key_value_pairs.map! {|k,v| "#{k}=#{v}"}
                "{#{key_value_pairs.join(",")}}"
              else
                ""
              end

  endpoints = if args.first.is_a? Array
                args.map {|e| e.join(",")}.join("|")
              else
                args.join ","
              end

  application "bridge", variables + endpoints, &block
end

#deflect(uri, &block) ⇒ Object

Deflect a call by sending a REFER. Takes a SIP URI as argument, rerouting the call to that SIP URI.

Beware that REFER only can be used on established calls. If a call hasn’t been established with e.g. the #answer application, you should use #redirect instead.



86
87
88
# File 'lib/librevox/applications.rb', line 86

def deflect uri, &block
  application "deflect", uri, &block
end

#export(var, args = {}, &block) ⇒ Object

Exports a channel variable from the A leg to the B leg. Variables and their values will be replicated in any new channels created from the one export was called.

Set :local => false if the variable should only be exported to the B-leg.

Examples:

export "some_var"

Only export to B-leg

export "some_var", :local => false

See Also:



101
102
103
104
105
# File 'lib/librevox/applications.rb', line 101

def export var, args={}, &block
  nolocal = args[:local] == false ? "nolocal:" : "" # ugly!!111

  application "export", "#{nolocal}#{var}", &block
end

#gentones(tgml, &block) ⇒ Object

Generate TGML tones

Examples:

Generate a 500ms beep at 800Hz

gentones "%(500,0,800)"

Generate a DTMF string

gentones "0800500005"

See Also:



113
114
115
# File 'lib/librevox/applications.rb', line 113

def gentones tgml , &block
  application "gentones", tgml, &block
end

#hangup(cause = "", &block) ⇒ Object

Hang up current channel

Examples:

hangup

Hang up with a reason

hangup "USER_BUSY"

See Also:



123
124
125
# File 'lib/librevox/applications.rb', line 123

def hangup cause="", &block
  application "hangup", cause, &block
end

#play_and_get_digits(file, invalid_file, args = {}, &block) ⇒ Object

Plays a sound file and reads DTMF presses.

Examples:

play_and_get_digits "please-enter.wav", "wrong-choice.wav",
  :min          => 1,
  :max          => 2,
  :tries        => 3,
  :terminators  => "#",
  :timeout      => 5000,
  :regexp       => '\d+'

See Also:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/librevox/applications.rb', line 137

def play_and_get_digits file, invalid_file, args={}, &block
  min         = args[:min]          || 1
  max         = args[:max]          || 2
  tries       = args[:tries]        || 3
  terminators = args[:terminators]  || "#"
  timeout     = args[:timeout]      || 5000
  variable    = args[:variable]     || "read_digits_var"
  regexp      = args[:regexp]       || "\\d+"

  args = [min, max, tries, timeout, terminators, file, invalid_file,
    variable, regexp].join " "

  params = {:variable => variable}

  application "play_and_get_digits", args, params, &block
end

#playback(file, &block) ⇒ Object

Plays a sound file on the current channel.

Examples:

playback "/path/to/file.wav"

See Also:



158
159
160
# File 'lib/librevox/applications.rb', line 158

def playback file, &block
  application "playback", file, &block
end

#pre_answer(&block) ⇒ Object

Pre-answer establishes early media but does not answer.



166
167
168
# File 'lib/librevox/applications.rb', line 166

def pre_answer &block
  application "pre_answer", &block
end

#read(file, args = {}, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/librevox/applications.rb', line 171

def read file, args={}, &block
  min         = args[:min]          || 1
  max         = args[:max]          || 2
  terminators = args[:terminators]  || "#"
  timeout     = args[:timeout]      || 5000
  variable    = args[:variable]     || "read_digits_var"

  arg_string = "%s %s %s %s %s %s" % [min, max, file, variable, timeout,
    terminators]

  params = {:variable => variable}

  application "read", arg_string, params, &block
end

#record(path, params = {}, &block) ⇒ Object

Records a message, with an optional limit on the maximum duration of the recording.

Examples:

Without limit

record "/path/to/new/file.wac"

With 20 second limit

record "/path/to/new/file.wac", :limit => 20

See Also:



193
194
195
196
# File 'lib/librevox/applications.rb', line 193

def record path, params={}, &block
  args = [path, params[:limit]].compact.join(" ")
  application "record", args, &block
end

#redirect(uri, &block) ⇒ Object

Redirect a channel to another endpoint. You must take care to not redirect incompatible channels, as that wont have the desired effect. I.e. if you redirect to a SIP URI, it should be a SIP channel.

#redirect can only be used on non-established calls, i.e. calls that has not been answered with the #answer application yet. If the call has been answered, use #deflect instead.



209
210
211
# File 'lib/librevox/applications.rb', line 209

def redirect uri, &block
  application "redirect", uri, &block
end

#respond(code, &block) ⇒ Object

Send SIP session respond code.

Examples:

Send 403 Forbidden

respond 403

See Also:



217
218
219
# File 'lib/librevox/applications.rb', line 217

def respond code, &block
  application "respond", code.to_s, &block
end

#set(variable, value, &block) ⇒ Object

Sets a channel variable.

Examples:

set "some_var", "some value"

See Also:



225
226
227
# File 'lib/librevox/applications.rb', line 225

def set variable, value, &block
  application "set", "#{variable}=#{value}", &block
end

#transfer(context, &block) ⇒ Object

Transfers the current channel to a new context.

Examples:

transfer "new_context"

See Also:



233
234
235
# File 'lib/librevox/applications.rb', line 233

def transfer context, &block
  application "transfer", context, &block
end

#unbind_meta_app(key, &block) ⇒ Object

Unbinds a previously bound key with bind_meta_app



241
242
243
# File 'lib/librevox/applications.rb', line 241

def unbind_meta_app key, &block
  application "unbind_meta_app", key.to_s, &block
end

#unset(variable, &block) ⇒ Object

Unset a channel variable.



249
250
251
# File 'lib/librevox/applications.rb', line 249

def unset variable, &block
  application "unset", variable, &block
end