Manipulating Channel Variables

From FreeSWITCH Wiki
Jump to: navigation, search

Contents

Basic Dialplan Usage

It's easy to use the pre-existing value of a variable to change call flow, an example, found in the default configuration:

 <condition field="${call_debug}" expression="^true$" break="never">
   <action application="info"/>
 </condition>

This will run the "info" app anytime a call hits this extension with the "call_debug" variable set to "true".

Trimming and Substrings

You can select a portion of a variable's value, (just like a substr function) like this:

 var = 1234567890
 ${var:offset:length} 
 ${var:0:1}  // 1
 ${var:1}    // 234567890
 ${var:-4}   // 7890
 ${var:-4:2} // 78
 ${var:4:2}  // 56


Using anything <= 0 as the length will return from the specified position to the end of the string.

Search and replace

Using an inline Lua command, you can perform a search and replace against a text string:

From the dialplan:

 <action application="set" data="RETURN_STRING=${lua(~stream:write(tostring(string.gsub(&quot;STRING_TO_SEARCH&quot;, &quot;STRING_TO_FIND&quot;, &quot;REPLACEMENT_STRING&quot;))))}"/>

From the CLI:

 lua ~stream:write(tostring(string.gsub("STRING_TO_SEARCH", "STRING_TO_FIND", "REPLACEMENT_STRING")))

Setting a variable as undefined

You can set a variable to _undef_

by default you might see

From: "sipp" <sip:199098@192.168.199.81>;tag=c8BKQ1ta440Hj.
Remote-Party-ID: "sipp" <sip:199098@192.168.199.81>;party=calling;screen=yes;privacy=off.

if you set

<action application="set" data="effective_caller_id_name=_undef_"/>

you get

From: <sip:199098@192.168.199.81>;tag=c8BKQ1ta440Hj.
Remote-Party-ID: <sip:199098@192.168.199.81>;party=calling;screen=yes;privacy=off.

FYI: in Version 1.0.4 (exported) this will result in

From: <sip:199098@192.168.199.81>;tag=c8BKQ1ta440Hj.
Remote-Party-ID: "_undef_" <sip:199098@192.168.199.81>;party=calling;screen=yes;privacy=off.