How to do routing using javascript
(→How it works) |
|||
Line 61: | Line 61: | ||
=== 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 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 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 main code flow (that is, outside any function) is then executed after setting the [[call.route]] message parameters in the message variable. | ||
− | 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. |
=== DB Example=== | === DB Example=== |
Revision as of 18:16, 18 March 2013
To configure a script for Javascript routing you have to list it in the javascript.conf file.
The Javascript module has support for programmatically routing a call step by step.
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
- retrieve information from database
Contents |
Configure script in Javascript module
To configure a script for Javascript routing it must be listed in the javascript.conf file:
[general] routing=example.js
Configure a gateway
The example.js for outside routing requires to configure accfile.conf. In this way a gateway is configured to route the calls.
[gwout] enabled=yes protocol=sip username=outcalls password=mypass registrar=a.b.c.d
Routing Script
Write example.js in /usr/src/yate/share/scripts where Yate scripts are (or where you have 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 message.line = "gwout"; Channel.callJust("line/"+message.called); }
How it works
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 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.
DB Example
This example uses Javascript to retrieve information from DB
// Get DB Object var m = new Message("database"); // Specify connection to use m.account = "yateadmin"; // Define Query m.query = "SELECT * FROM lines WHERE location IS NOT NULL ORDER BY line LIMIT 5"; // Run the Query if (m.dispatch()) { if (m.rows > 0) { Engine.output("Got " + m.rows + " records of " + m.columns + " fields"); Engine.output("result[0,0] = " + m.getResult(0,0)); var res = m.getColumn("location"); for (var i = 0; i < res.length; i++) { Engine.output("location[" + i + "] = " + res[i]); } } } else { Engine.output("Query failed"); }
See also