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

#answerObject

Answers an incoming call or session.



13
14
15
# File 'lib/librevox/applications.rb', line 13

def answer
  application "answer"
end

#att_xfer(endpoint) ⇒ Object

TODO:

Add support for origination_cancel_key

Make an attended transfer

Examples:

att_xfer("user/davis")

See Also:



28
29
30
# File 'lib/librevox/applications.rb', line 28

def att_xfer(endpoint)
  application "att_xfer", endpoint
end

#bind_meta_app(args = {}) ⇒ 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:



40
41
42
43
44
45
46
# File 'lib/librevox/applications.rb', line 40

def bind_meta_app(args = {})
  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
end

#bridge(*args) ⇒ 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:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/librevox/applications.rb', line 63

def bridge(*args)
  variables = if args.last.is_a? Hash
                pairs = args.pop.map {|k,v| "#{k}=#{v}"}
                "{#{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
end

#deflect(uri) ⇒ 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.



90
91
92
# File 'lib/librevox/applications.rb', line 90

def deflect(uri)
  application "deflect", uri
end

#export(var, args = {}) ⇒ 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:



105
106
107
108
109
# File 'lib/librevox/applications.rb', line 105

def export(var, args = {})
  nolocal = args[:local] == false ? "nolocal:" : ""

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

#gentones(tgml) ⇒ Object

Generate TGML tones

Examples:

Generate a 500ms beep at 800Hz

gentones "%(500,0,800)"

Generate a DTMF string

gentones "0800500005"

See Also:



117
118
119
# File 'lib/librevox/applications.rb', line 117

def gentones(tgml)
  application "gentones", tgml
end

#hangup(cause = "") ⇒ Object

Hang up current channel

Examples:

hangup

Hang up with a reason

hangup "USER_BUSY"

See Also:



127
128
129
# File 'lib/librevox/applications.rb', line 127

def hangup(cause = "")
  application "hangup", cause
end

#multiset(vars) ⇒ Object

Sets multiple channel variables in a single application call.

Examples:

multiset "var1" => "val1", "var2" => "val2"

See Also:



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

def multiset(vars)
  args = "^^|" + vars.map { |k, v| "#{k}=#{v}" }.join("|")
  application "multiset", args
end

#parkObject

Parks a call, keeping it active without routing it anywhere.



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

def park
  application "park"
end

#play_and_get_digits(file, invalid_file, args = {}) ⇒ 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:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/librevox/applications.rb', line 141

def play_and_get_digits(file, invalid_file, args = {})
  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 " "

  application "play_and_get_digits", args, variable: variable
end

#playback(file) ⇒ Object

Plays a sound file on the current channel.

Examples:

playback "/path/to/file.wav"

See Also:



160
161
162
# File 'lib/librevox/applications.rb', line 160

def playback(file)
  application "playback", file
end

#pre_answerObject

Pre-answer establishes early media but does not answer.



168
169
170
# File 'lib/librevox/applications.rb', line 168

def pre_answer
  application "pre_answer"
end

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



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

def read(file, args = {})
  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]

  application "read", arg_string, variable: variable
end

#record(path, params = {}) ⇒ 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 = {})
  args = [path, params[:limit]].compact.join(" ")
  application "record", args
end

#redirect(uri) ⇒ 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)
  application "redirect", uri
end

#respond(code) ⇒ 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)
  application "respond", code.to_s
end

#set(variable, value) ⇒ 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)
  application "set", "#{variable}=#{value}"
end

#transfer(context) ⇒ Object

Transfers the current channel to a new context.

Examples:

transfer "new_context"

See Also:



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

def transfer(context)
  application "transfer", context
end

#unbind_meta_app(key) ⇒ Object

Unbinds a previously bound key with bind_meta_app



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

def unbind_meta_app(key)
  application "unbind_meta_app", key.to_s
end

#unset(variable) ⇒ Object

Unset a channel variable.



258
259
260
# File 'lib/librevox/applications.rb', line 258

def unset(variable)
  application "unset", variable
end