Dialplan XML

From FreeSWITCH Wiki

(Redirected from Mod dialplan xml)
Jump to: navigation, search

Contents

Introduction

The XML Dialplan is the default dialplan that is used. This creates a flexible dialplan, and allows for 3rd party tools to be quickly and easily created since there is an abundance of software that can manipulate dialplans. XML is also easily and readily edited by hand without requiring any special tools other than a text editor.

The XML dialplans allow for Perl Regular Expression matching on fields, which gives you a lot of flexibility in a standardized format which some may be accustomed to already, and others can readily find documentation on how to work with Perl Regular Expressions.

Dial plan parser

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} 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 realtime info such as date and time or other quickly accessible information and should not be abused.

Anatomy of the XML Dialplan

There are several elements in the XML dialplan. In general you have context, extension, condition and action. Each is processed in order until you reach the action tag which tells freeswitch what action to perform. You are not limited to only one condition or action tag for a given extension.

Context

Contexts are a logical grouping of extensions. You may have multiple extensions contained within a single context.

The context tag has a required parameter of 'name'. There is one reserved name 'any' which matches any context. The name is to assist you in identifying the context. The "dialplan" section of the freeswitch.xml meta document may contain several contexts

<?xml version="1.0"?>
<document type="freeswitch/xml">
  <section name="dialplan" description="Regex/XML Dialplan">
    <context name="default">
    </context>
  </section>
</document>

Extension

Extensions are destinations for a call. They are given a name and a grouping of conditions and actions are contained within them instructing FreeSwitch what to do.

The extension tag has a required parameter of 'name'. This is just a arbitrary name that you give this extension to assist you in identifying it later. It has an optional argument of 'continue' which is set to true or false. If continue is true even if a match is found, FreeSwitch will continue to execute the following extensions for matches once this extension has completed its actions. The default is to not continue.

For example, to add extension 500 to your dialplan, add the following section to your dialplan as indicated above:
Informational Tip

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


 


Edit This
 <!--- ext 500 -->
 <extension name="500">
   <condition field="destination_number" expression="^500$">
       <!--- The % behind the username tells FS to lookup the user in it's local sip_registration database -->
       <action application="bridge" data="sofia/profilename/500%x.x.x.x"/> 
       <!--- x.x.x.x in the line above is the IP address to the FreeSWITCH server/device -->
       <!--- If you don't want to bridge a call to a local registered user, but to a SIP URI, use the @ instead of %: 
       <action application="bridge" data="sofia/profilename/500@x.x.x.x"/> -->
   </condition>
 </extension>

 


Legend: Text wrapped in [] indicates optional and is not part of the actual code. An '|' inside [] indicates mutiple possible values and also is not part of the code. Text wrapped in {} indicates it's a description of the parameter in place of the param itself.

<extension name="{exten_name}" [continue="[true|false]"]>
  • continue=true means even if an extension executes to continue parsing the next extension too. continue="false" is the default behavior and need not be specified.

The {exten_name} above may be anything but if it's an exact match with the destination number the parser will leap to this extension to begin the searching, but that does not mean it will execute the extension. Searching will either begin at the first extension in the context, or at the point the parser has jumped to in the case described above. Each condition is parsed in turn first taking the 'field' param. The parser will apply the Perl Compatible Regular Expression (go here for PCRE syntax information) to each 'field' param encountered. If the expression matches, it will parse each existing <action> tag in turn and add the data from the <application> tags to the channels todo list. If a matched expression contains any data wrapped in () the variables $1,$2..$N will be valid and expanded in any of 'data' params from the subsequent action tags.

If the expression does NOT match, it will parse each <anti-action> tag in turn and add the data from the <application> tags to the channels todo list.

Informational Tip

since there was no match the () feature is not available in anti-actions


 


The 'break' param indicates how to behave in relation to matching:

  • ) 'on-true' - stop searching conditions after the first successful match.
  • ) 'on-false' - stop searching after the first unsuccessful match. (default behavior)
  • ) 'always' - stop at this conditon regardless of a match or non-match.
  • ) 'never' - continue searching regardless of a match or non-match.

If not specified, it defaults to 'on-false'.

 <condition field="[{field_name}|${variable_name}|$api_func(api_args)]" expression="{expression}" break="[on-true|on-false|always|never]">
   <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>

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:

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

Condition

Conditions are pattern matching tags that help FreeSwitch decide if the current call should be processed in this extension or not. When matching conditions against the current call you have several variables that you can compare against.

  • context Why can we use the context as a field? Give us examples of usages please.
  • rdnis Redirected Number, the directory number to which the call was last presented.
  • destination_number Called Number, the number this call is trying to reach (within a given context)
  • dialplan Name of the dialplan module that are used, the name is provided by each dialplan module. Example: XML
  • caller_id_name Name of the caller (provided by the User Agent that has called us).
  • caller_id_number Directory Number of the party who called (callee) -- can be masked (hidden)
  • ani Automatic Number Identification, the number of the calling party (callee) -- cannot be masked
  • ani2 The type of device placing the call [1]
  • uuid Unique identifier of the current call? (looks like a GUID)
  • source Name of the FreeSwitch module that received the call (e.g. PortAudio)
  • chan_name Name of the current channel (Example: PortAudio/1234). Give us examples when this one can be used.
  • network_addr IP address of the signalling source for a VoIP call.

In addition to the above you can also do variables using the syntax ${variable} or api functions using the syntax %{api} {args}

Variables may be used in either the field or the expression, as follows

<condition field="destination_number" expression="^${some_destination}$">

or

<condition field="${some_var}" expression="^([[:alnum:]]*)$">

You have two tags that are required for conditions, 'field' which contains the variable you wish to match against and 'expression' which contains the regular expression to apply. Using () in a regular expression will result in $1, $2 ... $N being populated appropriately which can be used later in the action tag. There is an optional tag 'break' which can take the values on-true, on-false, always and never. This lets you select how FreeSwitch will continue to process the extension, if for example you have break="on-false" and a condition does not match, FreeSwitch discontinues processing any more conditions in this extension and moves onto the next. If the optional tag 'break' is not given, it defaults to break="on-false".

In the next example, an SPA3000 device has been configured to register with Freeswitch as a GW to make PSTN calls. The IP x.x.x.x is the IP address of the GW. (The SPA3000 GW is configured the same as if it were to be connected to Asterisk. The external line is on port 5061 on the SPA3000. You can find instructions by searching this Wiki).

<extension name="To PSTN">
  <condition field="fdnis" expression="9541231234"/> 
  <condition field="destination_number" expression="(.*)">
      <action application="bridge" data="sofia/profilename/$1@x.x.x.x:5061"/>
   </condition>
</extension>

If you need to do "nested conditions", you can accomplish the same thing using break="on-false" and thinking of the conditions as being "stacked". If the first condition fails, it will not continue to the next condition, thus achieving the same effect as nesting conditions (which is not allowed).

Action and Anti-Action

Action tags are executed when there is a match, anti-action tags are executed when there is not a match. There are two arguments to action tags, application and data. Application is the registered application to execute, for example bridge, data is the argument to that application for example sofia/profilename/123@myserver. When you use anti-action tags you cannot use $1 since the regexp did not match, that value will not be populated.

<context name="default">
  <extension name="demo">
    <condition field="destination_number" expression="^(\d{7})$">
      <action application="bridge" data="sofia/profilename/$1@myprovider"/>
      <anti-action application="playback" data="number-is-invalid.wav"/>
    </condition>
  </extension>
</context>

Available Actions

See API Reference and Dialplan Functions

EXAMPLES

Example 1

In the example below, the particular extension will be selected only if the IP address of the calling endpoint is 192.168.1.1. In the second condition, the dialed number is extracted in variable $1 and put in the data of the bridge application, in order to dial out to IP address 192.168.2.2

<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 the first condition field is TERMINATED by a / !!! The last condition field which contains the action/anti-action is terminated by a regular </condition> tag. Also note that the above example is NOT the same as below:

<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 Test1Wrong example will not route the call properly, because the variable $1 will not have any value, since the destination number was matched in a different condition field.

Example 2

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.

<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>

Here, 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

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:

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


On the other hand, if you anticipate receiving non-digits, or you want to match on more than just digits, use ".+" instead of "\d+" because \d+ matches numeric digits only, whereas a .+ will match all characters from the current position to the end of the string:

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

Example 4

In this example we need to strip the leading digits as above, but we also need to place a new prefix before the called number. Assuming that FreeSWITCH™ receives the number 00123456789 and we need to replace the 00 with the 011, we can use the following extension:

<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

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:

<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 G711 uLaw and the second G711 ALaw.

Continuing the examples above, we have:

<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 G711 uLaw and

<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

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.

<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>

Example 7

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. (ie: 1111@example2.company.com is not called)

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

<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

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

 <extension name="9191">
   <condition field="destination_number" expression="^9191$"/>
   <condition field="${sip_authorized}" expression="true">
     <anti-action application="reject" data="407"/>
   </condition>

   <condition>
     <action application="playback" data="/tmp/itworked.wav"/>
   </condition>
 </extension>

Example 9

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:

   <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:

     <extension name="Local_Extension">
     <condition field="destination_number" expression="^(XXXxxxxxxx)$" continue="on-true">
       <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

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.

   <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

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.

<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>


Related

Personal tools
Community