FreeSWITCH 拨号脚本之default.xml

#!/usr/bin/lua 
 
--Author:jncheng
--Version:20150520
--Email:[email protected]


-------------------------------------------------------------------------------------------
session:setVariable("continue_on_fail","USER_BUSY,NO_ANSWER,TIMEOUT,NO_USER_RESPONSE")
--session:setVariable("call_time_out","10")
--session:setVariable("bridge_answer_timeout","10")
--get the dial extension number
dest_exten = session:getVariable("destination_number")
src_exten = session:getVariable("caller_id_number")
exten_len=string.len(dest_exten)
fs_ivr_dir = "/usr/local/freeswitch/sounds"
cai_lin = fs_ivr_dir.."/ring.wav"
session:setVariable("ringback",cai_lin)
session:setVariable("effective_caller_id_name",src_exten)

--test the dial length
if (exten_len == 4) then
--freeswitch.consoleLog("INFO","the src dialpled numbers is:"..src_exten.."\n")
--Connect the local MySQL database(DBname,User,Password)
--local dbh = freeswitch.Dbh("test","fs","123qwe")
--Connect the ODBC DSN Database(odbcsourcename:Username:Passwod),in odbc.ini
local dbh = freeswitch.Dbh("odbc://fs:fs:123qwe")
--freeswitch.consoleLog("NOTICE","start connect DB...\r\n")
assert(dbh:connected())
--Get the dest_exten SIP Host
dbh:query("select sub_to_host as host from sip_subscriptions where sip_user="..dest_exten,function(row)
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.host))
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       dest_host_2 = string.format("%s",row.host)
end);
--freeswitch.consoleLog("INFO","the dest_exten host is:"..dest_host_2.."\n")
dbh:query("select realm as host from registrations where reg_user="..dest_exten,function(row)
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.host))
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       dest_host = string.format("%s",row.host)
end);
--freeswitch.consoleLog("INFO","the dest_exten host2 is:"..dest_host.."\n")

   --if the dest_host is nil,it mean the user not register,tell and hangup.
if (dest_host == nil and dest_host_2 == nil) then
   session:answer()
       session:setVariable("call_time_out","20")
       session:sleep(2000)
       session:streamFile(fs_ivr_dir.."/dialed_again.wav")
       session:sleep(2000)
       session:hangup()
else
       --Get the src_exten SIP Host 1
       dbh:query("select sub_to_host as host from sip_subscriptions where sip_user="..src_exten,function(row)
           --freeswitch.consoleLog("NOTICE","------------------------------------------")
           --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.host))
           --freeswitch.consoleLog("NOTICE","------------------------------------------")
           src_host_2 = string.format("%s",row.host)
       end);

--Get the src_exten SIP Host 2
dbh:query("select realm as host from registrations where reg_user="..src_exten,function(row)
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.host))
       --freeswitch.consoleLog("NOTICE","------------------------------------------")
       src_host = string.format("%s",row.host)
       end);

--If the src host == dest host,it meaning the user register on same host
       if (dest_host == src_host or dest_host_2 == src_host or dest_host == src_host_2 or dest_host_2 == src_host_2) then
   --get the local dial string
   dbh:query("select url from registrations where reg_user="..dest_exten,function(row)
           --freeswitch.consoleLog("NOTICE","------------------------------------------")
           --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.url))
           --freeswitch.consoleLog("NOTICE","------------------------------------------")
           exten_url = string.format("%s",row.url)
           end);
--define the split function,This not write by me.From internet
function Split(szFullString, szSeparator)  
             local nFindStartIndex = 1  
             local nSplitIndex = 1  
             local nSplitArray = {}  
             while true do  
               local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)  
               if not nFindLastIndex then  
               nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))  
               break  
             end  
             nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)  
             nFindStartIndex = nFindLastIndex + string.len(szSeparator)  
             nSplitIndex = nSplitIndex + 1  
             end  
             return nSplitArray  
           end  
           local list = Split(exten_url,";")
           local local_url = list[1]
--the log use for debug
--freeswitch.consoleLog("INFO","the dest_exten host is:"..dest_host.."\n")
--freeswitch.consoleLog("INFO","the src local url is:"..local_url.."\n")
--freeswitch.consoleLog("INFO","the dest_exten host2 is:"..dest_host_2.."\n")
session:setVariable("call_timeout","60")
session:execute("bridge",local_url)
           --Add tell why not scueefull bridge
           session:answer()
local fail_cause=session:getVariable("originate_disposition")
--freeswitch.consoleLog("ERR","the hangupCause is:"..fail_cause.."\n")
--fail_cause mail cause is : USER_BUSY,NO_ANSWER,CALL_REJECT
           session:streamFile(fs_ivr_dir.."/"..fail_cause..".wav")
session:sleep(2000)
           session:hangup()
else
   --if the src host != dest host ,then dial string is soifa/external/80090@$host:5080
if (dest_host == nil) then
               dest_url_new = "sofia/external/"..dest_exten.."@"..dest_host_2..":5080"
else
   dest_url_new = "sofia/external/"..dest_exten.."@"..dest_host..":5080"
end
--freeswitch.consoleLog("INFO","the dest url is:"..dest_host_new.."\n")
session:setVariable("call_timeout","60")
session:execute("bridge",dest_url_new)
--Add tell why not scueefull bridge
           session:answer()
local fail_cause=session:getVariable("originate_disposition")
--freeswitch.consoleLog("ERR","the hangupCause is:"..fail_cause.."\n")
--fail_cause mail cause is : USER_BUSY,NO_ANSWER,CALL_REJECT
           session:streamFile(fs_ivr_dir.."/"..fail_cause..".wav")
           session:sleep(2000)
session:hangup()
       end
end
--if the dialpled number length is not 4,tell use check the number  
else
 session:answer()
 session:setVariable("call_time_out","20")
 session:sleep(2000)
 session:streamFile(fs_ivr_dir.."/check_number.wav")
 session:sleep(2000)
 session:hangup()
end


你可能感兴趣的:(lua,freeswitch,拨号脚本)