[Freeswitch-users] Freeswitch dynamic routing of all calls
Vitalie Colosov
vetali100 at gmail.com
Fri Jul 8 01:24:46 MSD 2011
Hi,
It is easy to achieve using powerful Lua scripting and Core ODBC (
http://wiki.freeswitch.org/wiki/Using_ODBC_in_the_core)
If you did not have any experience with Lua, just copy paste script below,
and fix syntax errors if any :)
(Or you can achieve similar using perl, javascript, etc...)
Steps:
1. Add the following block inside your default dialplan (for example you
want to handle international calls, prefix "00" or "+")
-----------------------------------------------------------------------------
<extension name="international_with_some_routing">
<condition field="destination_number" expression="^(00|\+)(\d+)">
<action application="lua" data="call.lua $2"/>
</condition>
</extension>
-----------------------------------------------------------------------------
2. Create "call.lua" with the following lines and put it into "scripts"
folder under freeswitch main folder:
-----------------------------------------------------------------------------
--get db handler
local dbh = assert(freeswitch.Dbh("my_routing_db","my_user","my_pass"));
subscriber_number = session:getVariable("accountcode"); --or
session:getVariable("caller_id_number"); if you wish
called_number = argv[1];
--let's check about money
balance = 0;
dbh:query("select balance from subscriber where
subscriber_number='"..subscriber_number.."'",
function(row)
balance = tonumber(row.balance);
end);
if (balance <= 0) then
freeswitch.consoleLog("WARNING", "Call denied for
"..subscriber_number.." to "..called_number.." Balance: "..balance.."\n");
return;
end
--select price plan and gateway (you can implement more complex logic here,
to select the plan with lowest cost, etc)
--assuming you use mysql
dbh:query("select price_buy, price_sell, provider from dialplan where
instr('"..called_number.."',prefix)=1 order by prefix desc limit 1",
function(row)
price_buy=tonumber(row.price_buy);
price_sell=tonumber(row.price_sell);
called_gateway=row.provider;
end);
--add some checks if needed
call_duration_min = balance / price_sell;
call_duration_sec = call_duration_min * 60;
--release db handler
dbh:release();
--prepare outgoing session
called_parameters =
"{ignore_early_media=true,originate_timeout=90,price_sell="..price_sell..",price_buy="..price_buy..",subscriber_number="..subscriber_number..",hangup_after_bridge=true}";
called_string =
called_parameters.."sofia/gateway/"..called_gateway.."/"..called_number;
session:setVariable("ringback", "%(2000,4000,440,480)");
called_session = freeswitch.Session(called_string, session);
--sometimes it is needed :)
session:sleep(200);
if (called_session:ready()) then
-- this will disconnect the call after allowed seconds
session:execute("sched_hangup","+"..call_duration_sec);
--finally, bridge
freeswitch.bridge(session, called_session);
end
--that's it
-----------------------------------------------------------------------------
Make sure that you have valid registered gateway as returned by
"called_gateway" in your conf/sip_profiles.
Any questions, I will be glad to assist.
Regards,
Vitalie
2011/7/7 Nazim Aghabayov <nazim.aghabayov at gmail.com>
> Hello Mateusz,
>
> I'm finding ESL powerful yet complicated and using mod_xml_curl for
> dynamic routing.
> mod_xml_curl with with bindings = "dialplan" handles routing just fine.
> http://wiki.freeswitch.org/wiki/Mod_xml_curl
>
> Another approach is to a the Lua scripts for setting the variables
> "inside" the dialplan.
> FreeSWITH is a flexible system, so there are a lot of ways to achieve
> the same result.
>
> Best Regards,
> Nazim
>
>
> On 07/01/2011 05:55 PM, Mateusz Bartczak wrote:
> > OK but how can I respond for received events?
> >
> > I subscribe to receive events using following event socket command:
> >
> > event plain CHANNEL_CREATE
> >
> > then I got all events of that type and that's great but the question is
> how
> > can I handle those events, what's the syntax and where to put it?
> >
> > I will simply explain what I'm trying to achieve:
> >
> > 1. User dialed number
> > 2. CHANNEL_CREATE event is created
> > 3. I got this event using socket
> > 4. What to do now? How to respond for that event? For example I would
> like
> > to respond with dialstring to use, user called number 123, I would like
> to
> > return something like sofia/gateway123/00123
> >
> >
> >
> > 2011/6/30 Steven Ayre<steveayre at gmail.com>
> >
> >> Yes it's possible and I suggest you look at esl
> >>
> >> Steve on iPhone
> >>
> >> On 30 Jun 2011, at 09:54, Mateusz Bartczak<netcentrica at gmail.com>
> wrote:
> >>
> >>> Hi all
> >>>
> >>> I'm new to FS and I would like to know is it possible to implement
> >> following scenario:
> >>> 1. User dials number
> >>> 2. Routing script detects outgoing call event. Every call needs to be
> >> handled by routing script.
> >>> 3. Routing script takes in input: user name, domain, dialed number.
> Than
> >> it query database to find best SIP trunk to route the call, it also
> checks
> >> destination price per minute and calculates maximum call time for
> prepaid
> >> user.
> >>> 4. Routing script output is: SIP trunk to use, SIP call parameters (ie.
> >> callerid), maximum call duration
> >>> 5. FS read output from routing script and make call using returned
> >> parameters
> >>> Preferred routing implementation technology: background running unix
> >> deamon written in Java or PHP. Connection with FS via socket.
> >>> Event routing script will be multi-threaded, must be able to deal with
> a
> >> lot of calls in parallel and processing of one call should not block
> >> processing of other calls (I have this problem with Yate voip server,
> and
> >> that's really big problem)
> >>> Is it possible to do this using FS?
> >>> Any advices where to search for additional info? I know that there is
> >> event handler but can it return "dialstring" for outgoing call events?
> >>> Some code examples?
> >>>
> >>> I will really appreciate your help
> >>>
>
>
> _______________________________________________
> Join us at ClueCon 2011, Aug 9-11, Chicago
> http://www.cluecon.com 877-7-4ACLUE
>
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20110708/f8730f80/attachment-0001.html
More information about the FreeSWITCH-users
mailing list