How to do routing using javascript

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
(Configure script in Javascript module)
m (Routing Script)
 
(28 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The Javascript module has support for programmatically routing a call step by step.<br>
+
The Javascript module offers support for programmatically routing a call.<br>
  
 
Using the example below you will learn how to:
 
Using the example below you will learn how to:
Line 8: Line 8:
 
===Configure script in Javascript module ===
 
===Configure script in Javascript module ===
  
To configure a routing script you must be listed in the javascript.conf file:
+
To configure a routing script you must list it in the javascript.conf file.
 +
 
 +
Parameter '''routing''' from '''[general]''' section must contain the name of the script.
  
 
  [general]
 
  [general]
Line 15: Line 17:
 
===Configure a gateway===
 
===Configure a gateway===
  
The example.js for outside routing requires to configure [[Accfile|accfile.conf]]. In this way a gateway is configured to route the calls.
+
In order to route outside, a gateway is required. To set the gateway you must configure it in [[Accfile|accfile.conf]].
  
 
  [gwout]
 
  [gwout]
Line 22: Line 24:
 
  username=outcalls
 
  username=outcalls
 
  password=mypass
 
  password=mypass
  registrar=a.b.c.d
+
;the registrar has the following format IP:port
 +
  registrar=192.168.168.1:5060
  
 
=== Routing Script===
 
=== Routing Script===
  
Write example.js in /usr/src/yate/share/scripts where Yate scripts are (or where you have Yate sources).
+
This is the example.js script. <br>
 +
By default your script should be in share/scripts directory from Yate sources.
  
 
  '''// First display calling and called party numbers'''
 
  '''// First display calling and called party numbers'''
Line 35: Line 39:
 
         '''// 5060 is the default SIP port. When using the default port you don't need to specify it'''
 
         '''// 5060 is the default SIP port. When using the default port you don't need to specify it'''
 
         Channel.callJust("sip/sip:" + message.called + "@192.168.168.1:5060");
 
         Channel.callJust("sip/sip:" + message.called + "@192.168.168.1:5060");
 +
 
  } else if (message.called.substr(0,2)=="00" && message.called.length>10) {
 
  } else if (message.called.substr(0,2)=="00" && message.called.length>10) {
 
         '''//route calls using a function '''
 
         '''//route calls using a function '''
 
         routeOutside();
 
         routeOutside();
 +
 
  } else if (message.called.match("^60")) {
 
  } else if (message.called.match("^60")) {
 
         '''//route calls starting with 60 to 192.168.168.1'''
 
         '''//route calls starting with 60 to 192.168.168.1'''
         Channel.callJust("h323/"+message.called+"@192.168.168.1:1720");
+
         Channel.callJust("h323/" + message.called + "@192.168.168.1:1720");
 +
 
  } else if (message.caller=="209") {
 
  } else if (message.caller=="209") {
 
         '''//route calls from caller: 209'''
 
         '''//route calls from caller: 209'''
         Channel.callJust("iax/user_iax@192.168.168.1:4569/"+message.called+"@iaxcontext");
+
         Channel.callJust("iax/user_iax@192.168.168.1:4569/" + message.called + "@iaxcontext");
 +
 
  } else {
 
  } else {
 
         '''//route other calls that don't match the above rules'''  
 
         '''//route other calls that don't match the above rules'''  
Line 50: Line 58:
 
   
 
   
 
  function routeOutside() {
 
  function routeOutside() {
 +
   
 
     '''// rewrite caller number for all calls that will be routed to this account'''
 
     '''// rewrite caller number for all calls that will be routed to this account'''
 
     message.caller = "+40211231234";
 
     message.caller = "+40211231234";
 +
 
     '''// rewrite called number: strip 00 and add +_in front of the number'''
 
     '''// rewrite called number: strip 00 and add +_in front of the number'''
 
     message.called = "+" + message.called.substr(2);
 
     message.called = "+" + message.called.substr(2);
 +
 
     '''// send all calls starting with +40 to line "gwout" defined in accfile.conf'''
 
     '''// send all calls starting with +40 to line "gwout" defined in accfile.conf'''
     message.line = "gwout";
+
     if(message.called.substr(3) == "+40")        '''// alternative: if(message.called.match("^\+40.*"))
     Channel.callJust("line/"+message.called);
+
    {
 +
          message.line = "gwout";
 +
    }
 +
     Channel.callJust("line/" + message.called);
 
  }
 
  }
  
 
=== How it works===
 
=== How it works===
  
<!--When a call is coming in a new instance of the already parsed routing script is created and associated with the inbound call leg.<br>-->
+
When a call is coming, a inbound call leg is created.
When a call is coming, a new instance is created and associated with the inbound call leg.
+
  
The main code flow (that is, outside any function) is then executed after setting the [[call.route]] message parameters in the message variable.
+
The inbound call leg is associated with a new script instance.
 +
 
 +
The  parameters in the [[call.route]] message are set in the '''message''' variable.
 +
 
 +
Then the main code flow(that is, outside any function) is executed.
  
 
The Javascript code can make decisions based on these parameters and can call methods of the '''Channel''' object to route the call to the desired destination.
 
The Javascript code can make decisions based on these parameters and can call methods of the '''Channel''' object to route the call to the desired destination.
Line 71: Line 88:
 
'''See also'''
 
'''See also'''
  
* [[Javascript_routing|Javascript routing module]]
+
* [[Routing]]
 +
* [[Javascript]]
 +
* [[Accfile|accfile.conf]]
 +
 
 +
[[Category:Javascript]] [[Category:Programmers]] [[Category:Routing]]

Latest revision as of 12:03, 22 March 2015

The Javascript module offers support for programmatically routing a call.

Using the example below you will learn how to:

  • route calls starting with specific blocks of numbers to the desired destinations
  • route calls to a specific gateway set in accfile module

Contents

[edit] Configure script in Javascript module

To configure a routing script you must list it in the javascript.conf file.

Parameter routing from [general] section must contain the name of the script.

[general]
routing=example.js

[edit] Configure a gateway

In order to route outside, a gateway is required. To set the gateway you must configure it in accfile.conf.

[gwout]
enabled=yes
protocol=sip
username=outcalls
password=mypass
;the registrar has the following format IP:port
registrar=192.168.168.1:5060

[edit] Routing Script

This is the example.js script.
By default your script should be in share/scripts directory from Yate sources.

// First display calling and called party numbers
Engine.output("Got call from '" + message.caller + "' to '" + message.called + "'");

if (message.called.substr(0,2)=="19") {
        // route calls starting with 19 to 192.168.168.1
        // 5060 is the default SIP port. When using the default port you don't need to specify it
        Channel.callJust("sip/sip:" + message.called + "@192.168.168.1:5060");

} else if (message.called.substr(0,2)=="00" && message.called.length>10) {
        //route calls using a function 
        routeOutside();

} else if (message.called.match("^60")) {
        //route calls starting with 60 to 192.168.168.1
        Channel.callJust("h323/" + message.called + "@192.168.168.1:1720");

} else if (message.caller=="209") {
        //route calls from caller: 209
        Channel.callJust("iax/user_iax@192.168.168.1:4569/" + message.called + "@iaxcontext");

} else {
        //route other calls that don't match the above rules 
        Channel.callJust("wave/play//var/spool/sounds/hello.au");
}

function routeOutside() {
    
    // rewrite caller number for all calls that will be routed to this account
    message.caller = "+40211231234";

    // rewrite called number: strip 00 and add +_in front of the number
    message.called = "+" + message.called.substr(2);

    // send all calls starting with +40 to line "gwout" defined in accfile.conf
    if(message.called.substr(3) == "+40")        // alternative: if(message.called.match("^\+40.*"))
    {
         message.line = "gwout";
    }
    Channel.callJust("line/" + message.called);
}

[edit] How it works

When a call is coming, a inbound call leg is created.

The inbound call leg is associated with a new script instance.

The parameters in the call.route message are set in the message variable.

Then the main code flow(that is, outside any function) is executed.

The Javascript code can make decisions based on these parameters and can call methods of the Channel object to route the call to the desired destination.


See also

Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers