Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

We have only touched upon dialplan variables.  They are essential to do anything useful inside a 

Fs
 dialplan.

Variables are named bits of information that are stored for later use.  When a dialplan starts, a wealth of information comes pre-loaded in the form of variables. You can use regular expressions to match against a variable and control your dialplan execution as a result.

We will touch on a few common variables here to solidify their proper use and demonstrate some features of variable substitution in 

Fs
.

Accessing Variables

There are three kinds of variables available for you to access in your XML dialplan:

    • Channel variables
    • Global variables
    • Built-in variables (time, date, etc.)
Channel Variables

Channel variables can be used to get information about a call and control its behavior as it progresses through your dialplan.  Channel variables contain a wealth of information about the call being processed and an amazing array of options that can be set.  You access the contents of a channel variable by surrounding its name with ${ }.  For example, to append the default SIP domain to the end of a dial string, you can just reference ${domain_name} in the dial string text, and its value will be substituted automatically:

Code Block
languagexml
themeEmacs
    <action application="bridge" data="user/1000@${domain_name}/>

You set the value of a channel variable like this:

Code Block
languagexml
themeEmacs
    <action application="set" data="call_timeout=30"/>

For the complete list of Channel Variables and alternate ways to use them in dial strings, see: Channel Variables.  We'll present a short list here to give you a sense of what's possible.

Variable NameDescription
caller_id_name

The name of the calling party.

destination_numberThe number that the calling party dialed.
directionWhether this call leg is inbound or outbound
channel_name

The name of this call's inbound channel, for example:

sofia/sales/John_Smith@192.168.1.1

call_timeoutSet to the number of seconds to wait for a call to be answered before giving up.
stateThe state of this channel, for example CS_EXECUTE or CS_HANGUP
bridge_hangup_causeThe reason a call ended, for example NO_ANSWER, NORMAL_CLEARING or USER_BUSY


Built-In Variables

Built-in variables are accessed by name, without using the customary $ prefix.

The built-in variables are:   (updated 29-July-2014, switch_xml.c, switch_xml_std_datetime_check())

Built-In NameDescription

Standard Date and Time

date-time

The current data and time: YYYY-MM-DD HH:MM:SS

For example: 2014-08-12 15:34:59

time-of-day

The current local time: HH:MM:SS

For example: 15:34:59

Data and Time Parts (all based on local timezone)
yearThe current year through. (1970 through 9999)
monThe current month. (1 through 12)
mdayThe day of the current month. (1 through 31)
hourThe hour of the current day. (0 through 23)
minuteThe minute of the current hour. (0 through 59)
Advanced Data and Time Parts
wdayThe day of the current week. (Sunday = 1 through 7)
weekThe current week since January 1st. (1 through 53)
mweekThe week of the current month. (1 through 6)
ydayThe day of the current year (1 through 366)
mindayThe current minute of the day starting at midnight. (0 through 1440)
tz-offsetTimezone offset in hours from GMT. (-11 to +11)
dstReturns 1 if daylight savings time is in effect, or 0 if not.

In condition statements, you can compare against more than one built-in variable.  All comparisons must match (logical AND) before its actions will be executed.

There are a number of ways match against a built-in variable.  You can:

KindExampleDescription
Equality<condition wday="1">Is it Sunday?
Range<condition wday="2-4">

Is it Monday, Tuesday, or Wednesday?

List<condition wday="1,4">Is it Sunday or Wednesday?
Combination<condition wday="1-3,7">Is it Sunday, Monday, Tuesday, or Saturday?

The range operation can be used on times and dates as well.

Code Block
languagexml
themeEmacs
<condition time-of-day="08:00:00-09:00:00">
<condition date-time="2010-10-01 00:00:01~2010-10-15 23:59:59">
Note

The range operator for dates is "~", not "–"

Advanced Concepts

Nested Conditions

Conditions can be nested one inside each other. Nested conditions is hard, very very hard to get right. You may want to use a script, instead (yes, yes, you want to use a script). See mod_lua for Lua scripting.

When a (main) condition has nested conditions, first its (main) expression is evaluated, and its (main) actions are pushed in TODO list, then nested conditions are evaluated, and their actions pushed.

Each condition has an implied parameter "require-nested", that is by default set to "true".

When we have a condition with require-nested parameter set to "true", then ALL its nested conditions must evaluate to true (AND its expression must evaluate to true) for the condition to be evaluated to true.

If "require-nested" is set to false, then only the expression must evaluate to true for the condition to be true, whatever the nested conditions are.

Conditions are checked the usual way (respecting the "break" parameter), and actions inside conditions added to TODO list.

Let's see an example extension with nested conditions you can play with, checking all possible cases and corner cases.

 

Code Block
languagexml
themeEmacs
<extension name="nested_example">
    <condition field="destination_number" expression="^2901$" require-nested="false">
        <action application="log" data="ERR 00 CIDnum is ${caller_id_number} CIDname is ${caller_id_name}" />
        <action application="set" data="var_01=N/A" inline="true"/>
        <action application="set" data="var_02=N/A" inline="true"/>
        <action application="set" data="var_03=N/A" inline="true"/>
        <action application="set" data="var_04=N/A" inline="true"/>
        <action application="set" data="var_05=N/A" inline="true"/>
        <action application="log" data="ERR 01 I'm before..."/>
        <action application="set" data="var_01=01" inline="true"/>
        <action application="log" data="ERR 02 I'm before  ${var_01} ${var_02} ${var_03} ${var_04} ${var_05}"/>
        <condition field="caller_id_number" expression="1011" break="on-false">
            <action application="log" data="ERR 03 I'm the first..."/>
            <action application="log" data="ERR 04 I'm the first CIDnum is ${caller_id_number}" />
            <action application="set" data="var_02=02" inline="true"/>
            <action application="log" data="ERR 05 I'm the first ${var_01} ${var_02} ${var_03} ${var_04} ${var_05}"/>
        </condition>
        <action application="log" data="ERR 06 I'm in between..."/>
        <action application="set" data="var_03=03" inline="true"/>
        <action application="log" data="ERR 07 I'm in between ${var_01} ${var_02} ${var_03} ${var_04} ${var_05}"/>
        <condition field="${caller_id_name}" expression="Giovanni" break="on-false">
            <action application="log" data="ERR 08 I'm the second..."/>
            <action application="log" data="ERR 09 I'm the second CIDname is ${caller_id_name}" />
            <action application="set" data="var_04=04" inline="true"/>
            <action application="log" data="ERR 10 I'm the second ${var_01} ${var_02} ${var_03} ${var_04} ${var_05}"/>
        </condition>
        <action application="log" data="ERR 11 I'm after..."/>
        <action application="set" data="var_05=05" inline="true"/>
        <action application="log" data="ERR 12 I'm after ${var_01} ${var_02} ${var_03} ${var_04} ${var_05}"/>
    </condition>
</extension>
<extension name="call_has_not_stopped_before_here">
    <condition field="destination_number" expression=".*">
        <action application="log" data="ERR NOT STOPPED BEFORE HERE"/>
    </condition>
</extension>

There are a lot of things you can change to test this example:

  • Obviously, all the "expression": 2901, 1011, Giovanni

  • in first condition, require-nested can be set to "true" or "false"

  • the "inline" parameter of the "set" actions can be set to "true" or "false"

  • the "break" parameter of the nested conditions can be set to "on-false", "on-true", "never", "always"

Advanced Condition/Action Rules

break="on-true"

Here is a more complex example, using the break="on-true" and break="on-false" statements to perform time-based routing for a support organization. The user dials extension 1100. The actual support extension is 1105 and is staffed every day from 8am to 10pm, except Friday, when it is staffed between 8am and 1pm. At all other times, calls to 1100 are sent to the support "after-hours" mailbox.

Code Block
languagexml
themeEmacs
<extension name="Time-of-day">                                                      <!--if this is false, FreeSWITCH skips to the next *extension*.-->
	<condition field="destination_number" expression="^1100$" break="on-false"/>    <!--Don't bother evaluating the next condition set if this is true.-->
	<condition wday="6" hour="8-12" break="on-true">                                <!--Fri, 8am-12:59pm-->
		<action application="transfer" data="1105 XML default"/>
	</condition>
	<condition wday="1-5" hour="8-21" break="on-true">                              <!--Sunday-Thursday, 8am-9:59pm-->
		<action application="transfer" data="1105 XML default"/>
	</condition>
	<condition>                                                                     <!--this is a catch all, sending the call to voicemail at all other times. -->
		<action application="voicemail" data="default ${domain} 1105"/>
	</condition>
</extension>
Thing to Notice

The break="on-true" statement causes the extension to halt processing when the condition its used in matches.  In the example above, if the call arrives in the designated support hours, the call is transferred and the rest of the conditions are not processed.

By default, break="on-false" behavior is assumed if it is not specified.

break="never"

In this example, we use the break="never" statement to cause the first condition to always "fall through" to the next condition even if the first condition is false. This is useful to set various flags as part of extension processing. This example sets the variable begins_with_one if the destination number begins with 1.  

Code Block
languagexml
themeEmacs
<extension name="break-demo">
	<condition field="destination_number" expression="^1(\d+)$" break="never">
		<action application="set" data="begins_with_one=true"/>
	</condition>
	<condition field="destination_number" expression="^(\d+)$">
	 ...other actions that may query begins_with_one...
	</condition>
</extension>

Asterisk Pattern Matching

In addition to PCRE FreeSWITCH also supports Asterisk patterns. Any expression starting with an underscore (_) will be processed using asterisk pattern matching.  See Mod_dialplan_asterisk.

Note

Since * is normally reserved in regular expressions, its use in Asterisk patterns must be escaped with \, as shown in the second part of the example below.

Code Block
languagexml
themeEmacs
<extension name="US-Domestic">
	<condition field="destination_number" expression="_(NXXXXXXXXX)">
	<action application="bridge" data="sofia/internal/$1@example.com"/>
	</condition>
</extension>
 
<extension name="star-code-using-escape">
	<condition field="destination_number" expression="_(\*XX)(.)">
		<action application="log" data="ERR captured $1 ~~~ $2"/>
		<action application="answer"/>
		<action application="playback" data="tone_stream://path=${base_dir}/conf/tetris.ttml;loops=10"/>
	</condition>
</extension>

Example Library

This example shows how to ensure that all expressions match before executing actions, otherwise the anti-actions will be executed. In this case, the SIP gateway must be the default provider, and it must be an emergency call, and the auto-answer option must be enabled and stored in the database:

Code Block
languagexml
themeEmacs
<condition regex="all">
	<regex field="${sip_gateway}" expression="^${default_provider}$"/>
	<regex field="${emergency_call}" expression="^true$"/>
	<regex field="${db(select/emergency/autoanswer)}" expression="^1$"/>
 
	<!-- the following actions get executed if all regexes PASS -->
	<action application="set" data="call_timeout=60"/>
	<action application="set" data="effective_caller_id_name=${regex(${caller_id_name}|^Emerg(_.*)$|Auto%1)}"/>
	<action application="set" data="autoanswered=true"/>
	<action application="bridge" data="user/1000@${domain_name},sofia/gateway/1006_7217/${mobile_number}"/>
 
	<!-- the following anti-actions are executed if any of the regexes FAIL -->
	<anti-action application="set" data="effective_caller_id_name=${regex(${caller_id_name}|^Emerg(_.*)$|NotAuto%1)}"/>
	<anti-action application="set" data="call_timeout=30"/>
	<anti-action application="set" data="autoanswered=false"/>
	<anti-action application="bridge" data="user/1000@${domain_name},sofia/gateway/1006_7217/${mobile_number}"/>
</condition>

 

Caller Profile Fields vs. Channel Variables

One thing that may seem confusing is the distinction between a caller profile field (the built-in variables) and a channel variable.

Caller profile fields are accessed like this:

Code Block
languagexml
themeEmacs
<condition field="destination_number" attributes...>

 

While channel variables are accessed like this:

Code Block
languagexml
themeEmacs
<condition field="${sip_has_crypto}" attributes...>

 

Please take note of the ${variable_name} syntax. Channel variables may also be used in action statements.

In addition, API functions can be called from inside a condition statement to provide dynamic data.

For example, you can use the cond API:

Code Block
languagexml
themeEmacs
<condition field="${cond(${my_var} > 12 ? YES : NO)}" expression="^YES$">
    <action application="log" data="INFO ${my_var} is indeed greater than 12"/>
</condition>

 

This example tests ${my_var}. If it is more than 12, "YES" is returned. Otherwise "NO" is returned. The condition tests the results for "YES" and logs the resulting message to the FreeSWITCH log.

Availability of Variables

 

Informational Tip


Asterisk users must read!


 

 


The XML Dialplan has the ability to test a number of conditions based upon variables with expressions; however, it needs to be understood that some variables may not be available for conditional testing until the first transfer or execute_extension is performed (see workarounds below).

Why

In essence the XML Dialplan is to be used for Call Routing rather than for complex or extensive conditional tests and evaluations. This is why FreeSWITCH makes Lua, JavaScript, Perl, Python and other APIs available since they are far better alternatives than coming up with a convoluted XML solution, or worse yet some arcane and convoluted acronym such as "AEL".

This may be confusing to former Asterisk users since the info application such as <action application="info"/> will in fact display the variables as if they are available for a conditional test when in fact they may not.

The reason for this is that FreeSWITCH does the hunting and the executing in two separate steps. First - based on conditions, actions and anti-actions - all applications that need to be executed are gathered. Second, that sequence of applications is executed. This means that channel variables set by the executed applications won't be available to conditions at hunting time.

This is why you may find that your XML condition is failing even though the variable and its value are displayed with the <action application="info"/>.

Workarounds

The workaround for this is to either implement the vast majority of your dialplan logic within Lua, JavaScript or one of the other Dialplan scripting languages, OR execute an extension which will make those variables you seek to do conditional evaluations on available for parsing within your XML Dialplan condition.

NOTE: Since svn rev 14906 it is possible for certain applications to be run inline. This means that they are executed at hunting time which has the effect that channel variables set by these applications are available to the following conditions at hunting time.

 

Actions and Anti-Actions

So far, we've seen example dialplan entries that contain conditions along with the actions that run when the conditions match.

You can also specify 'anti-actions' that run if the conditions for the extension 'are not met'.

In this example, the value of ${my_var} is compared with 12, and a message is logged for either result.

Code Block
languagexml
themeEmacs
<condition field="${cond(${my_var} > 12 ? YES : NO)}" expression="^YES$">
    <action application="log" data="INFO ${my_var} is indeed greater than 12"/>
    <anti-action application="log" data="INFO ${my_var} is not greater than 12"/>
</condition>

 

Available Actions

See API Reference and Dialplan Functions

Inline Actions

You may set an extra attribute inline="true" on an action so that it will be executed during the hunting phase of the dialplan:

Code Block
languagexml
themeEmacs
<action inline="true" application="set" data="some_var=some_val"/>

 

This makes it possible to have a condition in the following extension, that matches on the ${some_var} field.

Note that the only applications that may be run inline are the ones that quickly get- or set some variable(s) and that don't access or modify the state of the current session.

Applications that are allowed to be run inline are:

check_acl

eval

event

export

log

presence

set

set_global

set_profile_var

set_user

unset

verbose_events

cidlookup

curl

easyroute

enum

lcr

nibblebill

odbc_query

Also keep in mind that inline executed applications don't show up in your call detail records like normally run applications do.

Complete Syntax

Code Block
languagexml
themeEmacs
<!-- For enumerated attributes the first value on the list is the default -->
 
<extension name="unique_extension_name" continue="[false|true]">
	<condition field="[field_name|${variable_name}|${api_func(api_args ${var_name})}]" expression="regular expression" break="[on-false|on-true|always|never]" require-nested="[true|false]">
		<condition ...><!-- Conditions can be nested --> ... </condition>
	<action application="app name" data="app arg"/>
	<anti-action application="app name" data="app arg"/>
	</condition>                                             <!-- Any number of condition tags may follow where the same rules apply -->
</extension>

 

Summary

Extensions hunting stops at the first extension where conditions evaluate to true and continue="false" (the default).

Conditions evaluate to true if the last checked condition is true.

Conditions are checked depth-first until:

break="on-false" and the condition is false

or break="on-true" and the condition is true

or break="always".

The condition is true if expression matches and either there are no nested conditions or require-nested="false" or nested conditions evaluate to true.

If condition is true, its <action>s are collected for execution, otherwise its <anti-action>s are collected for execution.

If require-nested="true" (the default) and nested conditions evalutate to false, no <anti-action>s are collected. (Is this intentional?)

 

Other Dialplan Pearls of Wisdom

The dialplan is parsed once when the call hits the dialplan parser in the ROUTING state. With one pass across the XML the result will be a complete list of instructions installed into the channel based on parsed <action> or <anti-action> tags.

Those accustomed to Asterisk may expect the call to follow the dialplan by executing the applications as it parses them allowing data obtained from one action to influence the next action. This is not the case with the exception being the ${api func(api arg ${var_name})} field type where a pluggable api call from a module may be executed as the parsing occurs. This is meant to be used to draw real-time information such as date and time or other quickly accessible information and should not be abused.

Auto Hunt

You many turn on auto_hunt and then if the Extension name precisely equals the dialed number, FreeSWITCH will jump to this extension to begin the searching. It may or may not match the conditions, though.

Dialing through gateways

"gateway" is treated as a keyword by mod_sofia, it obviously means the call will be placed through a configured gateway. This is an exception for the pattern sofia/profilename/extension@ip-address.

If a gateway, for instance, is named "gw", the bridge string for sending a call to gw's extension 100 would be:

Code Block
languagexml
themeEmacs
<extension name="testing">
    <condition field="destination_number" expression="^(100)$">
        <action application="bridge" data="sofia/gateway/gw/$1"/>
    </condition>
</extension>

 

 

Informational Tip


"destination_number" is a FreeSWITCH variable; it shouldn't be changed.


 

 

 

Examples

NOTE: if you plan to include your extension in a separated .XML file:

please disable or change enum extension if you don't need it

add the tag <include> and close it with </include>

Example 1: Matching a condition

The incoming call will be bridged only if it comes from 192.168.1.1. If it does, the destination number will be captured in $1, and the call will be bridged to the same number, at 192.168.2.2.

Note

This is a bizarre example. We can do better.

Code Block
languagexml
themeEmacs
<extension name="Test1">
	<condition field="network_addr" expression="^192\.168\.1\.1$"/>
	<condition field="destination_number" expression="^(\d+)$">
		<action application="bridge" data="sofia/profilename/$1@192.168.2.2"/>
	</condition>
</extension>

Note that this example is not the same as doing this:

Code Block
languagexml
themeEmacs
<extension name="Test1Wrong">
	<condition field="destination_number" expression="^(\d+)$"/>
	<condition field="network_addr" expression="^192\.168\.1\.1$">
		<action application="bridge" data="sofia/profilename/$1@192.168.2.2"/>
	</condition>
</extension>

The call will not be routed properly because the captured destination number in $1 is not available outside of the condition that created it.  This is a peculiarity of how captured values work.  You can work around this by storing the captured value in a standard variable, like this:

Code Block
languagexml
themeEmacs
<extension name="Test1_2">
	<condition field="destination_number" expression="^(\d+)$">
		<action application="set" data="dialed_number=$1"/>
	</condition>
	<condition field="network_addr" expression="^192\.168\.1\.1$">
		<action application="bridge" data="sofia/profilename/${dialed_number}@192.168.2.2"/>
	</condition>
</extension>

Example 2: Matching multiple conditions (AND) 

In this example we need to match a called number beginning with the prefix 1 AND match the incoming IP address at the same time.

Code Block
languagexml
themeEmacs
<extension name="Test2">
	<condition field="network_addr" expression="^192\.168\.1\.1$"/>
	<condition field="destination_number" expression="^1(\d+)$">
		<action application="bridge" data="sofia/profilename/$0@192.168.2.2"/>
	</condition>
</extension>

Notice that although we match with the rule 1(\d+)$ we don't use the variable $1 which would contain only the rest of the dialed number with the leading 1 stripped off, we use the variable $0 which contains the original destination number.

Example 3: Stripping leading digits

In this example we need to match a called number beginning with 00 but we also need to strip the leading digits. Assuming that FreeSWITCH™ receives the number 00123456789 and we need to strip the leading 00 digits, then we can use the following extension:

Code Block
languagexml
themeEmacs
<extension name="Test3.1">
	<condition field="destination_number" expression="^00(\d+)$">
		<action application="bridge" data="sofia/profilename/$1@192.168.2.2"/>
	</condition>
</extension>

If you anticipate receiving non-digits, or you want to match on more than just digits, you can use ".+" instead of "\d+".

Note

Exercise caution when using regular expressions containing (.*) or (.+). The more specific the regular expression, the less chance that your system can be exploited by an unauthorized user.

Example 4: Adding a prefix

In this example, numbers beginning with 00 are transformed into the same number, only beginning with 011 instead.  If

Fs
 receives 00123456789, we should dial 011123456789:

Code Block
languagexml
themeEmacs
<extension name="Test4">
	<condition field="destination_number" expression="^00(\d+)$">
		<action application="bridge" data="sofia/profilename/011$1@x.x.x.x"/>
	</condition>
</extension>

Example 5: SIP Profiles (dialing with different configurations) 

In this example we will demonstrate the use of profiles when using a FreeSWITCH endpoint that supports profiles, like mod_sofia. Assuming that we want to use different call settings (codecs, DTMF modes, etc) for sending the calls to different IP addresses, we can create different profiles. For example, in the configuration of sofia.conf, we see an example profile named "test", which we rename to profile1 for this example, and add a profile2 for comparison:

Code Block
languagexml
themeEmacs
<profile name="profile1">
    <param name="debug" value="1"/>
    <param name="rfc2833-pt" value="101"/>
    <param name="sip-port" value="5060"/>
    <param name="dialplan" value="XML"/>
    <param name="dtmf-duration" value="100"/>
    <param name="codec-prefs" value="PCMU@20i"/>
    <param name="codec-ms" value="20"/>
    <param name="use-rtp-timer" value="true"/>
</profile>
<profile name="profile2">
    <param name="debug" value="1"/>
    <param name="rfc2833-pt" value="101"/>
    <param name="sip-port" value="5070"/>
    <param name="dialplan" value="XML"/>
    <param name="dtmf-duration" value="100"/>
    <param name="codec-prefs" value="PCMA@20i"/>
    <param name="codec-ms" value="20"/>
    <param name="use-rtp-timer" value="true"/>
</profile> 

The difference between the two profiles are in the codecs. The first uses G.711 u-law and the second G.711 A-law.

Continuing the examples above, we have:

Code Block
languagexml
themeEmacs
 <extension name="Test5ulaw">
    <condition field="network_addr" expression="^192\.168\.1\.1$"/>
    <condition field="destination_number" expression="^1(\d+)$">
        <action application="bridge" data="sofia/profile1/$0@192.168.2.2"/>
    </condition>
</extension>

to send the call in G.711 uLaw and

Code Block
languagexml
themeEmacs
<extension name="Test5alaw">
    <condition field="network_addr" expression="^192\.168\.1\.1$"/>
    <condition field="destination_number" expression="^1(\d+)$">
        <action application="bridge" data="sofia/profile2/$0@192.168.2.2"/>
    </condition>
</extension>

 

Example 6: Calling registered user

This example shows how to bridge to devices that have registered with your FreeSWITCH box. In this example we assume that you have setup a sofia profile called 'local_profile' and your phones are registering with the domain example.com. Note the '%' instead of '@' in the data string.

Code Block
languagexml
themeEmacs
 <extension name="internal">
    <condition field="source" expression="mod_sofia" />
    <condition field="destination_number" expression="^(4\d+)">
        <action application="bridge" data="sofia/local_profile/$0%example.com" />
    </condition>
</extension>ondition>undefined</extension>undefined<extension name="internal">
<condition field="source" expression="mod_sofia" />
<condition field="destination_number" expression="^(4\d+)">
    <action application="bridge" data="sofia/local_profile/$0%example.com" />
</condition>undefined</extension>

 

Example 7: Action failover on failed action

The following example shows how it is possible to call another action if the first action fails.

If the first action is successful the call is bridged to 1111@example1.company.com and will exist until one of the parties hangs up. After this, no other processing will be done because the caller's channel is closed. (i.e.: 1111@example2.company.com is not called)

If the initial call to 1111@example1.company.com was not successful the channel will not be closed and the second action will be called.

Code Block
languagexml
themeEmacs
 <extension name="internal">
    <condition field="destination_number" expression="^1111">
        <action application="set" data="hangup_after_bridge=true"/>
        <action application="bridge" data="sofia/local_profile/1111@example1.company.com" />
        <action application="bridge" data="sofia/local_profile/1111@example2.company.com" />
    </condition>
</extension>

Note: If you have more than one action and the application of the first action 

DOES hangup the channel, the second action will NOT be called.

DOES NOT hangup the channel, the second action will be called.

Example 8: Check user is authenticated

The following example requires that a caller be authenticated before passing through. It was yanked from a mailing list post.

Code Block
languagexml
themeEmacs
 <extension name="9191">
    <condition field="destination_number" expression="^9191$"/>
    <condition field="${sip_authorized}" expression="true">
        <anti-action application="rejectrespond" data="407"/>
    </condition>
    <condition>
        <action application="playback" data="/tmp/itworked.wav"/>
    </condition>
</extension>

 

Example 9: Routing DID to an extension

To route incoming calls which come in to a certain DID to a fixed extension 1001, do something LIKE the following (from a mailing list post) (where XXXxxxxxxx is the phone number of your incoming DID)

In public.xml:

Code Block
languagexml
themeEmacs
 <extension name="test_did">
    <condition field="destination_number" expression="^(XXXxxxxxxx)$">
        <action application="transfer" data="$1 XML default"/>
    </condition>
</extension>

 

and then in default.xml have something like this in the default context:

Code Block
languagexml
themeEmacs
 <extension name="Local_Extension">
    <condition field="destination_number" expression="^(XXXxxxxxxx)$">
        <action application="set" data="dialed_ext=$1"/>
    </condition>
    <condition field="destination_number" expression="^${caller_id_number}$">
        <action application="set" data="voicemail_authorized=${sip_authorized}"/>
        <action application="answer"/>
        <action application="sleep" data="1000"/>
        <action application="voicemail" data="check default $${domain} ${dialed_ext}"/>
        <anti-action application="ring_ready"/>
        <anti-action application="set" data="call_timeout=10"/>
        <anti-action application="set" data="hangup_after_bridge=true"/>
        <anti-action application="set" data="continue_on_fail=true"/>
        <anti-action application="bridge" data="USER/1001@$${domain}"/>
        <anti-action application="answer"/>
        <anti-action application="sleep" data="1000"/>
        <anti-action application="voicemail" data="default $${domain} ${dialed_ext}"/>
    </condition>
</extension>

 

(the 1001 in the "bridge" line is the extension we're ringing)

FYI, calls from the "public" go into the public context where they then need to be transferred to another more friendly context for processing, like default. That is why you add the entry to public and the 'data="$1 XML PFC"' says to transfer called number $1 to the context PFC using XML dialplan. In the default context is where you actually ring the phone.

$${domain} a variable that it automatically fills in for you with your domain (most likely your IP) and the calling number. Just leave them as is.

Example 10: Route to a gateway extension with custom caller id

In this example we demonstrate an outgoing call with 10 digits from extension 1000 then route it to the asterlink.com gateway. This examples shows how to route for a specific extension and allows custom caller id for that extension.

Code Block
languagexml
themeEmacs
<extension name="asterlink.com">
    <condition field="caller_id_number" expression="^1000$"/>
    <condition field="destination_number" expression="^(\d{10})$">
        <action application="set" data="effective_caller_id_number=8001231234"/>
        <action application="set" data="effective_caller_id_name=800 Number"/>
        <action application="bridge" data="sofia/gateway/asterlink.com/1208$1"/>
    </condition>
</extension>

 

Example 11: Route based on number prefix

In this example we demonstrate routing to different destination based on NPANXX. Also how to respond to the calling party with a different failure message than the destination sends to FreeSWITCH.

Code Block
languagexml
themeEmacs
<extension>
    <condition field="network_addr" expression="^(66\.123\.321\.231|70\.221\.221\.221)$" break="on-false"/>
    <condition field="destination_number" expression="^\d+$" break="never">
        <action application="set" data="continue_on_fail=NORMAL_TEMPORARY_FAILURE,TIMEOUT,NO_ROUTE_DESTINATION"/>
        <action application="set" data="bypass_media=true"/>
        <action application="set" data="accountcode=myaccount"/>
    </condition>
    <condition field="destination_number" expression="^(1813\d+|1863\d+|1727\d+|1941\d+|404\d+)$" break="never">
        <action application="bridge" data="sofia/outbound_profile/${sip_to_user}@switch1.mydomain.com"/>
        <action application="info"/>
        <action application="respond" data="503"/>
        <action application="hangup"/>
    </condition>
    <condition field="destination_number" expression="^(1404\d+|1678\d+|1770\d+)$">
        <action application="bridge" data="sofia/outbound_profile/${sip_to_user}@switch2.mydomain.com"/>
        <action application="info"/>
        <action application="respond" data="503"/>
        <action application="hangup"/>
        <anti-action application="respond" data="503"/>
        <anti-action application="hangup"/>
    </condition>
</extension>

 

Example 12: Handle calls which match no extension

In this example we demonstrate how to catch invalid extensions/Destinations.

You need to add this extension at the bottom of your dialplan before ENUM can get included.

See mod_enum.

Code Block
languagexml
themeEmacs
 <extension name="catchall">
    <condition field="destination_number" expression=".*" continue="true">
        <action application="playback" data="misc/invalid_extension.wav"/>
    </condition>
</extension>

 

Example 13: Call Screening

In this example, we ask the caller for a name, connect to the called party and announce that name. The called party may then press 1 to accept the call, or hang up. If the called party hangs up, the caller is connected with voicemail.

Code Block
languagexml
themeEmacs
 <extension name="screen">
    <condition field="destination_number" expression="^(\d{4})$">
        <action application="set" data="call_screen_filename=/tmp/${caller_id_number}-name.wav"/>
        <action application="set" data="hangup_after_bridge=true" />
        <action application="answer"/>
        <action application="sleep" data="1000"/>
        <action application="phrase" data="voicemail_record_name"/>
        <action application="playback" data="tone_stream://%(500, 0, 640)"/>
        <action application="set" data="playback_terminators=#*0123456789"/>
        <action application="record" data="${call_screen_filename} 7 200 2"/>
        <action application="set" data="group_confirm_key=1"/>
        <action application="set" data="fail_on_single_reject=true"/>
        <action application="set" data="group_confirm_file=phrase:screen_confirm:${call_screen_filename}"/>
        <action application="set" data="continue_on_fail=true"/>
        <action application="bridge" data="user/$1"/>
        <action application="voicemail" data="default $${domain} $1"/>
        <action application="hangup"/>
    </condition>
</extension>

 

Example 14: Media recording

This extension is used to play/record media in audio (wav) format recording / playback extension

Thanks to rupa for the help.

Code Block
languagexml
themeEmacs
 <extension name="recording">
    <condition field="destination_number" expression="^(2020)$">
        <action application="answer"/>
        <action application="set" data="playback_terminators=#"/>
        <action application="record" data="/tmp/recorded.wav 20 200"/>
    </condition>
</extension>
<extension name="playback">
    <condition field="destination_number" expression="^(2021)$">
        <action application="answer"/>
        <action application="set" data="playback_terminators=#"/>
        <action application="playback" data="/tmp/recorded.wav"/>
    </condition>
</extension>

 

Example 15: Speaking Clock

This example will speak time using the Flite text to speech engine.

See: mod_flite

Code Block
languagexml
themeEmacs
<include>
    <extension name="SpeakTime">
        <condition field="destination_number" expression="^2910$">
            <action application="set" data="actime=${strftime(%H:%M)}"/>
            <action application="set" data="tts_engine=flite"/>
            <action application="set" data="tts_voice=slt"/>
            <action application="speak" data="It is +${actime}"/>
        </condition>
    </extension>
</include>

 

Example 16: Block certain codes

This extension example is to demonstrate how to block certain NPAs that you do not want to terminate based on caller id area codes and respond with SIP:503 to your origination so that they can route advance if they have other carrier to terminate to.

Code Block
languagexml
themeEmacs
<extension name="blocked_cid_npa">
    <condition field="caller_id_number" expression="^(\+1|1)?((876|809)\d{7})$">
        <action application="respond" data="503"/>
        <action application="hangup"/>
    </condition>
</extension>

 

Example 17: Receive fax from inbound did

To use the predefined fax_receive extension in freeswitch/conf/dialplan/default.xml for inbound calls, put this in freeswitch/conf/dialplan/public/fax.xml:

Code Block
languagexml
themeEmacs
<include>
    <extension name="incoming-fax">
        <condition field="destination_number" expression="^$${local_fax_number}$">
            <action application="set" data="domain_name=$${domain}"/>
            <action application="transfer" data="9178 XML default"/>
        </condition>
    </extension>
</include>

 

Then in freeswitch/conf/vars.xml you set your fax number to 1234 or whatever:

Code Block
languagexml
themeEmacs
<X-PRE-PROCESS cmd="set" data="local_fax_number=1234"/>

 

Example 18: Add international call prefix to effective_caller_id_number on incoming BRI calls

When using FreeTDM with zaphfc on a BRI line, the incoming calls received will not contain the international call prefix in the caller_id_number. This extension adds it to the effective_caller_id_number.

The international call prefix is explained here: http://en.wikipedia.org/wiki/International_prefix.

In Germany, the international call prefix is called "Verkehrsausscheidungsziffer" (VAZ), see http://de.wikipedia.org/wiki/Verkehrsausscheidungsziffer.

The international call prefix is never transmitted. It can be predicted by looking at the ToN information.

Code Block
languagexml
themeEmacs
<extension name="Add-VAZ" continue="true">
    <!-- On incoming BRI calls, the Verkehrsausscheidungsziffer (VAZ) is dropped. This extension adds it again to the caller_id_number. TODO: add support for international numbers -->
    <condition field="source" expression="^mod_freetdm$">
        <action application="set" data="effective_caller_id_number=0${caller_id_number}"/>
    </condition>
</extension>


Example 19: DISA 

Be able to dial into FS box and get a dialtone to dial again, just like in Asterisk's DISA()

In FS/conf/dialplan/public/*.xml

Code Block
languagexml
themeEmacs
 <!-- -->
<!-- -->
<!-- -->
<!-- CAUTION!!! We TRANSFER here. Possible security breach. CAUTION!!! -->
<!-- -->
<!-- -->
<!-- -->
<extension name="incoming-bri-wor">
    <condition field="destination_number" expression="^(disa_target)$">
        <action application="answer"/>
        <action application="start_dtmf"/>
        <action application="play_and_get_digits" data="2 5 3 37000 # $${base_dir}/sounds/en/us/callie/ivr/8000/ivr-please_enter_pin_followed_by_pound.wav $${base_dir}/sounds/en/us/callie/ivr/8000/ivr-pin_or_extension_is-invalid.wav digits ^$${DISA_PASSWORD}$"/>
        <action application="transfer" data="$1 XML default"/>
    </condition>
</extension>
<!-- -->
<!-- -->
<!-- -->
<!-- CAUTION!!! We TRANSFER here. Possible security breach. CAUTION!!! -->
<!-- -->
<!-- -->
<!-- -->
<extension name="incoming-bri-wor">
    <condition field="destination_number" expression="^(disa_target)$">
        <action application="answer"/>
        <action application="start_dtmf"/>
        <action application="play_and_get_digits" data="2 5 3 37000 # $${base_dir}/sounds/en/us/callie/ivr/8000/ivr-please_enter_pin_followed_by_pound.wav $${base_dir}/sounds/en/us/callie/ivr/8000/ivr-pin_or_extension_is-invalid.wav digits ^$${DISA_PASSWORD}$"/>
        <action application="transfer" data="$1 XML default"/>
    </condition>
</extension>


In FS/conf/dialplan/default/03_DISA.xml

Code Block
languagexml
themeEmacs
<!-- DISA - allow to dial into the box and get a dialtone like new trunk -->
<include>
    <extension name="DISA for FS">
        <condition field="destination_number" expression="^(disa_target)$">
            <action application="answer"/>
            <action application="read" data="2 15 'tone_stream://%(10000,0,350,440)' digits 30000 #"/>
            <action application="execute_extension" data="${digits}"/>
            <action application="transfer" data="disa_target XML default"/>
        </condition>
    </extension>
</include>

 

Please replace "disa_target" to you extension number.

You can use this technique to dial into your box from a pstn / mobile phone and get a dialtone to do anything with.

Example 20: Fix invalid caller ID 

If you run into a problem where your B-Leg do not like the invalid caller ID; for instance, you have an INVITE message with From: header as From: <sip:Unavailable@Unavailable.invalid:5060> You can force the invalid number to be replaced by a fixed caller ID number. The following example checks for a valid NANPA CLID:

Code Block
languagexml
themeEmacs
<extension name="invalid_caller_id_fix" continue="true">
    <condition field="caller_id_number" expression="^1?([2-9]\d{2}[2-9]\d{6})$">
        <action application="set" data="effective_caller_id_number=$1"/>
        <anti-action application="set" data="effective_caller_id_number=2135551212"/>
    </condition>
</extension>

 

Example 21: Block outbound caller ID 

To have caller ID block for calling party by dialing *67 follows by the dial number, you can do the following:

Code Block
languagexml
themeEmacs
<extension name="block_caller_id">
    <condition field="destination_number" expression="^\*67(\d+)$">
        <action application="privacy" data="full"/>
        <action application="set" data="sip_h_Privacy=id"/>
        <action application="set" data="privacy=yes"/>
        <action application="transfer" data="$1 XML default"/>
    </condition>
</extension>

 

Example 22: Play MOH while doing a database lookup 

If you want to play MOH while doing a data dip that takes a long time, the non-ESL way to do this by making the dialplan use the FSAPI via variable expansion to call luarun on the script. This way a new thread will be launched to execute the Lua script.

Code Block
languagexml
themeEmacs
<extension name="Get_Data">
    <condition field="destination_number" expression="^(Get_Data)$">
        <action application="play_and_get_digits" data="4 16 3 7000 # phrase:Enter_Case_Number phrase:Invalid_entry case_number \d+" />
        <action application="set" data="x=${expand(luarun GetDataFromLDAP.lua ${case_number} ${uuid})}"/>
        <action application = "playback" data="/tmp/LongMusicFile.wav"/>
    </condition>
</extension>

 

In the Lua script you can use the "uuid" argument passed, to break the MOH or transfer to another extension

Code Block
languageperl
themeEmacs
--GetDataFromLDAP.lua 
key = argv[1] sessionId = argv[2] api = freeswitch.API() --Do your data dips here --Once the database operations are done you can simply stop the MOH or transfer to another extension 
api:execute("uuid_break", sessionId) -- break the MOH

 

SIP-Specific Dialstrings

SIP dialing has several options. Here are some aspects of what you might call the anatomy of a SIP dialstring.

Dialing A SIP URI

Basic syntax is: sofia/my_profile/user@host Host can be a name or an IP address, for example:

Code Block
languagexml
themeEmacs
sofia/my_profile/1234@192.168.0.1

 

This would dial 1234 at host 192.168.0.1 via the profile "my_profile". If you use a name instead of an IP address, Sofia will try to resolve the name as a NAPTR or SRV record before trying it as a standard A record.

Dialing A Registered User

There are two options depending upon whether or not there is an alias for the domain.
Without an alias you can do this:

Code Block
languagexml
themeEmacs
sofia/my_profile/1234%mydomain.com

 

If you have an alias for the domain then this syntax is valid:

Code Block
languagexml
themeEmacs
sofia/mydomain.com/1234

 

Note how the profile does not need to be explicitly supplied in the dialstring.

Also you can do it this way for users defined in the directory:

Code Block
languagexml
themeEmacs
user/1234@mydomain.com

Dialing Through A Gateway (SIP Provider)

A gateway is a means for making outbound calls through a SIP provider. For example:

Code Block
languagexml
themeEmacs
sofia/gateway/mygateway.com/1234

 

This will dial through the gateway named "mygateway.com" to user 1234.
Note how there is no need to append anything after the user "1234"
This is an example of how NOT to do it:

Code Block
languagexml
themeEmacs
sofia/gateway/mygateway.com/1234@mygateway.com <==== WRONG WRONG WRONG

 

Dialing With A Specific Transport

Sometimes you will need to specify the transport, for example TCP, UDP, TLS, or SCTP.
This can be done by appending a semicolon and the transport method. For example:

Code Block
languagexml
themeEmacs
sofia/my_profile/1234@192.168.0.1;transport=tcp

 

Specifying The Codec

Occasionally you may want to force the system to use a specific codec. This syntax will accomplish that:

Code Block
languagexml
themeEmacs
{absolute_codec_string=XXXX}sofia/my_profile/user@your.domain.com

 

In this example, XXXX represents the codec to be used. The possible codec values are listed here
Additional dialstring examples from Absolute Codec String variable.

Getting Fancy With PortAudio

If you have PortAudio running and would like to specify the codec you need to originate first and bridge second:

Code Block
languagexml
themeEmacs
originate {absolute_codec_string=XXXX}sofia/default/foo@bar.com bridge:portaudio/auto_answer inline

 

Changing the SIP Contact user

FreeSWITCH normally uses mod_sofia@ip:port for the internal SIP contact. To change this to foo@ip:port, there is a variable, sip_contact_user:

Code Block
languagexml
themeEmacs
{sip_contact_user=foo}sofia/my_profile/1234@192.168.0.1;transport=tcp

 

Using a Custom SIP URI

FreeSWITCH allows you to specify custom URI's as needed. For example, you may need to interoperate with equipment that accepts a URI only if it is formatted in a particular way. The key is to prefix your SIP URI with "sip:" in the dialstring. For example:

Code Block
languagexml
themeEmacs
sofia/my_profile/sip:xxxx;phone-context=cdp.udp@somedomain.com;user=phone

 

The above example will send the the URI exactly as specified after the "sip:" prefix.

Testing the dialplan with a command line


Code Block
languagexml
themeEmacs
originate loopback/<destination number>/<mycontext> hangup inline

 

- Note: You can also set your variables to match your dialplan requirement. See below example:

Code Block
languagexml
themeEmacs
originate {toll_allow=international}loopback/0116628888888/default hangup inline

 

Setting up SIP Diversion Header for call forward

Code Block
languagexml
themeEmacs
<action application="export" data="sip_h_Diversion=<sip:2134445555@1.2.3.4>;reason=unavailable"/>